SFSafariViewController

OverView

2015年, Apple 在 SFSafariViewController 中导入了许多振奋人心的新改变。许多使用者想要在他们的App 中使用完整的Safari 浏览器功能,但是又不希望强制使用者跳离App 去使用Safari 浏览器(这会影响使用者留存率),或者没有时间去建置内建于App 中的浏览器。藉由在 App 中提供等同于 Safari 浏览器的功能而不是提示使用者开启 Safari 浏览器, SFSafariViewController 让许多人看见了曙光。

API文档

简单介绍

一个和系统浏览器safari在外观上基本相同,并且共享cookie的视图控制器

/*!
 @class SFSafariViewController
 A view controller for displaying web content in a Safari-like interface with some of Safari’s features.
 一个和系统浏览器safari在外观上基本相同的视图控制器
 */
SF_EXTERN API_AVAILABLE(ios(9.0))
@interface SFSafariViewController : UIViewController

一些初始化方法

禁用的初始化方法
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithCoder:(NSCoder *)aDecoder NS_UNAVAILABLE;
- (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil NS_UNAVAILABLE;

默认的初始化方法,初始化的URL,只支持 http://或https://模式
在调用` -initWithURL: `之后修改configuration将不会对视图控制器产生任何影响
/*! @abstract Returns a view controller that loads a URL.
    @param URL the initial URL to navigate to. Only supports initial URLs with http:// or https:// schemes.
    @param configuration the configuration for the new view controller.
    @discussion This is a designated initializer. You can use
    @link -initWithURL: @/link to initialize an instance with the default configuration. Mutating the configuration after invoking the initializer has no effect on the view controller.
*/
- (instancetype)initWithURL:(NSURL *)URL configuration:(SFSafariViewControllerConfiguration *)configuration NS_DESIGNATED_INITIALIZER API_AVAILABLE(ios(11.0));

/*! @abstract Returns a view controller that loads a URL.
    @param URL the initial URL to navigate to. Only supports initial URLs with http:// or https:// schemes.
    @param entersReaderIfAvailable indicates if the Safari Reader version of content should be shown automatically
    when Safari Reader is available on a web page.
 */
- (instancetype)initWithURL:(NSURL *)URL entersReaderIfAvailable:(BOOL)entersReaderIfAvailable NS_DESIGNATED_INITIALIZER API_DEPRECATED_WITH_REPLACEMENT("-initWithURL:configuration:", ios(9.0, 11.0));

/*! @abstract Returns a view controller that loads a URL.
    @param URL the initial URL to navigate to. Only supports initial URLs with http:// or https:// schemes.
 */
- (instancetype)initWithURL:(NSURL *)URL;

几个属性

/*! @abstract The view controller's delegate. */
@property (nonatomic, weak, nullable) id <SFSafariViewControllerDelegate> delegate;

/*! @abstract A copy of the configuration with which the view controller was
        initialized.  SFSafariViewController 初始化时的配置信息*/
@property (nonatomic, readonly, copy) SFSafariViewControllerConfiguration *configuration API_AVAILABLE(ios(11.0));

/*! @abstract The preferred color to tint the background of the navigation bar and toolbar. If SFSafariViewController is in Private
    Browsing mode or is displaying an anti-phishing warning page, this color will be ignored. Changes made after the view controller
    has been presented will not be reflected.
 */
 SFSafariViewController 导航栏和底部工具栏背景颜色,在controller presented 之后修改将不会有任何效果
@property (nonatomic, nullable) UIColor *preferredBarTintColor API_AVAILABLE(ios(10.0));

/*! @abstract The preferred color to tint the control buttons on the navigation bar and toolbar. If SFSafariViewController is in Private
    Browsing mode or is displaying an anti-phishing warning page, this color will be ignored. Changes made after the view controller
    has been presented will not be reflected.
 */
 SFSafariViewController 导航栏和底部工具栏上的button颜色,在controller presented 之后修改将不会有任何效果
@property (nonatomic, nullable) UIColor *preferredControlTintColor API_AVAILABLE(ios(10.0));

/*! @abstract The style of dismiss button to use in the navigation bar to close SFSafariViewController.
    The default value is SFSafariViewControllerDismissButtonStyleDone, which makes the button title the localized
    string "Done". You can use other values such as "Close" to provide consistency with your app. "Cancel" is
    ideal when using SFSafariViewController to log in to an external service. All values will show a string localized
    to the user's locale. Changing this property after SFSafariViewController is presented will animate the change.
 */
 左上角关闭按钮的样式
@property (nonatomic) SFSafariViewControllerDismissButtonStyle dismissButtonStyle API_AVAILABLE(ios(11.0));

几个可选代理

未知的神秘力量
/*! @abstract Called when the view controller is about to show UIActivityViewController after the user taps the action button.
    @param URL the URL of the web page.
    @param title the title of the web page.
    @result Returns an array of UIActivity instances that will be appended to UIActivityViewController.
 */
- (NSArray<UIActivity *> *)safariViewController:(SFSafariViewController *)controller activityItemsForURL:(NSURL *)URL title:(nullable NSString *)title;

未知的神秘力量
/*! @abstract Allows you to exclude certain UIActivityTypes from the UIActivityViewController presented when the user taps the action button.
    @discussion Called when the view controller is about to show a UIActivityViewController after the user taps the action button.
    @param URL the URL of the current web page.
    @param title the title of the current web page.
    @result Returns an array of any UIActivityType that you want to be excluded from the UIActivityViewController.
 */
- (NSArray<UIActivityType> *)safariViewController:(SFSafariViewController *)controller excludedActivityTypesForURL:(NSURL *)URL title:(nullable NSString *)title API_AVAILABLE(ios(11.0));

点击 Done 的代理回调,与此同时view controller 将要以 模态的形式消失
/*! @abstract Delegate callback called when the user taps the Done button. Upon this call, the view controller is dismissed modally. */
- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller;

仅仅初始化URL加载完成触发回调,随后其他页面加载将不会被触发
/*! @abstract Invoked when the initial URL load is complete.
    @param didLoadSuccessfully YES if loading completed successfully, NO if loading failed.
    @discussion This method is invoked when SFSafariViewController completes the loading of the URL that you pass
    to its initializer. It is not invoked for any subsequent page loads in the same SFSafariViewController instance.
 */
- (void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully;

在没有用户作用的情况下,加载初始化页面发生重定向时调用
/*! @abstract Called when the browser is redirected to another URL while loading the initial page.
    @param URL The new URL to which the browser was redirected.
    @discussion This method may be called even after -safariViewController:didCompleteInitialLoad: if
    the web page performs additional redirects without user interaction.
 */
- (void)safariViewController:(SFSafariViewController *)controller initialLoadDidRedirectToURL:(NSURL *)URL API_AVAILABLE(ios(11.0));

引用其他类

@interface SFSafariViewControllerConfiguration : NSObject <NSCopying>

/*! @abstract Indicates if SFSafariViewController should automatically show the Reader version of web pages. This will only
    happen when Safari Reader is available on a web page.
 */
 是否进入浏览器的的阅读模式
@property (nonatomic) BOOL entersReaderIfAvailable;

/*! @abstract Indicates if SFSafariViewController should enable collapsing of the navigation bar and hiding of the bottom toolbar
    when the user scrolls web content.
    @discussion The default value is YES.
    指示当用户滚动web内容时SFSafariViewController是否应该启用缩放导航栏和隐藏底部工具栏。
 */
@property (nonatomic) BOOL barCollapsingEnabled;

@end

使用方法

    NSString *urlString = @"https://support.apple.com/zh-cn/HT207428";
    
    SFSafariViewController *sfViewControllr;
    if (@available(iOS 11.0, *)) {
        SFSafariViewControllerConfiguration *config = [SFSafariViewControllerConfiguration new];
        config.entersReaderIfAvailable = YES;
        config.barCollapsingEnabled = YES;
        sfViewControllr = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:urlString] configuration:config];
    } else {
        // Fallback on earlier versions
        sfViewControllr = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:urlString] entersReaderIfAvailable:YES];
    }

    sfViewControllr.preferredBarTintColor = [UIColor redColor];
    sfViewControllr.preferredControlTintColor = [UIColor yellowColor];
    sfViewControllr.dismissButtonStyle = SFSafariViewControllerDismissButtonStyleClose;
    sfViewControllr.delegate = self;
    sfViewControllr.hidesBottomBarWhenPushed = YES;
    
    
    [self presentViewController:sfViewControllr animated:YES completion:^{

        //...

    }];
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值