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.
- 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)
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];
[webBrowserView loadURLString:@"https://www.apple.com"];
- (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;
- Swift edition
- CocoaPods
SCWebBrowserView
is available under the MIT license. See the LICENSE file for more info.
SCWebBrowserView
是一个基于 UIWebView
和 WKWebView
的封装,继承于 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 的处理逻辑)
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];
[webBrowserView loadURLString:@"https://www.apple.com"];
- (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;
- Swift 版本
- CocoaPods
SCWebBrowserView
使用的是 MIT 许可证。 详情见 LICENSE 文件。