In an App Extension (ex: custom keyboard), that would have been handled through UIViewController.extensionContext
but as of iOS 8.1.2, the field is nil
in the following Swift call:
self.extensionContext?.openURL(appURL, completionHandler: nil)
// Does nothing because extensionContext is nil (iOS 8.1)
Actually, it is not possible either to use Application.sharedApplication.openURL(...)
in an App Extension as stated in Apple documentation.
So, as of 8.1.2, the workaround is to use a dumb UIWebView
to redirect to the containing app like this:
let webView = UIWebView(frame: CGRectMake(0, 0, 0, 0));
let url = "MyKeyboard://";
let content = "<head><meta http-equiv='refresh' content='0; URL=\(url)'></head>";
webView.loadHTMLString(content, baseURL: nil);
self.view.addSubview(webView);
let delayInSeconds = 2.0
let startTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delayInSeconds * Double(NSEC_PER_SEC)))
dispatch_after(startTime, dispatch_get_main_queue()) { () -> Void in
webView.removeFromSuperview()
}
Where MyKeyboard://
must be defined accordingly as URL scheme in the containing app.
Please take note that I don't know yet whether this is approved by Apple. Forwarding to apps from custom keyboards may be bad enough for user experience.