iOS 10 Today Widget Buttons

Tom Coomer

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

CSmith

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.:

<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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Today widget in iOS 10 is not expanding

Today Widget Extension Height - iOS10

iOS 10 Open Today Widget from main App in Swift

Today widget in iOS10 height issue in swift

Hide "Show More" button from Today widget in iOS10

How to set dark background for iOS10 today widget

iOS Today Widget - Determine if active

iOS 8 today widget - UIAlertView

iOS 10 Today View Widget: How to tell if it's *actually* on screen / visible?

Swift - iOS10 - CoreData - FetchRequest in Today Widget always return 0 elements

Core Data iOS 8 Today Widget issue

iOS in-app-purchase + Today Extension widget

iOS Today Extension / widget -- location permissions?

iOS 8 Today widget alignment issue

iOS - create a today widget extension programmatically

Send message from iOS Today Widget to app

How to expand iOS Today Widget by default?

Today Widget has no content on iOS 8 device

How to rename an iOS 8 Today Widget?

What is the purpose of widgetPerformUpdateWithCompletionHandler in iOS 8 Today Widget?

iOS 8 Today Widget - SLComposeViewController not displaying properly

how to localize iOS 8 today extensions widget and also the widget name

smartface update and clear buttons in iOS widget disappeared

Today Extension compact mode height in iOS 10

iOS8 Today Widget - Change title and add stuff to default

iOS 8 Today Widget shows up blank for a few seconds

iOS8 today view widget not showing UITableViewController

iOS (Today) Widget - launch app by tapping on app icon

iOS 8 Beta Today extension widget not showing in a Swift app?