We all have experienced this in one way or another - you have an embedded browser in some app, but you inevitably found yourself wanting to switch to Safari with 'View in Safari' button. Why? Simply because user experienced in native Safari is better due to local storage and cookies.
Yes, starting from iOS8 we can all enjoy Safari-like browsing speed in apps, thanks to WKWebView. The problem was though, that each app had its own browser interface, which resulted in inconsistent look and feel across the system.
But now these problems are gone, since Apple's release of SFSafariViewController.
First things first: import
Afterwards we are instantiating
- Creating with only base URL
- Creating with bas URL as well as entering 'Reading Mode' in case it is available
NSString *sURL = @"http://google.com"; NSURL *URL = [NSURL URLWithString:sURL]; SFSafariViewController *safari = [[SFSafariViewController alloc] initWithURL:URL]; // 1. SFSafariViewController *safari = [[SFSafariViewController alloc] initWithURL:URL entersReaderIfAvailable:YES]; // 2. [self presentViewController:safari animated:YES completion:nil];
It is worth mentioning though, that
There is nothing cool happening in delegate protocol:
This method is invoked when SFSafariViewController completes the loading of the URL that you pass to its initializer. The method is not invoked for any subsequent page loads in the same SFSafariViewController instance.
The view controller calls this method when the view is about to show an activity view controller. Your delegate can provide unique application-specific services (such as a social media service) to be included with the system-provided sharing services.
Your method should dismiss the view controller.
This sums it up, basically. Not too much to control here, but definitely a nice solution for apps that don't require custom browser but do want to keep user from switching from their app to Safari and forget from where they have came from in the first place.