Tag Archives: iOS

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.

 

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:

 

Xcode – convert iPhone storyboard to iPad storyboard

How to convert an iPhone storyboard into an iPad storyboard:

Also see this answer — to convert an iPhone storyboard to an iPad storyboard, do the following:

  1. From Xcode, right-click on the storyboard and choose Open As → Source code
  2. Search for targetRuntime=”iOS.CocoaTouch” and change it to targetRuntime=”iOS.CocoaTouch.iPad”
  3. Right-click on the storyboard again and choose Open As → iOS Storyboard

The storyboard will now show all views in the correct size.

XCode – Rename Scheme

You need to click on the scheme button, then hit “Manage Schemes”. Once you do so, you’ll have a window like this:

enter image description here

Select the scheme you want to change, and hit Return.

Now the scheme name will be editable, and you can change it to your hearts desire. Then hit Return again to save it.