The current project: A complete rewrite of the good old CompareMe app in Apples new programming language Swift. So, there are some insights & code snippets of a Swift ActionSheet example I want to share with the interested iOS developer community.

Today, let’s create some action sheets to open websites, twitter profiles in the native twitter app on the iOS device or create a eMail on the device. The Swift code is tested on Xcode 6.1 GM with iOS 8.1 on OS X Yosemite 10.10 GM.

@IBAction func testButtonPressed(sender: AnyObject) {
        
        var alert = UIAlertController(title: "Open external link?", message: "Webpage in Safari, profile in Twitter app or compose a new eMail.", preferredStyle: UIAlertControllerStyle.ActionSheet)

        alert.addAction(UIAlertAction(title: "Open http://www.codedifferent.com",
            style: UIAlertActionStyle.Default, handler: { (action:UIAlertAction!) -> Void in
                UIApplication.sharedApplication().openURL(NSURL(string: "http://www.codedifferent.com")!)
                return
        }))

        alert.addAction(UIAlertAction(title: "Open @codedifferent in Twitter app",
            style: UIAlertActionStyle.Default, handler: { (action:UIAlertAction!) -> Void in
                UIApplication.sharedApplication().openURL(NSURL(string: "twitter://user?screen_name=codedifferent")!)
                return
        }))

        alert.addAction(UIAlertAction(title: "Create eMail",
            style: UIAlertActionStyle.Default, handler: { (action:UIAlertAction!) -> Void in
                UIApplication.sharedApplication().openURL(NSURL(string: "mailto:your@emailadress.com")!)
                return
        }))

        alert.addAction(UIAlertAction(title: "Cancel",
            style: UIAlertActionStyle.Cancel, handler: nil))
        
        if let popoverController = alert.popoverPresentationController {
            popoverController.sourceView = sender as UIView
            popoverController.sourceRect = sender.bounds
        }

        self.presentViewController(alert, animated: true, completion: nil)
}

If you present your alert as a UIAlertControllerStyle.ActionSheet, then be sure to include the popoverController.sourceView & popoverController.sourceRect. Otherwise your app will terminate on a big screen device like the iPad.On those devices, the ActionSheet is not displayed as a modal view, which covers the whole screen like on iPhone devices. Here the ActionSheet is presented as a popup besides the element, which created the ActionSheet.

Compose a new email with passing a “mailto:…@…” url is the easiest way to accomplish that goal. But be aware, that the user is switching to the Mail program to compose that mail. If the user sends or cancels the form, the user will not be directed back to your application. So if you want the user to stay in your app, or if you want to define more parameters, like the subject or body of the email, then you have to use MFMailComposeViewController … But that’s an other post 😉

Pin It on Pinterest

Shares
Share This