Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

ios - 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
 }

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)
 }

Also tried :

 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.

I referred this link, Julio Bailon’s answer :

openURL not work in Action Extension

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...