Tom Coomer Tom Coomer - 11 months ago 192
iOS Question

iOS 10 Today Widget Buttons

In iOS 10 the new Today Widgets also appear when the user 3D Touches the app icon. I would like to add buttons there that launch in to the app and open a particular document (similar to the Apple Music widget). What should I be using to achieve this?

Thank you

Answer Source

Today Extensions have an NSExtensionContext, exposed as property extensionContext. ExtensionContext has an openURL method, which you can pass a private URL to launch your app.

This is a pretty broad question, so providing a broad overview of the technique.

(1) register a custom URL scheme in your main app, e.g. myapp:

Add keys to application plist file, e.g.:


this registers with iOS that your application handles the "myapp" scheme. Use your own name here, and pick one that is highly likely to be unique!

(2) call openURL on your extensionContext of your today extension view controller

[self extensionContext] openURL[NSURL URLWithString:@"myapp://someurl"] completionHandler:nil];

(3) handle the call in the Application Delegate of your app and it's handleOpenURL method

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
   // do something here

Of course this assumes you've placed buttons in your today widget and have the appropriate handlers to consume the button press and pass an appropriate URL to the app.

You've tagged the question with Swift, but these sample snippets are in Obj-C, sorry about that.