XCode11错误“应用程序扩展中不提供open(_:options:completionHandler :)". [英] XCode11 error "open(_:options:completionHandler:) is unavailable in application extensions"

查看:299
本文介绍了XCode11错误“应用程序扩展中不提供open(_:options:completionHandler :)".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上周刚刚发布了Touchgram v1.0,它是99%的iMessage应用程序扩展,并且正在尝试更新为XCode11.

I just launched Touchgram v1.0 last week, which is 99% iMessage app extension, and am trying to update to XCode11.

我现在遇到错误open(_:options:completionHandler:) is unavailable in application extensions

我已经确认即使在普通示例中,也会发生这种情况尝试从iMessage应用程序启动Web URL:

I have confirmed this occurs even in a trivial sample that tries to launch a web URL from an iMessage app:

对于示例:

    let openSel = #selector(UIApplication.open(_:options:completionHandler:))
    while (responder != nil){
        if responder?.responds(to: openSel ) == true {
            // cannot package up multiple args to openSel 
            // so we explicitly call it on the iMessage application instance

            // found by iterating up the chain
            (responder as? UIApplication)?.open(url, completionHandler:handler) 
            return
        }
        responder = responder!.next
    }

也在 Apple开发者论坛上询问.

推荐答案

(唯一)问题,这是DTS确认的iOS 13中的有意更改.我认为这是打击键盘扩展中滥用行为的一部分,该行为是iMessage扩展的副作用.

As documented in the (sole) issue on that sample, this was an intentional change in iOS 13 as confirmed by DTS. My belief is this was part of a crackdown on abusive behaviour in keyboard extensions, which picked up iMessage extensions as a side-effect.

我已经想出了一种解决方法,与他们的建议相同.

I'd already come up with a workaround, which is the same as they recommend.

  1. 使用self.extensionContext?.open
  2. 将URL转发到您的父应用
  3. 拥有父级应用程序,然后在您的外部启动外部应用程序或URL 代表.
  1. Forward the URL to your parent app using self.extensionContext?.open
  2. Have the parent app then launch the external app or URL on your behalf.

这是Touchgram的完整工作扩展

Here's the complete working extension from Touchgram

// UIViewController+iMessageContext.swift
// applied to class MessagesViewController: MSMessagesAppViewController, UrlOpeningInIMessage 

protocol UrlOpeningInIMessage {
    func openFromiMessageContext(url:URL)
}


extension UrlOpeningInIMessage where Self:UIViewController {
    func openFromiMessageContext(url:URL) {
        let handler = { (success:Bool) -> () in
            if success {
                os_log("Finished opening URL", log:tgEnv.logImUI, type:.debug)
            } else {
                os_log("Failed to open URL", log:tgEnv.logImUI, type:.debug)
            }
        }
        // logic same as onLaunchMainPressed, since XCode11 unable to compile extension using UIApplication.open
        // so we pass the URL through to the parent app to launch on our behalf
        let appName = Bundle.appName()
        let encodedUrl = url.dataRepresentation.base64EncodedString()
        guard let appUrl: URL = URL(string: "\(appName)://?url=\(encodedUrl)") else { return }
        // can only open our app, not generalised URLs
        self.extensionContext?.open(appUrl, completionHandler: handler)
    }
}

这篇关于XCode11错误“应用程序扩展中不提供open(_:options:completionHandler :)".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆