来自App Extension的openURL

Mas*_*zza 15 uiapplication openurl ios8 ios-app-extension

在iOS 8 beta 2上,应该可以使用app扩展中的openUrl写入发行说明中:

在此输入图像描述

但是,当我尝试使用此API(在Xcode 6 beta 2上)时,我收到以下错误:

在此输入图像描述

Beta 2是否真的解决了这个问题?

Lau*_*Fan 42

你可以使用这段代码:

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

API文档: openURL:completionHandler:

你也可以参考这个问题: openURL在Action Extension中不起作用

  • Only Today Extensions仅支持openUrl.请参阅此主题:http://stackoverflow.com/questions/24297273/openurl-not-work-in-action-extension (2认同)

coy*_*yer 6

接受的解决方案仅适用于Today extensionsSwift 3.1 中的工作解决方案(在 iOS10 中测试),适用于其他扩展类型:

您需要创建自己的 URL Scheme,然后将此函数添加到您的 ViewController 并使用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
}
Run Code Online (Sandbox Code Playgroud)