Skip to content

Instantly share code, notes, and snippets.

@tototti
Last active January 5, 2016 09:14
Show Gist options
  • Save tototti/554d910bb3fa373802c7 to your computer and use it in GitHub Desktop.
Save tototti/554d910bb3fa373802c7 to your computer and use it in GitHub Desktop.
TOTWebView.m
//
// TOTWebView.m
// WebKitResearch
//
// Created by tototti on 2014/12/13.
// Copyright (c) 2014 tototti. All rights reserved.
//
#import "TOTWebView.h"
@interface TOTWebView() {
WKWebView* _wkWebView;
UIWebView* _uiWebView;
}
@end
@implementation TOTWebView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if ([WKWebView class]) {
_wkWebView = [[WKWebView alloc] initWithFrame:frame];
_wkWebView.navigationDelegate = self;
_wkWebView.UIDelegate = self;
[self addSubview:_wkWebView];
} else {
_uiWebView = [[UIWebView alloc] init];
_uiWebView.delegate = self;
[self addSubview:_uiWebView];
}
return self;
}
- (void)setFrame:(CGRect)frame {
[super setFrame:frame];
[_wkWebView setFrame:frame];
[_uiWebView setFrame:frame];
}
- (void)loadRequest:(NSURLRequest *)request {
if (_wkWebView) {
[_wkWebView loadRequest:request];
}
if (_uiWebView) {
[_uiWebView loadRequest:request];
}
}
#pragma mark - UIWebView
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
return [self.delegate shouldStartLoadWithURL:request.URL];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
if (self.delegate != nil) {
[self.delegate didStartLoading];
}
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if (self.delegate != nil) {
[self.delegate didFinishLoading];
}
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
if (self.delegate != nil) {
[self.delegate didFailLoadingWithError:error];
}
}
#pragma mark - WKWebView
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
if (self.delegate != nil) {
[self.delegate didStartLoading];
}
}
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
if (self.delegate != nil) {
[self.delegate didFinishLoading];
}
}
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation
withError:(NSError *)error {
if (self.delegate != nil) {
[self.delegate didFailLoadingWithError:error];
}
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
// カスタムスキームの処理
WKNavigationActionPolicy policy = WKNavigationActionPolicyAllow;
if (self.delegate != nil) {
NSURL *url = navigationAction.request.URL;
if (![self.delegate shouldStartLoadWithURL:url]) {
// スキームによるキャンセル
policy = WKNavigationActionPolicyCancel;
}
}
decisionHandler(policy);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment