當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
[Cordova]JS和Native交互实现关键代码(iOS)
生活随笔
收集整理的這篇文章主要介紹了
[Cordova]JS和Native交互实现关键代码(iOS)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、JS中定義的exec函數:define("cordova/exec", function(require, exports, module) 。關鍵實現代碼如下:
1.建立command對象,并且將命令推入commandQueuevar command = [callbackId, service, action, actionArgs];commandQueue.push(JSON.stringify(command));
2.建立不可見的iframe,并且在后續會將此iframe添加的頁面function createExecIframe() {? ? var iframe = document.createElement("iframe");? ? iframe.style.display = 'none';? ? document.body.appendChild(iframe);? ? return iframe;}
3.將iframe的鏈接設置為“gap://ready”,此時網頁端會發送一個請求execIframe = execIframe || createExecIframe();execIframe.src = "gap://ready";
二、UIWebView中攔截請求
1.CDVViewController中,實現了webview的代理方法:- (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
2.當網頁端發起iframe中的請求時,請求會被此方法攔截:if ([[url scheme] isEqualToString:@"gap"]) {? ? [_commandQueue fetchCommandsFromJs];? ? return NO;}當發現網頁鏈接是gap協議的,此時通過fetchCommandsFromJs方法獲取命令對象并執行,并且返回NO。實現的效果:既攔截了命令,而且頁面不做變化。
2.fetchCommandsFromJs實現
(1)通過執行js腳本,獲取網頁端js對象commandQueue中的命令(轉換為Json的字符串格式,包含服務名,方法名,以及參數列表)NSString* queuedCommandsJSON = [_viewController.webView stringByEvaluatingJavaScriptFromString:? ?@"cordova.require('cordova/exec').nativeFetchMessages()"];? ?(2)將json字符串解析,并且合成CDVInvokedUrlCommand對象NSArray* commandBatch = [queuedCommandsJSON JSONObject];CDVInvokedUrlCommand* command = [[CDVInvokedUrlCommand alloc] initFromJson:commandBatch];
(3)執行Command對象//通過類名獲取plugin實例CDVPlugin* obj = [_viewController.commandDelegate getCommandInstance:command.className];//通過方法名創建方法對象SEL normalSelector = NSSelectorFromString(methodName);//通過參數字符串列表創建參數列表對象NSMutableArray* arguments = nil;NSMutableDictionary* dict = nil;[command legacyArguments:&arguments andDict:&dict];//發送消息objc_msgSend(obj, legacySelector, arguments, dict);
三、Native代碼回調JS
1.在plugin的功能方法實現中,我們手動調用如下函數向js頁面返回結果[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
2.在如上方法的實現中,實現如下:NSString* js = [NSString stringWithFormat:@"cordova.require('cordova/exec').nativeCallback('%@',%d,%@,%d)", callbackId, status, argumentsAsJSON, keepCallback];[_viewController.webView stringByEvaluatingJavaScriptFromString:js];
四、總結:經上面的步驟,一個完整的交互流程就實現了。
1.建立command對象,并且將命令推入commandQueuevar command = [callbackId, service, action, actionArgs];commandQueue.push(JSON.stringify(command));
2.建立不可見的iframe,并且在后續會將此iframe添加的頁面function createExecIframe() {? ? var iframe = document.createElement("iframe");? ? iframe.style.display = 'none';? ? document.body.appendChild(iframe);? ? return iframe;}
3.將iframe的鏈接設置為“gap://ready”,此時網頁端會發送一個請求execIframe = execIframe || createExecIframe();execIframe.src = "gap://ready";
二、UIWebView中攔截請求
1.CDVViewController中,實現了webview的代理方法:- (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
2.當網頁端發起iframe中的請求時,請求會被此方法攔截:if ([[url scheme] isEqualToString:@"gap"]) {? ? [_commandQueue fetchCommandsFromJs];? ? return NO;}當發現網頁鏈接是gap協議的,此時通過fetchCommandsFromJs方法獲取命令對象并執行,并且返回NO。實現的效果:既攔截了命令,而且頁面不做變化。
2.fetchCommandsFromJs實現
(1)通過執行js腳本,獲取網頁端js對象commandQueue中的命令(轉換為Json的字符串格式,包含服務名,方法名,以及參數列表)NSString* queuedCommandsJSON = [_viewController.webView stringByEvaluatingJavaScriptFromString:? ?@"cordova.require('cordova/exec').nativeFetchMessages()"];? ?(2)將json字符串解析,并且合成CDVInvokedUrlCommand對象NSArray* commandBatch = [queuedCommandsJSON JSONObject];CDVInvokedUrlCommand* command = [[CDVInvokedUrlCommand alloc] initFromJson:commandBatch];
(3)執行Command對象//通過類名獲取plugin實例CDVPlugin* obj = [_viewController.commandDelegate getCommandInstance:command.className];//通過方法名創建方法對象SEL normalSelector = NSSelectorFromString(methodName);//通過參數字符串列表創建參數列表對象NSMutableArray* arguments = nil;NSMutableDictionary* dict = nil;[command legacyArguments:&arguments andDict:&dict];//發送消息objc_msgSend(obj, legacySelector, arguments, dict);
三、Native代碼回調JS
1.在plugin的功能方法實現中,我們手動調用如下函數向js頁面返回結果[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
2.在如上方法的實現中,實現如下:NSString* js = [NSString stringWithFormat:@"cordova.require('cordova/exec').nativeCallback('%@',%d,%@,%d)", callbackId, status, argumentsAsJSON, keepCallback];[_viewController.webView stringByEvaluatingJavaScriptFromString:js];
四、總結:經上面的步驟,一個完整的交互流程就實現了。
總結
以上是生活随笔為你收集整理的[Cordova]JS和Native交互实现关键代码(iOS)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 重构,体现一个工程师的基本素养和底蕴(细
- 下一篇: javascript中的this讲解