最近个人app一个小需求,在Safari中的Action Extension 需要跳转 Containing App,试过一些接口,要么被废弃,要么被限制,要么无效。花了一俩天寻找与试错,明明很小的需求,却浪费了那么多时间精力,必须记录下:
xcode8.3 iOS8.0~iOS12可行
最后结果:
1、swift版本:
let url = NSURL(string: "webcapture://")
let selectorOpenURL = sel_registerName("openURL:")
let context = NSExtensionContext()
context.open(url! as URL, completionHandler: nil)
var responder = self as UIResponder?
while (responder != nil){
if responder?.responds(to: selectorOpenURL) == true{
responder?.perform(selectorOpenURL, with: url)
}
responder = responder!.next
}
2、OC版本:
NSString *scheml = @"webcapture://";
NSURL *url = [NSURL URLWithString:scheml];
SEL selectorOpenURL = sel_registerName("openURL:");
NSExtensionContext *context = [[NSExtensionContext alloc] init];
[context openURL:url completionHandler:nil];
UIResponder *responder = (UIResponder *)self;
while (responder != nil) {
if ([responder respondsToSelector:selectorOpenURL]) {
[responder performSelector:selectorOpenURL withObject:url];
}
responder = responder.nextResponder;
}