1

I am writing a Today Widget for an iOS app. The widget has a few action buttons. I want to receive the click event when someone clicks on it. However, it should not launch the app.

I've already tried this but to no avail.

My current implementation is to define a URL Scheme, and call openURL on those button presses like so:

Button 1 links to myApp://button1

Button 2 links to myApp://button2

Button 3 links to myApp://button3

I am receiving these events in the AppDelegate's

application(_:open:options:)

Here's the Code in TodayWodgetController

@IBAction func widgetClicked(sender: UIButton){
    if sender == button1 {
        let u = NSURL(string: "myApp://button1")
        self.extensionContext?.open(u! as URL, completionHandler: nil)            
    }
    ...
}

and here is the code I'm using in the host app's AppDelegate

func application(_ app: UIApplication,
                          open url: URL,
                          options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    
    if url.absoluteString.range(of: "button1") != nil{
        print ("Button 1 Pressed")
    }

    ....

    return true
    
}

However, like I said, it also launches the host App. I want it to just send me the click event without launching the App.

Any help would be appreciated.

Community
  • 1
  • 1
Aldo
  • 707
  • 7
  • 15

1 Answers1

0

I don't think there is a way to do that from an app extension.

Definition of open(_ URL: URL, completionHandler: ((Bool) -> Void)? = nil) as described by Apple:

Asks the system open a URL on behalf of the currently running app extension.

Each extension point determines whether to support this method, or under which conditions to support this method. In iOS 8, only the Today extension point (used for creating widgets) supports this method.

Important: Apple allows a widget to use the open(_:completionHandler:) method to open the widget’s own containing app.

PGDev
  • 23,751
  • 6
  • 34
  • 88