Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a specific View Controller from Today Widget?

I'm creating a simple today widget which contains one button, this button should - when pressed - open a specific View Controller in my corresponding application.

The only solution I've thought of was to create a URL type which can only open the initial view controller (at least to my knowledge).

Below is the code I'm currently using inside the UIButton action:

   var url = NSURL(string: "_my_url_://")
    extensionContext?.openURL(url!, completionHandler: nil)
like image 242
Julia Grill Avatar asked Feb 11 '23 12:02

Julia Grill


1 Answers

You can create a URL type for your app (say myapp://) and parse the part after the host name. So for example myapp://signupform or myapp://activities/15. You have the flexibility to make those URLs whatever you want. You can't really tie URL types to specific VCs (automatically), you have to do the work of reading the URL and swapping out VCs in your app delegate.

When you do this you'll then need to parse this URL in your app delegate. You'll get called on your app delegate with the method application:openURL:sourceApplication:annotation: (docs) when your app is opened via a URL, and you can inspect the URL for whatever items you need.

Based on looking at the URL you'll then manually manipulate the nav stack based on which view controller you need to show. So for example you might grab a view controller from your storyboard and add that in, or you might just switch to a given tab in your tab bar controller, or you might back out all the current view controllers to your root screen before you do anything. Unfortunately those actions don't have a universal answer and it all depends on what exactly you're trying to do.

like image 178
Parrots Avatar answered Feb 13 '23 02:02

Parrots