iOS Add Button to Widget Extension

How can i add clickable button to iOS widget?

This is possible, but there are some limitations.

  1. You can't just execute code, you can only deeplink to your app which then handles the click.
  2. systemSmall widget is treated as one clickable area (no granular control over multiple touch targets)
  3. systemMedium and systemLarge can have more touch targets and use Link 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



Leave a reply



Submit