iosswift5xcode13

How can i add clickable button to iOS widget?


I am new with XCode and iOS development.

What we need to do: We need to create widget that will have button on it. This button must be clickable and when clicking on it, some action should be triggered. (For our particular case, we need to start recording form device microphone)

What we have done: We have created iOS application, added widget extension and we are able to communicate small string between main app and widget. I have searched on internet, also went trough following question: ios-add-button-to-widget-extension I have also found following topic: How to add Buttons to a Widget but i am struggling to have a clear idea.

How can i add clickable button to iOS widget?

Thanks


Solution

  • September 2023 Update (iOS 17)

    Full interaction functionality is now possible! This answer provides all the details: https://stackoverflow.com/a/77190038/1759443

    This answer works for both Widgets and Live Activities.

    Pre-iOS 17 Answer

    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.