在swift 3.0中从我的键盘扩展中打开我的应用程序 [英] Open My application from my keyboard extension in swift 3.0
问题描述
我正在尝试从键盘扩展程序打开。我有自定义键盘,我从设置中添加了键盘。在我的自定义键盘上有一个按钮显示更多,我想在此按钮上单击打开我的应用程序。
I am trying to open from my keyboard extension. I am having custom keyboard and I have add that keyboard from setting. On my custom keyboard there is one button "Show More", and I want to open my app on this button click.
所以我尝试了以下代码:
So I have tried following code :
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自动建议:
It is working successfully, but as we know in swift Selector("method_name:")
is deprecated and use #selector(classname.methodname(_:))
instead so it is giving warning. And I want to solve that warning. So I have tried as Xcode automatically suggested :
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)
}
我也尝试了其他可能的方式,但没有运气。如果有人知道该怎么做,请告诉我。
I have also tried others possible ways, but no luck. If anyone know how to do, please let me know.
我推荐此链接,Julio Bailon的回答:
I referred this link, Julio Bailon’s answer :
推荐答案
以下代码适用于 Xcode 8.3.3,iOS10,Swift3 ,没有任何编译器警告:
Following code works on Xcode 8.3.3, iOS10, Swift3 without any compiler warnings:
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)
}
这篇关于在swift 3.0中从我的键盘扩展中打开我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!