最近個(gè)人app一個(gè)小需求嗅蔬,在Safari中的Action Extension 需要跳轉(zhuǎn) Containing App歌亲,試過一些接口菇用,要么被廢棄,要么被限制陷揪,要么無效惋鸥。花了一倆天尋找與試錯(cuò)悍缠,明明很小的需求卦绣,卻浪費(fèi)了那么多時(shí)間精力,必須記錄下:
xcode8.3 iOS8.0~iOS12可行
最后結(jié)果:
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;
}