launch containing app from iOS8 Custom Keyboard

2020-01-28 02:23发布

I want to launch my containing app.

I tried using URL schemes.

The URL scheme launched the app from other places - so the problem is not there.

Looks like this object is nil:

   self.extensionContext

thus i can't run this method:

[self.extensionContext openURL:url completionHandler:nil];

Can I launch my app? Do URL Schemes work in a custom keyboard?

thanks!

11条回答
我想做一个坏孩纸
2楼-- · 2020-01-28 03:19

Try this code

    UIResponder* responder = self;
    while ((responder = [responder nextResponder]) != nil)
    {
        NSLog(@"responder = %@", responder);
        if([responder respondsToSelector:@selector(openURL:)] == YES)
        {
            [responder performSelector:@selector(openURL:) withObject:[NSURL URLWithString:urlString]];
        }
    }
查看更多
混吃等死
3楼-- · 2020-01-28 03:20

I struggled with this for a couple of days. I could not get the UIWebView working inside the custom keyboard.

Fix: put it inside the viewDidAppear:animated instead of viewDidLoad (on another note this is where the code for the keyboard's custom height should stay as well).

Code:

- (void)viewDidAppear:(BOOL)animated {
    UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSString * content = @"<a href='your_app_schema://your_params'>anchor</a>";
    [webView loadHTMLString:content baseURL:nil];
    [self.view addSubview:webView];
}
查看更多
我想做一个坏孩纸
4楼-- · 2020-01-28 03:23

Apple does not allow any app extensions other than Today extensions to open the containing app.

From the guidelines:

A Today widget (and no other app extension type) can ask the system to open its containing app by calling the openURL:completionHandler: method of the NSExtensionContext class.

You can check Here

查看更多
Bombasti
5楼-- · 2020-01-28 03:25

I tried to do above solutions for latest xcode 8.2 and swift 3.0.

Unfortunately. I can't get it work. So I found my own solution and it works well in swift 3.0, xcode 8.2

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

  // Usage
  //call the method like below
  //self.openApp(urlString)

  //URL string need to included custom scheme.
  //for example, if you created scheme name = customApp
  //urlString will be "customApp://?[name]=[value]"
  //self.openApp("customApp://?category=1")
查看更多
干净又极端
6楼-- · 2020-01-28 03:26

This is what I found to open any URL using what has been described above:

UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
NSString *urlString = @"https://itunes.apple.com/us/app/watuu/id304697459";
NSString * content = [NSString stringWithFormat : @"<head><meta http-equiv='refresh' content='0; URL=%@'></head>", urlString];
[webView loadHTMLString:content baseURL:nil];
[self.view addSubview:webView];
[webView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2.0];

Please note that in this case I am instantiating this call from the UIInputViewController.

This method should also work using the URL scheme from the containing app

NOTE: As of iOS 8.3 Apple has killed this method

查看更多
登录 后发表回答