A Safari-like interface with some of Safari's features

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");
}

效果图:

1. 进入按钮
2. 加载完成
3. 侧滑返回
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容