重点:iOS11以前用SFSafariViewController , ios11及以后用SFAuthenticationSession
SFSafariViewController可以无感获取,SFAuthenticationSession的弹窗授权暂时没有办法避免
if (@available(iOS 11.0, *)) {
SFAuthenticationSession *session = [[SFAuthenticationSession alloc]initWithURL:[NSURL URLWithString:@"https://www.jifenkoudai.com/jifen/index.html#/masterid"] callbackURLScheme:@"xianpaozc" completionHandler:^(NSURL * _Nullable callbackURL, NSError * _Nullable error) {
NSString *scheme = [callbackURL scheme];
//web通过 location.href把cookies值通过scheme传过来
}];
self.session = session;
[self.session start];
} else {
//SFSafariViewController只能在init时候传入url,不可以跳转
self.safariView = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:@"https://www.jifenkoudai.com/jifen/index.html#/masterid"]];
self.safariView.delegate = self;
self.safariView.view.backgroundColor = [UIColor whiteColor];
//不允许隐藏、透明度不得低于0.05、不允许设置view为CGSizeZero,不能移出屏幕外,但是可以放在某个view的底部
self.safariView.view.alpha = 0.05;
self.safariView.view.frame = CGRectMake(0.0, 0.0, 0.5, 0.5);
[self.view insertSubview:self.safariView.view atIndex:0];
//必须手动调用addChildViewController和didMoveToParentViewController
[self addChildViewController:self.safariView];
[self.safariView didMoveToParentViewController:self];
}
SFSafariViewController在代理中,移除控制器
//加载完成之后调用
- (void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully
{
if(didLoadSuccessfully){
[self.safariView willMoveToParentViewController:nil];
[self.safariView.view removeFromSuperview];
[self.safariView removeFromParentViewController];
}
}
appdelegate中回调处理scheme
#pragma mark - 处理浏览器的location.href
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
//web通过 location.href把cookies值通过scheme传过来
NSString *scheme = [url scheme];
}