简体   繁体   中英

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

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, eg myapp:

Add keys to application plist file, eg:

<key>CFBundleURLTypes</key>
<array>
 <dict>
  <key>CFBundleURLName</key>
  <string>com.mycompany.myapp</string>
  <key>CFBundleURLScheme</key>
  <array>
   <string>myapp</string>
  </array>
 </dict>
</array>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM