Coding Explorer Blog

Exploring how to code for iOS in Swift and Objective-C

  • Home
  • Apps
  • About
  • Contact

Archives for January 2014

Storing data with NSUserDefaults

Last updated on June 25, 2014

How can you store data between application launches?

There are plenty of ways, but here I will show you the simplest, NSUserDefaults.

NSUserDefaults is a simple property list (aka plist) where an app can store simple data.  While there is no limit to its size (besides the device’s own limits), you should not store a large amount of data here.  The file is written and read atomically (as a whole), so the more data that is in there, the longer that will take.  Nonetheless, it is a great place to store settings, high scores, and the like.

[Read more…]

Filed Under: Syntax

Updating an app when returning from background in iOS

Last updated on June 25, 2014

I am working on an app that needs to update the UI based on the current date when it is loaded.  I told it to do the update in my viewWillAppear, but about half the time it would not update when I loaded the program.  Shouldn’t viewWillAppear always be called when your view is about to appear?  Apparently not, but I’ll tell you how I got the functionality I was looking for.

Updating from the background with Notifications

So, viewWillAppear will run every time your view is loaded from disk, or being called to be onscreen by a segue from another view.  However, when I leave the app, use some other apps and come back, as far as my app was concerned, this view never left.  It left as far as the system was concerned, but in my app’s little world, it just had the same view up as it did when I left the app.  When I left the app, I sent it to the background.  That is the difference.

So, how do I update my screen when it returns from the background?  There are two notifications that would be of help here.  They are:

  • UIApplicationWillEnterForegroundNotification
  • UIApplicationDidBecomeActiveNotification

As your program is brought back from the background, these two notifications are raised, in that order.  UIApplicationWillEnterForegroundNotification will run right before the view should appear onscreen, and UIApplicationDidBecomeActiveNotification should run just after.  In my previous post about Supporting Dynamic Type, I mentioned how to subscribe to notifications, but for simplicity’s sake, I will show you here.

[Read more…]

Filed Under: Syntax

Supporting Dynamic Type for iOS7 Apps

Last updated on June 25, 2014

In a program I was working on, I decided to use Dynamic Type.  You can read about it here, but suffice it to say, it is a new way of handling text sizes in iOS7.  The user can set a value in their iOS device’s settings to set all text to be bigger or smaller than default, for any app that implements Dynamic Type.  This is a bit more advanced than my other posts, but it is surprisingly easy to do, so I thought I would write a post about what I learned.

Setup and using preferredFontForTextStyle

First off, you have to create an outlet for any label, textfield, or whatever you have that you want to dynamically change the text size of, so as to have a place to set the font.  The easiest way to do this is to use the assistant editor on your view, and command+drag your text control to the assistant editor (in the @interface area of your .m for a private outlet).  Once you release it will ask you a few questions, and the defaults are fine usually, so you just have to give it a name.

[Read more…]

Filed Under: Syntax

Subscribe to the Coding Explorer Newsletter

* indicates required

Follow Us

Facebooktwitterrss

Recent Posts

  • Error Handling in Swift
  • Creating and Modifying a URL in your Swift App
  • Watch Connectivity in Swift — Application Context
  • watchOS Hello World App in Swift
  • API Availability Checking in Swift

Categories

  • Class Reference
  • General
  • Interface Builder
  • My Apps
  • Objective-C
  • Swift
  • Syntax
  • Tutorial
  • Uncategorized

Archives

  • May 2016
  • March 2016
  • February 2016
  • December 2015
  • July 2015
  • June 2015
  • April 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • January 2014
  • November 2013
  • September 2013
  • August 2013
  • July 2013
  • Terms Of Use
  • Privacy Policy
  • Affiliate Disclaimer

Subscribe to the Coding Explorer Newsletter

* indicates required

Copyright © 2025 Wayne Media LLC · Powered by Genesis Framework · WordPress