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