FAQ on iOS5 Storyboard

Designing iOS user interface with storyboard was introduced in iOS5. It helps to know of the following classes: UIStoryboardUIStoryboardPopoverSegueUIStoryboardSegue. The recommended practice is to have one storyboard for the entire application. I’ll address some of the common questions related to storyboard.

1. How do you pass data between two view controllers in storyboard?
You do that by overriding – [UIViewController prepareForSeque:sender:]. It is invoked on the source view controller and data can be passed back to source view controller by providing implementation of your own custom delegate. The source view controller can be accessed by [segue sourceViewController] and destination view controller can be accessed by [segue destinationViewController].

2. What is a static table view cell?
Table view cells that you can design inline and don’t need a data source. For static cells to work the view needs to be in a UITableViewController.

3. What is a dynamic table view cell?
The most common way of providing a table view cell. You can design it in the xib and it needs a table view data source. A very nice feature of storyboard.

4. How do you set which view controller is the first one to show?
Select a view controller and in Utilities pane go to Attributes Inspector, there check the property ‘Is Initial View Controller’.

5. How to add navigation controller to an existing view controller in storyboard?
From Menu you do: (Editor -> Embed In -> Navigation Controller)

6. How do you set delegate of destination view controller during transition?
Inside – [UIViewController prepareForSeque:sender:] you do [segue destinationViewController].delegate = self

7. How do you programmatically load a storyboard in AppDelegate or for that matter anywhere else?
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@”MainStoryboard” bundle:nil];
self.window.rootViewController = (UINavigationController*)[storyboard instantiateInitialViewController];

(MainStoryboard is name of your storyboard file, replace it in case you use a different name)

See also -[UIStoryboard instantiateViewControllerWithIdentifier:]

8. How do you access a storyboard from within a view controller?
– [UIViewController storyboard]

9. How do you provide a custom segue?
By subclassing UIStoryboardSegue and overriding – [UIStoryboardSegue perform]. Inside method perform you can access source and destination view controllers by [self sourceViewController], [self destinationViewController]

10. How do you close a view controller opened as modal through segue transition?
-[UIViewController dismissModalViewControllerAnimated:]

11. How do you programmatically perform a segue transition?
The segue has to be specified by an identifier in the xib and then the source view controller can call
– [UIViewController performSegueWithIdentifier:sender:]

Feel free to leave a comment if you like any other questions answered.


Checking Type of Internet Connection Wifi/Mobile

There are primarily two ways of connecting to internet in android. It can be either wifi or 3G (mobile data connection). If its a wifi connection and the application wants the wifi connection to stay active while the device goes to sleep then acquire a wifi lock and release it when the application is finished with internet related work otherwise the application will lose connectivity after devices goes to sleep. On the other hand 3G does not requires any type of lock to stay awake. A code can inquire about the type of network a device is connected to using following piece of code:

ConnectivityManager conManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = conManager.getNetworkInfo((ConnectivityManager.TYPE_WIFI));
NetworkInfo mobileInfo = conManager.getNetworkInfo((ConnectivityManager.TYPE_MOBILE));

if (wifiInfo.isConnectedOrConnecting() )
       Log.v("NetworkState", "Connected or connecting to wifi");
else if (mobileInfo.isConnectedOrConnecting())
       Log.v("NetworkState", "Connected or connecting to mobile data connection");

Add the following permission to the application AndroidManifest.xml otherwise the application will throw a run time exception:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Registering for Broadcast through AndroidManifest

Here is a small tip that took me a while to figure out but hopefully will save someone time and frustration. I have a BroadcastReceiver in my application that listens for network connectivity changes so the app. can resume background work on network availability.

If you register for receiver in AndroidManifest.xml then you need to write the actual constant value inside the <intent-filter> tag:

       <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>

The value is for the intent constant ConnectivityManager.CONNECTIVITY_ACTION. There are no warning or exceptions if you put this constant variable straight away in AndroidManifest.xml but the receiver won’t catch the network change events so a little hard to debug.

Note that if you register for a broadcast receiver from within code then just referring to the constant will work fine.

Checking your app. against Android Market technical filters

As android is designed to run on many different type of devices so an app has to be designed such that it is able to cope variety of devices with different hardware. I been working on an android app which uses internet so its not a must to have wifi for the device as it could just as well connect with 3G. I wanted to be sure that the app will be available for devices that might not have wifi but still could connect using 3G. This can be configured through the <uses-feature> tag in the application AndroidManifest.xml:

<uses-feature android:name="android.hardware.wifi" android:required="false" />

What android market does is that it filters the applications for a device so that users see and download only those applications which are compatible with their device. The market filtering is based on explicit and implicit features of an application and it is implemented in part by the aapt tool. A developer can check against this filtering by the following command:

$ aapt dump badging <path_to_exported_.apk>

The tool will return you a lot of other information such as what screen size and density an application is compatible with, what language it supports etc. The aapt tool can be found under SDK Install Dir/platforms/android-x/tools or SDK Install Dir/platform-tools

Here is the output by the above command for my application:

package: name=’guestshots.activities’ versionCode=’1′ versionName=’1.0′
application: label=’GuestShots’ icon=’res/drawable-mdpi/logo.png’
launchable activity name=’guestshots.activities.AccountSetup’label=’GuestShots’ icon=”
supports-screens: ‘small’ ‘normal’ ‘large’
supports-any-density: ‘true’
locales: ‘–_–‘
densities: ‘120’ ‘160’ ‘240’

So looking at this output I’m 100% sure that my app. doesn’t force the wifi requirement and will be available for all three density and screen size devices and that a device need to have touch screen in order to use my application.

Working with Android SD Cards image on Mac

I wanted to load images (could be some other media file for that matter) into an sd card and connect that sd card image with the an android emulator. The post by Jayway team (http://blog.jayway.com/2009/04/22/working-with-sd-cards-in-the-android-emulator/) helped me to get started but those instructions are linux specific and didn’t work for me on mac. In the end it turned out pretty simple. So here is what I did:

Go to SDK Install Dir/tools folder:

./mksdcard 128M sdcard1.iso

Now clicking the .iso file mac would automatically load it in Finder. Thanks to Jayway post I created a directory /dcim/Camera in the loaded iso file and dragged couple of images into this directory.

From here onwards you have couple of options:

1. Create an android virtual device (avd) using the android SDK Manager or android command line tool, whichever you prefer, and specify the path to the sd image file (sdcard1.iso). To shoot up the GUI utility just type ./android in SDK Install Dir/tools folder. For command line utility refer here: http://developer.android.com/guide/developing/tools/avd.html

To use an existing avd:

2. emulator -avd my_android1.5 -sdcard sdcard1.iso

And there you go you should be able to see the images in Android Gallery application.