GSWebViewGWebView是集成了两代Web引擎的最好用的网页JS交互框架之一
GWebView是集成了两代Web引擎的最好用的网页JS交互框架之一
GSWebView文档:https://github.com/xorshine/GSWebView
GSWebView下载地址:https://github.com/xorshine/GSWebView.git
如UIWebView一样使用GSWebView,用熟悉的API开发
- GSWebView被设计成UIWebView相同的样式,意在降低开发者的使用难度。
- 引入WebKit与JavaScriptCore库,即刻出发,开始使用GSWebView。
@property (nonatomic, readonly, strong) UIScrollView *scrollView; @property (nonatomic, readonly) BOOL canGoBack; @property (nonatomic, readonly) BOOL canGoForward; - (void)reload; - (void)stopLoading; - (void)goBack; - (void)goForward; - (BOOL)gswebView:(GSWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(GSWebViewNavigationType)navigationType; - (void)gswebViewDidStartLoad:(GSWebView *)webView; - (void)gswebViewDidFinishLoad:(GSWebView *)webView; - (void)gswebView:(GSWebView *)webView didFailLoadWithError:(NSError *)error;
JS交互重点
GSWebView定义了两套协议GSWebViewDelegate和GSWebViewJavaScript,GSWebViewDelegate定义了加载状态,GSWebViewJavaScript则只定义了JS交互。#prama mark - GSWebViewJavaScript
/**
交互协议
*/
@protocol GSWebViewJavaScript <NSObject>
@optional
/**
调用OC方法
- (NSArray<NSString *>*)gswebViewRegisterObjCMethodNameForJavaScriptInteraction
{
return @[@"getCurrentUserId"];
}
//当JS调用一个'- (void)getCurrentUserName:(NSString *)name'的OC方法时,参数name由JS传来,
//那么在实现该OC方法时,只需要正确知道参数类型或基本结构,你也可以写为id类型做普适,在方法内部做转换。
- (void)getCurrentUserId:(NSString *)Id
{
NSLong@(@"JS调用到OC%@",Id);
}
*/
- (NSArray<NSString *>*)gswebViewRegisterObjCMethodNameForJavaScriptInteraction;
服务端JavaScript源码必须的改动
- 改动并非是为了增加复杂度,而是GSWebView内部的WKWebView必须通过Apple.Inc指定的方法
> Adding a scriptMessageHandler adds a function window.webkit.messageHandlers.<name>.postMessage(<messageBody>) for all frames.
举例说明:
JS中有一个getConsultationInfo(id)方法,客户端获取到id实现该方法,这是UIWebView时代
但是在GSWebView中,必须这样:
```javascript
//获取客户端iOS版本
var version = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
version = parseInt(ver[1], 10);
if(version >= 7.0 && version < 8.0){
getConsultationInfo(id);
}else if(version>=8.0){
window.webkit.messageHandlers.getConsultationInfo.postMessage(id)
}
提醒与注意事项
如果之前使用了UIWebView,如今使用GSWebView,在服务端对JS源码做出改动后,必须要考虑客户端老版本的兼容情况。当改动服务端的JS代码,势必导致老版本中的UIWebView交互失效。在下有个建议:当GSWebView加载成功,我们调用服务端预先写好的方法 function shouldUseLatestWebView(isBool);
NSString * shouldUseLatestWebView;
if (IS_IOS_8) {
shouldUseLatestWebView = [NSString stringWithFormat:@"shouldUseLatestWebView('%@')", @"1"];
}else{
shouldUseLatestWebView = [NSString stringWithFormat:@"shouldUseLatestWebView('%@')", @"0"];
}
[self.webview excuteJavaScript:jsGetCurrentUserId completionHandler:^(id _Nonnull params, NSError * _Nonnull error) {
if (error) {
NSLog(@"注入JS方法shouldUseLatestWebView出错:%@",[error localizedDescription]);
}
}];
//一个全局的变量
var isBool = "";
function shouldUseLatestWebView(isBool){
isBool = isBool;
}
if(isBool == "0" || isBool == ""){
getConsultationInfo(id);
}else if(isBool == "1"){
window.webkit.messageHandlers.getConsultationInfo.postMessage(id);
}
评论
