我看到一些关于这个问题的帖子,但仍然没有得到它。
我为包含应用程序设置了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
仅适用于今日扩展程序,因此我尝试了UIWebView
。但它不起作用。
有什么问题?
答案 0 :(得分: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
}
}
答案 1 :(得分: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];