/SCWebBrowserView

A UIView subclass designed to wrapper UIWebView and WKWebView, using UIWebView on the version prior to iOS 8 and WKWebView on iOS 8 and later. 一个兼容 iOS 8 以下版本的 web 容器,基于 UIWebView 和 WKWebView 的封装。

Primary LanguageObjective-CMIT LicenseMIT

SCWebBrowserView 中文介绍

A UIView subclass designed to wrapper UIWebView and WKWebView, using UIWebView on the version prior to iOS 8 and WKWebView on iOS 8 and later by default.

Feature

  • iOS 7+ support for iPhone and iPad devices
  • Cookie synchronizing for WKWebView
  • Provide muitilpe level of Custom URL Scheme handling
  • Supports subclassing(e.g. custom cookie filter, custom handling logic for Custom URL Scheme)

Usage

1. Create your configuration object if you need some custom configuration.

SCWebBrowserViewConfiguration *configuration = [[SCWebBrowserViewConfiguration alloc] init];
configuration.mediaPlaybackRequiresUserAction = NO;
configuration.allowsInlineMediaPlayback = YES;
configuration.scalesPageToFit = YES;
configuration.webViewType = SCWebBrowserViewTypeUIWebView;

2. Create your web browser view with your custom configuration, or create your web browser view with default configuration by using initializer -initWithFrame:.

SCWebBrowserView *webBrowserView = [[SCWebBrowserView alloc] initWithFrame:self.view.bounds configuration:configuration];
webBrowserView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
webBrowserView.delegate = self;
webBrowserView.allowsBackForwardNavigationGestures = YES;
[self.view addSubview:webBrowserView];

3. Load a URL address.

[webBrowserView loadURLString:@"https://www.apple.com"];

4. Implement SCWebBrowserViewDelegate methods if needed.

- (void)webBrowserViewDidStartLoad:(SCWebBrowserView *)webBrowserView;
- (void)webBrowserViewDidFinishLoad:(SCWebBrowserView *)webBrowserView;
- (void)webBrowserView:(SCWebBrowserView *)webBrowserView didFailLoadWithError:(NSError *)error;
- (BOOL)webBrowserView:(SCWebBrowserView *)webBrowserView shouldStartLoadWithRequest:(NSURLRequest *)request;

- (void)webBrowserView:(SCWebBrowserView *)webBrowserView didUpdateTitle:(nullable NSString *)title;
- (void)webBrowserView:(SCWebBrowserView *)webBrowserView didUpdateProgress:(double)progress;

5. If you need to handle some common business logic for your web view, we really recommend you subclass SCWebBrowserView, and override the needed methods( Example Code ):

- (void)didStartLoad NS_REQUIRES_SUPER;
- (void)didFinishLoad NS_REQUIRES_SUPER;
- (void)didFailLoadWithError:(NSError *)error NS_REQUIRES_SUPER;
- (BOOL)shouldStartLoadWithRequest:(NSURLRequest *)request NS_REQUIRES_SUPER;

- (void)didUpdateTitle:(nullable NSString *)title NS_REQUIRES_SUPER;
- (void)didUpdateProgress:(double)progress NS_REQUIRES_SUPER;

TODO

  • Swift edition
  • CocoaPods

License

SCWebBrowserView is available under the MIT license. See the LICENSE file for more info.


中文介绍

SCWebBrowserView 是一个基于 UIWebViewWKWebView 的封装,继承于 UIView。默认情况下,在 iOS 8.0 及以后,使用 WKWebView,在 iOS 8.0 以前使用 UIWebView

功能

  • 支持 iOS 7 以上的 iOS 设备
  • WKWebView 模式下,自动同步 Shared Cookie Storage 中的 cookie 到 WKWebView
  • 提供不同层次的 Custom URL Scheme 的处理机制
  • 支持一些子类自定义的操作(比如,为 WKWebView 的 cookie 同步自定义 cookie filter, 自定义 Custom URL Scheme 的处理逻辑)

使用

1. 如果你需要一些自定义配置的话,你可以先创建一个 SCWebBrowserViewConfiguration 对象。

SCWebBrowserViewConfiguration *configuration = [[SCWebBrowserViewConfiguration alloc] init];
configuration.mediaPlaybackRequiresUserAction = NO;
configuration.allowsInlineMediaPlayback = YES;
configuration.scalesPageToFit = YES;
configuration.webViewType = SCWebBrowserViewTypeUIWebView;

2. 根据上一步创建的 configuration,创建一个 SCWebBrowserView 对象,或者直接使用 -initWithFrame: 创建一个 SCWebBrowserView 对象(此时,使用的是默认的 configuration)。

SCWebBrowserView *webBrowserView = [[SCWebBrowserView alloc] initWithFrame:self.view.bounds configuration:configuration];
webBrowserView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
webBrowserView.delegate = self;
webBrowserView.allowsBackForwardNavigationGestures = YES;
[self.view addSubview:webBrowserView];

3. 加载一个 URL 地址。

[webBrowserView loadURLString:@"https://www.apple.com"];

4. 如果你需要监听一些事件,可以实现 SCWebBrowserViewDelegate 中的方法。

- (void)webBrowserViewDidStartLoad:(SCWebBrowserView *)webBrowserView;
- (void)webBrowserViewDidFinishLoad:(SCWebBrowserView *)webBrowserView;
- (void)webBrowserView:(SCWebBrowserView *)webBrowserView didFailLoadWithError:(NSError *)error;
- (BOOL)webBrowserView:(SCWebBrowserView *)webBrowserView shouldStartLoadWithRequest:(NSURLRequest *)request;

- (void)webBrowserView:(SCWebBrowserView *)webBrowserView didUpdateTitle:(nullable NSString *)title;
- (void)webBrowserView:(SCWebBrowserView *)webBrowserView didUpdateProgress:(double)progress;

5. 如果你需要针对通用业务逻辑做一些自定义处理,你可以自定义一个 SCWebBrowserView 的子类,然后根据需要重写(override)以下几个方法(示例代码):

- (void)didStartLoad NS_REQUIRES_SUPER;
- (void)didFinishLoad NS_REQUIRES_SUPER;
- (void)didFailLoadWithError:(NSError *)error NS_REQUIRES_SUPER;
- (BOOL)shouldStartLoadWithRequest:(NSURLRequest *)request NS_REQUIRES_SUPER;

- (void)didUpdateTitle:(nullable NSString *)title NS_REQUIRES_SUPER;
- (void)didUpdateProgress:(double)progress NS_REQUIRES_SUPER;

TODO

  • Swift 版本
  • CocoaPods

License

SCWebBrowserView 使用的是 MIT 许可证。 详情见 LICENSE 文件。