UIAlertView and UIActionSheet

iOS 7 screenshots:

iOS 6 screenshots:

The app pauses for three seconds with a white screen and white status bar (black text on top of the status bar). Then it displays a UIAlertView with four buttons. If you press Ignore, it will wait for three more seconds and then display a UIActionSheet with five butons. If you press Call Premier Khrushchev, it will launch the Safari app and go to the Wikipedia article for Nikita Khrushchev. See the novel Fail-Safe, the film (Henry Fonda, Walter Matthau), and the video (George Clooney, Harvey Keitel).

A UIAlertView pops in out of nowhere. A UIActionSheet slides up from the bottom of a UIView. See Alert Views and Action Sheets in the Human Interface Guidelines.

A UIAlertView has a delegate that adopts the protocol UIAlertViewDelegate. A UIActionSheet has a delegate that adopts the UIActionSheetDelegate protocol. Our ViewController plays the rôle of both delegates.

Source code in Alert.zip

  1. main.m
  2. Class AlertAppDelegate
  3. Class ViewController calls the showAlert: and showActionSheet: methods of the View.
  4. Class View
  5. Alert-Info.plist: in older versions of iOS, the information property list file had to contain the key CFBundleURLTypes.

Enumerations

A list of enumerations is a series of variables that hold consecutively numbered integers starting at zero by default. See the two lists at the top of ViewController.m.

Formatted for a mobile device

The URL
http://en.m.wikipedia.org/wiki/Nikita_Khrushchev
is formatted for a browser on a mobile device. The URL
http://en.wikipedia.org/wiki/Nikita_Khrushchev
is formatted for a browser on a desktop computer. Both URLs have one underscore.

Curly braces in a case

I had to write {curly braces} around the case actionKhrushchev: of the switch statement in the actionSheet:clickedButtonAtIndex: method of class ViewController because I declared variables in this case.

Launch another app

If the user presses the “Call Premier Khrushchev” button of the UIActionSheet, the applicationWillResignActive: and applicationDidEnterBackground: methods of the application delegate will be called, in that order. Then another application (Safari) will be launched. See the case actionkhrushchev: in the switch statement in the actionSheet:clickedButtonAtIndex: method in class ViewController.

When we press the Home button to stop Safari and resume this app, the applicationWillEnterForeground: and applicationDidBecomeActive: methods of the application delegate will be called, in that order.

The URL scheme

The URL can begin with one of the following schemes. Only http: worked on my iPhone Simulator. See Communicating with Other Apps. Does IHasApp tell you what schemes are accepted by the apps on your iPhone? iTunes Linkmaker.

  1. http:
  2. https:
  3. tel:
  4. mailto:

Things to try

  1. Change the URL to @"http://maps.google.com/maps?q=Moscow"

  2. Instead of scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:, would it be easier to call the performSelector:withObject:afterDelay: we saw here?

  3. If anything goes wrong with the recording and playback in Segmented, display a UIAlertView.

Let another app launch this app.

Any URL starting with http: will cause openURL: to launch Safari. Let’s make any URL starting with alert: launch our Alert app. See Registering Custom URL Schemes.

  1. Edit the Alert-Info.plist file in the Supporting Files folder in XCode’s Project Navigator. Add a new key whose name is CFBundleURLTypes and whose value is an array of dictionaries that announces that another app can launch this one with any URL starting with alert:. The Alert-Info.plist file will contain the following lines. You can see them with TextEdit.app, or by control-clicking on Alert-Info.plist in the Project Navigator and selecting
    Open As → Soure Code. When you are done editing, remove the app from the simulator and run it again.

    	<key>CFBundleURLTypes</key>
    	<array>
    		<dict>
    			<key>CFBundleURLName</key>
    			<string>edu.nyu.scps.Alert</string>
    			<key>CFBundleURLSchemes</key>
    			<array>
    				<string>alert</string>
                            </array>
                    </dict>
            </array>
    

  2. Create a second app. Insert the following code into the application:didFinishLaunchingWithOptions: method of the application delegate of the second app immediately before the return YES;. It uses the application argument passed to application:didFinishLaunchingWithOptions:.
    	NSURL *url = [NSURL URLWithString: @"alert:"];
    
    	if (![application canOpenURL: url]) {
    		NSLog(@"can't open URL \"%@\", url);
    		return YES;
    	}
    
    	NSLog(@"[application openURL: \"%@\"] == %d", url, [application openURL: url]);
    

  3. Build and Run the second app.

  4. The second app can include information in the URL after the colon. For example, the second app can create the URL by saying
    	NSURL *url = [NSURL URLWithString: @"alert:hello"];
    
    To receive this information, insert these statements into the application:didFinishLaunchingWithOptions: method of the Alert app.
    	NSURL *url =
    		[launchOptions objectForKey: UIApplicationLaunchOptionsURLKey];
    	NSString *s = url == nil ? @"" : [url description];
    
    If the Alert app was launched by a second app, the string s in the Alert app will now be the URL that the second app used (in this case @"alert:hello"), including the information after the colon. If the Alert app was launched by a finger tapping on the Alert app’s icon, the string s in the Alert app will be the empty string @"". Launch the Alert app by pressing the Xcode Run button before you launch the Alert app by running the second app.