Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
885 views
in Technique[技术] by (71.8m points)

objective c - iOS10 SFSafariViewController not working when alpha is set to 0

I'm using SFSafariViewController to grab user's cookie in my app. Here's is my code:

SFSafariViewController *safari = [[SFSafariViewController alloc]initWithURL:[NSURL URLWithString:referrerUrl] entersReaderIfAvailable:NO];
    safari.delegate = self;
    safari.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    safari.view.alpha = 0.0;
    safari.view.hidden = true;
    [self dismissViewControllerAnimated:false completion:nil];
    NSLog(@"[referrerService - StoreViewController] presenting safari VC");
    [self presentViewController:safari animated:false completion:nil];

This works well on iOS 9. but on iOS 10 it seems that the SF controller doesn't work (it also block my current context - which happens to be another UIWebView).

Anyone can suggest of an alternative way to hide a SFSafariViewController?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Updated answer:

Apple prohibits this kind of SafariViewController usage in last version of review guidelines:

SafariViewContoller must be used to visibly present information to users; the controller may not be hidden or obscured by other views or layers. Additionally, an app may not use SafariViewController to track users without their knowledge and consent.

Old answer:

In iOS 10 there are some additional requirements for presented SFSafariViewController:

1) Your view should not be hidden, so hidden should be set to NO

2) The minimum value for alpha is 0.05

3) You need to add controller manually with addChildViewController: / didMoveToParentViewController: (callback's doesn't called otherwise).

4) UIApplication.keyWindow.frame and SFSafariViewController.view.frame should have non-empty intersection (in appropriate coordinate space), that means:

  • safari view size should be greater than CGSizeZero

  • you can't place safari view off the screen

  • but you can hide safari view under your own view

Code example:

self.safariVC = [[SFSafariViewController alloc] initWithURL:referrerUrl];
self.safariVC.delegate = self;
self.safariVC.view.alpha = 0.05;
[self addChildViewController:self.safariVC];
self.safariVC.view.frame = CGRectMake(0.0, 0.0, 0.5, 0.5);
[self.view insertSubview:self.safariVC.view atIndex:0];
[self.safariVC didMoveToParentViewController:self];

Also, don't forget to remove safariVC properly after the end of the usage:

[self.safariVC willMoveToParentViewController:nil];
[self.safariVC.view removeFromSuperview];
[self.safariVC removeFromParentViewController];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...