SFSafariViewController :
A view controller for displaying web content in a Safari-like interface with some of Safari’s features. The web content in SFSafariViewController shares cookie and website data with web content opened in Safari.
示例代码:
#import <SafariServices/SafariServices.h>
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UIButton *openButton; // 进入浏览器按钮
@end
@implementation ViewController
#pragma mark - Getter
- (UIButton *)openButton {
if (!_openButton) {
_openButton = [UIButton buttonWithType:UIButtonTypeSystem];
_openButton.frame = CGRectMake(0, 120, CGRectGetWidth(self.view.frame), 50);
[_openButton setTitle:@"Open In Safari" forState:UIControlStateNormal];
[_openButton addTarget:self action:@selector(openURLInSafari) forControlEvents:UIControlEventTouchUpInside];
}
return _openButton;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.openButton];
}
#pragma mark - Action
- (void)openURLInSafari {
SFSafariViewController *sfSafariVC = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:@"https://www.jianshu.com"] entersReaderIfAvailable:NO];
sfSafariVC.delegate = self;
sfSafariVC.preferredBarTintColor = [UIColor colorWithRed:247 / 255.0 green:247 / 255.0 blue:247 / 255.0 alpha:1.0];
sfSafariVC.preferredControlTintColor = [UIColor colorWithRed:228 / 255.0 green:66 / 255.0 blue:53 / 255.0 alpha:1.0];
[self presentViewController:sfSafariVC animated:YES completion:nil];
}
#pragma mark - Delegate
#pragma mark - SFSafariViewControllerDelegate
- (void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully {
NSLog(@"SafariViewController did complete initial load = %@", (didLoadSuccessfully ? @"YES" : @"NO"));
}
- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
NSLog(@"点击了\"完成\"按钮或侧滑返回, 退出了 SFSafariViewController");
}
效果图:
图一、
进入浏览器按钮
图二、
加载完成
图三、
侧滑返回
网友评论