Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 750
VRAwesome Avatar asked Oct 13 '16 11:10

VRAwesome


1 Answers

Swift 5.0:

Open Info.plist of hosting app.

  1. Add Url Types -> Item 0 -> Url Schemes -> Item 0 : "yourappname"
  2. Add Url Types -> Item 0 -> Url Schemes -> URL Identifier: "your bundle id"

Go to Keyboard App:

Add following code properly:

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

Call : openApp ("yourappname://your bundle id")

like image 180
Muge Cevik Avatar answered Sep 28 '22 09:09

Muge Cevik