我正在尝试从键盘扩展程序打开。我有自定义键盘,我从设置中添加了键盘。在我的自定义键盘上有一个“显示更多”按钮,我想在此按钮上单击打开我的应用程序。
所以我尝试了以下代码:
let context = NSExtensionContext()
context.open(url! as URL, completionHandler: nil)
var responder = self as UIResponder?
while (responder != nil) {
if responder?.responds(to: Selector("openURL:")) == true {
responder?.perform(Selector("openURL:"), with: url)
}
responder = responder!.next
}
它正在成功运行,但正如我们在swift Selector("method_name:")
中所知,已弃用并使用#selector(classname.methodname(_:))
代替它,因此它会发出警告。我想解决这个警告。所以我试过Xcode自动建议:
if responder?.responds(to: #selector(UIApplication.openURL(_:))) == true {
responder?.perform(#selector(UIApplication.openURL(_:)), with: url)
}
也尝试过:
if responder?.responds(to: #selector(NSExtensionContext.open(_:))) == true {
responder?.perform(#selector(NSExtensionContext.open(_:)), with: url)
}
我也尝试过其他可能的方法,但没有运气。如果有人知道怎么做,请告诉我。
我提到了这个链接,Julio Bailon的回答:
答案 0 :(得分:3)
以下代码适用于 Xcode 8.3.3,iOS10,Swift3 ,没有任何编译器警告:
func openUrl(url: URL?) {
let selector = sel_registerName("openURL:")
var responder = self as UIResponder?
while let r = responder, !r.responds(to: selector) {
responder = r.next
}
_ = responder?.perform(selector, with: url)
}
答案 1 :(得分:2)
guard let url = URL(string: UIApplicationOpenSettingsURLString) else { return }
extensionContext?.open(url, completionHandler: { (success) in
if !success {
var responder = self as UIResponder?
while (responder != nil){
let selectorOpenURL = NSSelectorFromString("openURL:")
if responder?.responds(to: selectorOpenURL) == true {
_ = responder?.perform(selectorOpenURL, with: url)
}
responder = responder?.next
}
}
})
答案 2 :(得分:1)
Swift 5.0:
打开托管应用的Info.plist。
转到键盘应用程序:
正确添加以下代码:
@objc func openURL(_ url: URL) {
return
}
func openApp(_ urlstring:String) {
var responder: UIResponder? = self as UIResponder
let selector = #selector(openURL(_:))
while responder != nil {
if responder!.responds(to: selector) && responder != self {
responder!.perform(selector, with: URL(string: urlstring)!)
return
}
responder = responder?.next
}
}
致电:openApp(“您的应用名称://您的捆绑包ID”)