How can i add clickable button to iOS widget?
This is possible, but there are some limitations.
- You can't just execute code, you can only deeplink to your app which then handles the click.
systemSmall
widget is treated as one clickable area (no granular control over multiple touch targets)systemMedium
andsystemLarge
can have more touch targets and useLink
to deeplink to your app with a specific action.
To see an example of how this can be done, you can refer to this post.
User enabled custom Buttons in iOS Widget
As specified in Apple's Human Interface Guidelines:
"Widgets are highly customizable and can contain buttons, text, layout customizations, images, and more."
So yes, it's allowed.
See full guideline:
https://developer.apple.com/ios/human-interface-guidelines/extensions/widgets/
iOS 10 Today Widget Buttons
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.
Interact with item inside widgets iOS14
You can do this using Link in SwiftUI.
Link(destination: url, label: {
// add your UI components in this block on which you want to perform click action.
}
url: Provide a unique string to identify widget action in the main app.
Now In the main app, use below method in AppDelegate.swift file:
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
}
In this method, you can identify the URL that comes from the WidgetKit action.
iOS8 Today Widget - Change title and add stuff to default
Change the CFBundleDisplayName
in your info.plist
file.
I'm not sure what you mean by adding button to the label text. You can't change the colour of this text.
How can I add more than 5 widgets in a widget extension? the maximum number of WidgetBundle's widget cannot exceed 5
I am experiencing same error within WidgetBundle
. If you are interested in having more than 5 widgets for the app, there is a workaround:
@main
struct WidgetKitExtension: WidgetBundle {
@WidgetBundleBuilder
var body: some Widget {
Widget1()
Widget2()
Widget3()
Widget4()
Bundle2().body
}
}
struct Bundle2: WidgetBundle {
@WidgetBundleBuilder
var body: some Widget {
Widget5()
Widget6()
Widget7()
}
}
Basically, you create second WidgetBundle
and use it in your first one. I have verified that this works as of Xcode 12.0.1
Haven't tested submitting the app with more than 5 widgets though.
Related Topics
iOS Today Extension with Core Data
How Is a Swift Cgvector Created with Dx and Dy (Derivative)
Notification in Swift Every Day at a Set Time
How to Go to Specific Native View Controller from React-Native Code
iOS 8 [Uiapplication Sharedapplication].Scheduledlocalnotifications Empty
Center Swiftui View in Top-Level View
Firebase Database Not Equal Request - Alternative Solution (For iOS)
Get All Ranges of a Substring in a String in Swift
iPhone - Memory Leak - Nsdata Datawithcontentsofurl & UIwebview
Customize Google Maps Blue Dot for Current Location
Display: Flex; Doesn't Work on iPad (Both Chrome and Safari)
Google Signin: Unable to Disconnect iOS App
How to Enable/Disable Onetimecode Feature for Specific Text Field
Can Push Notifications Be Used to Run Code Without Notifying User