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