Skip to content

Commit

Permalink
WebView跳转支持
Browse files Browse the repository at this point in the history
  • Loading branch information
fengchuanxiang committed Sep 12, 2017
1 parent 84790fb commit bffa689
Show file tree
Hide file tree
Showing 11 changed files with 819 additions and 56 deletions.
49 changes: 49 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# .gitignore_global
####################################
######## OS generated files ########
####################################
.DS_Store
.DS_Store?
*.swp
._*
.Spotlight-V100
.Trashes
Icon?
ehthumbs.db
Thumbs.db
*.xcuserstate
project.xcworkspace
xcuserdata
UserInterfaceState.xcuserstate
project.xcworkspace/
xcuserdata/
UserInterface.xcuserstate
####################################
############# Packages #############
####################################
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

## Build generated
build/
DerivedData/

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3

## Other
*.moved-aside

51 changes: 51 additions & 0 deletions FCXUniversial/FCXDiscover/FCXWebView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// FCXWebView.h
// SKWeb
//
// Created by fcx on 2017/9/12.
// Copyright © 2017年 冯 传祥. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface FCXWebView : UIWebView

@property (nonatomic, copy) NSString *urlString;

/**
* 注册JS回调
*
* @param handlerName 事件名
* @param handler 相应回调Block
*/
- (void)registerHandler:(NSString*)handlerName handler:(id)handler;

/**
* 注册JS回调
*
* @param handlerName 事件名
* @param JSONHandler 返回JSON格式的回调
*/
- (void)registerHandler:(NSString*)handlerName JSONHandler:(void (^)(id obj))JSONHandler;

/**
* Native调用H5
*
* @param handlerName 函数名
* @param data 传给H5的数据
*/
- (void)callHandler:(NSString*)handlerName data:(id)data;

/**
* 设置WebView的代理
*
* @param webViewDelegate 代理
*
* @note 之前的delegate不能使用
*/
- (void)setWebViewDelegate:(id<UIWebViewDelegate>)webViewDelegate;

- (void)loadURL:(NSURL *)url;
- (void)loadURLString:(NSString *)urlString;

@end
139 changes: 139 additions & 0 deletions FCXUniversial/FCXDiscover/FCXWebView.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
//
// FCXWebView.m
// SKWeb
//
// Created by fcx on 2017/9/12.
// Copyright © 2017年 冯 传祥. All rights reserved.
//

#import "FCXWebView.h"
#import <JavaScriptCore/JavaScriptCore.h>

@interface FCXWebView () <UIWebViewDelegate>
{
NSMutableDictionary *_messageHandlersDict;
__weak id _webViewDelegate;
JSContext *_context;
}

@end

@implementation FCXWebView

- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setup];
}
return self;
}

- (void)setup {
_messageHandlersDict = [[NSMutableDictionary alloc] init];
self.delegate = self;
self.scrollView.bounces = NO;//防止弹框出现后,滑动弹框界面也会跟着滑动
}

- (void)setFrame:(CGRect)frame {
[super setFrame:frame];
}

- (void)setWebViewDelegate:(id<UIWebViewDelegate>)webViewDelegate {
_webViewDelegate = webViewDelegate;
}

- (void)registerHandler:(NSString *)handlerName handler:(id)handler {
if (!handlerName || !handler) {
return;
}
[_messageHandlersDict setObject:[handler copy] forKey:handlerName];
}

- (void)registerHandler:(NSString *)handlerName JSONHandler:(void (^)(id))JSONHandler {
[self registerHandler:handlerName handler:JSONHandler];
}

- (void)callHandler:(NSString *)handlerName data:(id)data {
if (!handlerName) {
return;
}
JSValue *func = [_context evaluateScript:handlerName];
if (data) {
[func callWithArguments:@[data]];
} else {
[func callWithArguments:@[]];
}
}

- (void)loadRequest:(NSURLRequest *)request {
[self clearCache];
[super loadRequest:request];
}

- (void)loadURL:(NSURL *)url {
if (!url || ![url isKindOfClass:[NSURL class]]) {
return;
}
[self loadRequest:[NSURLRequest requestWithURL:url]];
}

- (void)loadURLString:(NSString *)urlString {
if (!urlString || ![urlString isKindOfClass:[NSString class]]) {
return;
}
self.urlString = urlString;
[self loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}

#pragma mark - UIWebViewDelegate

- (void)webViewDidFinishLoad:(UIWebView *)webView {
//禁止复制、粘贴
[webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitUserSelect='none';"];
[webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none';"];

_context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
for (NSString *key in _messageHandlersDict.allKeys) {
_context[key] = _messageHandlersDict[key];
}

// 异常的回调处理
_context.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
context.exception = exceptionValue;
NSLog(@"异常信息:%@", exceptionValue);
};

if (_webViewDelegate && [_webViewDelegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
[_webViewDelegate webViewDidFinishLoad:webView];
}
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
if (_webViewDelegate && [_webViewDelegate respondsToSelector:@selector(webView:didFailLoadWithError:)]) {
[_webViewDelegate webView:webView didFailLoadWithError:error];
}
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (_webViewDelegate && [_webViewDelegate respondsToSelector:@selector(webView:shouldStartLoadWithRequest:navigationType:)]) {
return [_webViewDelegate webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];
}
return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
if (_webViewDelegate && [_webViewDelegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
[_webViewDelegate webViewDidStartLoad:webView];
}
}

// 清除缓存
- (void)clearCache {
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}

- (void)dealloc {
self.delegate = nil;
[self stopLoading];
}

@end
31 changes: 31 additions & 0 deletions FCXUniversial/FCXDiscover/FCXWebViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,45 @@
//

#import <UIKit/UIKit.h>
#import "FCXWebView.h"

@interface FCXWebViewController : UIViewController

@property (nonatomic, strong, readonly) FCXWebView *webView;
@property (nonatomic, copy) NSString *urlString;
@property (nonatomic, copy) NSString *admobID;
@property (nonatomic, strong) UIColor *navBackColor;//!<导航条返回按钮颜色(没有默认取导航条的tinColor,再取不
@property (nonatomic, strong) UIImage *backImage;
@property (nonatomic, strong) UIImage *backImageHighlighted;
@property (nonatomic, strong) UIColor *progressColor;
@property (nonatomic, unsafe_unretained) BOOL hideNavRightItem;//!<隐藏导航条右边默认分享按钮

- (void)loadRequest:(NSURLRequest *)request;
- (void)loadURL:(NSURL *)url;
- (void)loadURLString:(NSString *)urlString;

/**
* 注册JS回调
*
* @param handlerName 事件名
* @param handler 相应回调Block
*/
- (void)registerHandler:(NSString*)handlerName handler:(id)handler;

/**
* 注册JS回调
*
* @param handlerName 事件名
* @param JSONHandler 返回JSON格式的回调
*/
- (void)registerHandler:(NSString*)handlerName JSONHandler:(void (^)(id obj))JSONHandler;

/**
* Native调用H5
*
* @param handlerName 函数名
* @param data 传给H5的数据
*/
- (void)callHandler:(NSString*)handlerName data:(id)data;

@end
Loading

0 comments on commit bffa689

Please sign in to comment.