2

我看到了一些关于这个问题的帖子,但仍然不明白。

我将 URLidentifier 设置为包含应用程序 - KbrdApp。如果我的包含应用程序启动它会返回标志“1”,这意味着“准备好”。

这是来自调用的自定义键盘扩展的代码viewDidLoad

UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
NSString *customURL = @"KbrdApp://";
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:customURL]];
[webView loadRequest:request];

我已经阅读了NSExtensionContext仅适用于 Today 扩展的内容,所以我尝试了UIWebView. 但它不起作用。

怎么了?

4

2 回答 2

1

问题中提到的方法在 iOS 8.0 中停止工作。

目前(iOS 9.2)您可以使用多种方式访问​​共享UIApplication实例及其隐藏openURL方法。

就个人而言,我更喜欢我开发的这种方法:

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

}
于 2016-04-28T20:47:28.770 回答
1

这是我的 obj c 语言版本。适用于 IOS 11.4.1、XCode 9.4.1

UIApplication 类的类别:

// UIApplication+UpenURL.h file
@interface UIApplication (OpenUrlHelper)

+ (UIApplication *)sharedApp;
- (void)openUrl:(NSURL *)url withOptions:(NSDictionary *)options andComplition:(void(^)(BOOL))complition;

@end

// UIApplication+UpenURL.m file
#import "UIApplication+UpenURL.h"
@implementation UIApplication (OpenUrlHelper)

+ (UIApplication *)sharedApp {
    __weak UIApplication *sharedApp = [UIApplication performSelector:@selector(sharedApplication)];
    return sharedApp;
}

- (void)openUrl:(NSURL *)url withOptions:(NSDictionary *)options andComplition:(void(^)(BOOL))complition {
    if (@available (iOS 10, *)) {
        [self performSelector:@selector(openURL:options:completionHandler:)
                   withObject:url
                   withObject:options
                   withObject:complition];
    } else {
        [self performSelector:@selector(openURL:) withObject:url];
    }
}

- (id)performSelector:(SEL)selector withObject:(id)obj1 withObject:(id)obj2 withObject:(id)obj3 {
    NSMethodSignature *signature = [self methodSignatureForSelector:selector];
    if (!signature)
        return nil;

    NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:signature];
    [invocation setTarget:self];
    [invocation setSelector:selector];
    [invocation setArgument:&obj1 atIndex:2];
    [invocation setArgument:&obj2 atIndex:3];
    [invocation setArgument:&obj3 atIndex:4];
    [invocation invoke];
    if (signature.methodReturnLength) {
        id anObject;
        [invocation getReturnValue:&anObject];
        return anObject;
    }
    return nil;
}

@end

然后在你需要的文件中,你应该

#import "UIApplication+UpenURL.h"

并使用这样的代码:

NSURL *url = [NSURL URLWithString:@"yourapp://"];
[[UIApplication sharedApp] openUrl:url withOptions:@{} andComplition:nil];
于 2018-07-27T12:48:17.937 回答