从iOS8自定义键盘启动包含应用程序

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)


det*_*man 9

回答我自己的问题:

在iOS8自定义键盘中,extensionContext对象为nil,因此我无法使用它来启动包含应用程序.

我想出的解决方法是:

  1. 为您的应用创建网址方案
  2. 在您的inputView中添加UIWebView
  3. 在webview中加载包含应用程序的url方案

我不确定苹果是否允许这种情况发生,但它现在有效.

  • @BastienBeurier看起来它不适用于8.3 beta1 (2认同)

Val*_*gin 8

这是键盘扩展的工作解决方案(在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)


use*_*173 5

Apple 不允许除 Today 扩展之外的任何应用扩展打开包含的应用。

从指南:

Today 小部件(并且没有其他应用程序扩展类型)可以通过调用 NSExtensionContext 类的 openURL:completionHandler: 方法来要求系统打开其包含的应用程序。

你可以在这里查看


归档时间:

查看次数:

6171 次

最近记录:

8 年,5 月 前