1

i'm new to swift, i have created a today widget , and i added a button to that. in the Action of that button, i want to launch a url in safari, but "sharedApplication( )" doesn't working, i'm using this code in Normal view controller :

override func viewDidLoad() {
        super.viewDidLoad()
        UIApplication.sharedApplication().openURL(NSURL(string: "facetime:mahdi@gmail.com")!)
        // Do any additional setup after loading the view, typically from a nib.
    }

but its not working in Today App Extension.

  • Please [read the documentation](https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionOverview.html) and [search Stack Overflow](http://stackoverflow.com/questions/26934888/app-and-extension-use-core-data-error-sharedapplication-is-unavailable) before you waste bandwidth by asking a question like this. – matt Jul 02 '15 at 14:55

2 Answers2

1

This is well documented. There is no sharedApplication() in a Today extension; the code isn't running in your app. In this particular situation, you can communicate with the NSExtensionContext instead.

matt
  • 515,959
  • 87
  • 875
  • 1,141
1

You can't use sharedApplication from an app extension.

Today widgets can open urls using the openUrl:completionHandler: method on NSExtensionContext, so you could do:

extensionContext?.openURL(NSURL(string: "facetime:mahdi@gmail.com")!, completionHandler: nil)

You should be doing this in the tap handler for your button though, not in viewDidLoad.

But note that the apple documentation states:

IMPORTANT

Apple allows any Today widget to use the openURL:completionHandler: method to open the widget’s own containing app.

If you employ this method to open other apps from your Today widget, your App Store submission might entail additional review to ensure compliance with the intent of Today widgets.

To learn more, read App Store Review Guidelines and iOS Human Interface Guidelines, linked to from Apple’s App Review Support page

dan
  • 9,695
  • 1
  • 42
  • 40