1
votes

Goal

From Today Extension (Widget) open app and perform different actions according to button tap.

  • target: iOS 10

Posts & articles readed


Already done

Create URLScheme

Target/MyApp/Info/URLTypes

  • URLType identifier = com.myCompany.MyAppName
  • URLType URLSchemes = MyAppName
  • URLType role = Viewer

Open host application from Widget

TodayViewController (Widget)

@IBAction func addButtonPressed(_ sender: UIButton) {
  let url = URL(string: "MyAppName://")!

  extensionContext?.open(url) { isSuccess in
    switch isSuccess {
    case true: print("Open URL Success") // It print when opening app
    case false: print("Open URL Failed")
    }
  }
}

The button works well when tapped. The Host application is launched.


Delegate func application(_:open:options:) not called

AppDelegate (Host Application)

// This delegate function is never called when the host application launches
func application(_ app: UIApplication, // breakpoint doesn't trigger
               open url: URL,
               options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { 

  print("FUNCTION CALLED ????????????") // Never print

  return true
}

This delegate function is never called when the host Application opens from button tap in the Widget. How can my app run some code when the application is opened from the widget. I tried using some other methods but they are deprecated.

I continue searching and it looks like that no AppDelegate function can be call when the widget open the host application because all the other methods like applicationWillEnterForeground is not called when the application lauches.

I tried on a real device but it's still not working.

1

1 Answers

2
votes

Goto your app target then info -> URL Types -> add a URL Scheme

in your TodayViewController paste the following code

self.extensionContext?.open(URL(string: "Your URL Scheme identifier")!, completionHandler: nil)