17

On iOS 8 beta 2 it should be possible to use openUrl from app extension as written into the release notes:

enter image description here

however when I try to use this API (on Xcode 6 beta 2) I get the following error:

enter image description here

Beta 2 really fixed this issue or not?

Andrew
  • 15,357
  • 6
  • 66
  • 101
Massimo Piazza
  • 663
  • 3
  • 7
  • 21

2 Answers2

45

you may use this code:

[self.extensionContext openURL:url completionHandler:^(BOOL success) {
        NSLog(@"fun=%s after completion. success=%d", __func__, success);
    }];

the API document: openURL:completionHandler:

you could also refer to this question: openURL not work in Action Extension

Community
  • 1
  • 1
Laurence Fan
  • 1,074
  • 1
  • 11
  • 15
  • @MassimoPiazza Which type Extension do you use when you add openURL sentence? I try it in Action extension but failed. I only success in Today extension. What result do you have? – Laurence Fan Jun 24 '14 at 03:32
  • @MassimoPiazza Got it. I am working on Action extension and openURL can not work. Wait for Apple's response. – Laurence Fan Jun 24 '14 at 16:08
  • 1
    I might add that this code worked inside an IBACtion for a button overlaid over the entire widget. Is there a way to do this without an invisible button? – Jackson Jul 30 '14 at 00:34
  • 3
    Only Today Extensions support openUrl. See this thread: http://stackoverflow.com/questions/24297273/openurl-not-work-in-action-extension – n8tr Aug 07 '14 at 18:28
6

Accepted solution only works in Today extensions, a working solution in Swift 3.1 (tested in iOS10) for other extension-types:

You need to create your own URL Scheme, then add this function to your ViewController and call it with openURL("myScheme://myIdentifier")

//  Function must be named exactly like this so a selector can be found by the compiler!
//  Anyway - it's another selector in another instance that would be "performed" instead.
func openURL(_ url: URL) -> Bool {
    var responder: UIResponder? = self
    while responder != nil {
        if let application = responder as? UIApplication {
            return application.perform(#selector(openURL(_:)), with: url) != nil
        }
        responder = responder?.next
    }
    return false
}
coyer
  • 4,122
  • 3
  • 28
  • 35