det*_*man 16 ios ios8 ios-app-extension
我想启动我的包含应用程序.
我尝试使用URL方案.
URL方案从其他地方启动了应用程序 - 所以问题不存在.
看起来这个对象是零:
self.extensionContext
Run Code Online (Sandbox Code Playgroud)
因此我无法运行此方法:
[self.extensionContext openURL:url completionHandler:nil];
Run Code Online (Sandbox Code Playgroud)
我可以启动我的应用吗?URL方案是否可以在自定义键盘中使用?
谢谢!
小智 13
试试这个代码
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]];
}
}
Run Code Online (Sandbox Code Playgroud)
回答我自己的问题:
在iOS8自定义键盘中,extensionContext对象为nil,因此我无法使用它来启动包含应用程序.
我想出的解决方法是:
我不确定苹果是否允许这种情况发生,但它现在有效.
这是键盘扩展的工作解决方案(在iOS 9.2上测试).此类别添加了访问隐藏sharedApplication对象的特殊方法,然后调用openURL:它.(当然,你必须使用openURL:你的应用程序方案的方法.)
// Usage:
// UIInputViewController.openURL(NSURL(string: "your-app-scheme://")!)
extension UIInputViewController {
func openURL(url: NSURL) -> Bool {
do {
let application = try self.sharedApplication()
return application.performSelector("openURL:", withObject: url) != nil
}
catch {
return false
}
}
func sharedApplication() throws -> UIApplication {
var responder: UIResponder? = self
while responder != nil {
if let application = responder as? UIApplication {
return application
}
responder = responder?.nextResponder()
}
throw NSError(domain: "UIInputViewController+sharedApplication.swift", code: 1, userInfo: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
最近我开发了略有不同的方法:
// Usage:
// UIApplication.sharedApplication().openURL(NSURL(string: "your-app-scheme://")!)
extension UIApplication {
public static func sharedApplication() -> UIApplication {
guard UIApplication.respondsToSelector("sharedApplication") else {
fatalError("UIApplication.sharedKeyboardApplication(): `UIApplication` does not respond to selector `sharedApplication`.")
}
guard let unmanagedSharedApplication = UIApplication.performSelector("sharedApplication") else {
fatalError("UIApplication.sharedKeyboardApplication(): `UIApplication.sharedApplication()` returned `nil`.")
}
guard let sharedApplication = unmanagedSharedApplication.takeUnretainedValue() as? UIApplication else {
fatalError("UIApplication.sharedKeyboardApplication(): `UIApplication.sharedApplication()` returned not `UIApplication` instance.")
}
return sharedApplication
}
public func openURL(url: NSURL) -> Bool {
return self.performSelector("openURL:", withObject: url) != nil
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6171 次 |
| 最近记录: |