openURL from App Extension

跟風遠走 提交于 2019-11-26 20:56:07

问题


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

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

Beta 2 really fixed this issue or not?


回答1:


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




回答2:


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
}



回答3:


In iOS 11 it seems that you can use UIApplication.sharedApplication.openURL in extensions without the problem.



来源:https://stackoverflow.com/questions/24356314/openurl-from-app-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!