Category Archives: Code Snippets

Read and Write Array, Dictionary and Other Collections to Files

http://iosdevelopertips.com/data-file-management/read-and-write-array-dictionary-and-other-collections-to-files.html

 

With just a few lines of code, you can read/write collections to/from files. The code below shows examples for writing and reading back both arrays and dictionaries.

Read and Write Collections to File

 

Write/Read data to .plist file

http://ipgames.wordpress.com/tutorials/writeread-data-to-plist-file/

First of all add a plist to your project in Xcode. For example “data.plist”.
Next, look at this code which creates path to plist in documents directory:

1) Create a list of paths.
2) Get a path to your documents directory from the list.
3) Create a full file path.
4) Check if file exists.
5) Get a path to your plist created before in bundle directory (by Xcode).
6) Copy this plist to your documents directory.

Read data:

 

Write data:

 

Remember about two things:


1) You must create a plist file in your Xcode project.

2) To optimize your app, better is to save all the data when application (or for example view) is closing. For instance in applicationWillTerminate. But if you are storing really big data, sometimes it cannot be saved in this method, because the app takes too long to close and the system will terminate it immediately.

 

How to change Status Bar text color in iOS 7

No need to code. It can all be done from the plist:

(1)  Set “View controller-based status bar appearance” to “NO”
(2)  Set “Status bar style” to “Opaque black style“.

Raw values are:
UIViewControllerBasedStatusBarAppearance = NO
UIStatusBarStyle = UIStatusBarStyleBlackOpaque

http://stackoverflow.com/questions/17678881/how-to-change-status-bar-text-color-in-ios-7

Change the Back button Title in iOS UINavigation

In the calling controller (meaning, the parent controller where you will push the new controller), add this in viewdidload:

 

http://programming.mvergel.com/2012/11/change-back-button-title-in-ios.html#.UseyJXk9XfM

iOS7 – Stop views from going under the navigation bar or tab bar in your viewController

First check for iOS 7 then implement:   self.edgesForExtendedLayout = UIRectEdgeNone;

 

UIAlertView – a simple text input dialog box

Create the alertView with style UIAlertViewStylePlainTextInput:

This produces an alertView:

UIAlertView that uses the UIAlertViewPlainTextInput alertStyle to ask a user name

When pressing any buttons, the regular delegate methods will be called and you can extract the textInput there like so:

 

View rotation methods

View rotation methods for iOS5 and iOS6

 

Segue – prepareForSegue

Make sure to give the segue an ID so that it can be found. In this example the segue ID would be “mySegueID”.

 

 

XCode – Size and designation iOS icons (Icon Sizes and Names) for iPhone and iPad

Device / Screen File Name (PNG) Icon Size (pixels)
iPhone and iPod
Application Icon for iPhone (retina display) Icon@2x.png 114 x 114
Application Icon icon for iPhone Icon.png 57 x 57
Settings / Spotlight icon for iPhone (retina display) Icon-Small@2x.png 58 x 58
Settings / Spotlight icon for iPhone Icon-Small.png 29 x 29
Launch image Portrait (retina display) Default@2x.png 640 x 960
Launch image Portrait Default.png 320 x 480
iPhone 5
IPhone 5 Launch image for Portrait (retina display) Default-568h@2x.png 640 x 1136
iPad
Application Icon for the new iPad (retina display) Icon-72@2x.png 144 x 144
Application Icon for the iPad Icon-72.png 72 x 72
Settings / Spotlight icon for iPad Icon-Small-50@2x.png 100 x 100
Settings / Spotlight icon for iPad Icon-Small-50.png 50 x 50
Launch image Portrait (retina display) Default-Portrait@2x.png 1536 x 2008
Launch image Portrait Default-Portrait.png 768 x 1004
Launch image Landscape (retina display) Default-Landscape@2x.png 2048 x 1496
Launch image Landscape Default-Landscape.png 1024 x 748
iTunes App Store
App icon for the App Store (retina display) iTunesArtwork@2x.png 1024 x 1024
App icon for the App Store iTunesArtwork.png 512 x 512

NSNotificationCenter – post and listen for a notification

In your first view controller (where you need to be notified of some event) you implement code to register and remove notification.

Now with the code above in place in your first view controller you will simply call it whenever you need it with this line of code which posts the notification: