iOS
Cordova
在git clone
之后直接运行,按照指导进入相关页面。
Installation
引入pod search Cordova
命令来搜索可用的
pod 'Cordova', '~> 4.0.1' # 支持Cordova WebView容器
添加完毕然后使用pod install
命令下载即可。/AppName/config.xml
这种样式。笔者的
<?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.hellocordova" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>iOSBoilerplate</name>
<description>
Cordova Demo in iOS Boilerplate
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Chevalier
</author>
<content src="index.html" />
<preference name="BackupWebStorage" value="local"/>
<plugin name="cordova-plugin-whitelist" version="1" />
<access origin="*" />
<access origin="*.baidu.*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
<allow-intent href="market:*" />
</platform>
<platform name="ios">
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
</platform>
<!--Cordova插件声明-->
<feature name="CordovaPluginsBridge">
<param name="ios-package" value="CordovaPluginsBridge" />
<param name="onload" value="false" />
</feature>
</widget>
Network Configuration
有时候在
<!-- Allow images, xhrs, etc. to google.com --> <access origin="http://google.com" /> <access origin="https://google.com" /> <!-- Access to the subdomain maps.google.com --> <access origin="http://maps.google.com" /> <!-- Access to all the subdomains on google.com --> <access origin="http://*.google.com" /> <!-- Enable requests to content: URLs --> <access origin="content:///*" /> <!-- Don't block any requests --> <access origin="*" />

<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>
<!-- Good default declaration: * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly * Disables use of eval() and inline scripts in order to mitigate risk of XSS vulnerabilities. To change this: * Enable inline JS: add 'unsafe-inline' to default-src * Enable eval(): add 'unsafe-eval' to default-src --> <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com; style-src 'self' 'unsafe-inline'; media-src *"> <!-- Allow everything but only from the same origin and foo.com --> <meta http-equiv="Content-Security-Policy" content="default-src 'self' foo.com"> <!-- This policy allows everything (eg CSS, AJAX, object, frame, media, etc) except that * CSS only from the same origin and inline styles, * scripts only from the same origin and inline styles, and eval() --> <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'"> <!-- Allows XHRs only over HTTPS on the same domain. --> <meta http-equiv="Content-Security-Policy" content="default-src 'self' https:"> <!-- Allow iframe to https://cordova.apache.org/ --> <meta http-equiv="Content-Security-Policy" content="default-src 'self'; frame-src 'self' https://cordova.apache.org">
Plugins
Config
任何一个插件首先需要在
<feature name="CordovaPluginsBridge"> <param name="ios-package" value="Echo" /> <param name="onload" value="true" /> </feature>
有沒有指定插件的初始值設定項。相反,應使用插件
JS Modules
关于
window.echo = function (str, callback) {
cordova.exec(
callback,
function (err) {
callback("Nothing to echo.");
},
"CordovaPluginsBridge",
"echo",
[str]
);
};
调用:
window.echo("echome", function (echoValue) {
alert(echoValue == "echome"); // should alert true.
});
要注意,一般对于$(document).ready()
中。
iOS 本地方法
- (void)myMethod:(CDVInvokedUrlCommand*)command { CDVPluginResult* pluginResult = nil; NSString* myarg = [command.arguments objectAtIndex:0]; if (myarg != nil) { pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; } else { pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Arg was null"]; } [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; }
iOS CDVPluginResult 訊息類型
您可以使用 CDVPluginResult
來返回結果的多種類型回
+ (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAs...
您可以創建 String
,Int
,Double
,Bool
,Array
,Dictionary
,ArrayBuffer
,和 Multipart
類型。你可以也離開了任何參數來發送狀態,或返回錯誤,或甚至選擇不發送任何外掛程式的結果,在這種情況下既不回撥火。
請注意以下複雜的傳回值為:
messageAsArrayBuffer
預計NSData*
並將轉換為ArrayBuffer
在JavaScript 回檔。同樣,任何ArrayBuffer
JavaScript 發送到一個外掛程式都將轉換為NSData*
. messageAsMultipart
預計,NSArray*
包含任何其他支援類型,並將發送整個陣列作為arguments
給您的JavaScript 回檔。這種方式,所有參數在序列化或反序列化作為必要的所以它是能夠安全返回NSData*
作為多部分,但不是Array
/ Dictionary
. 异步执行
如果对于部分执行时间较长的代码,可以放在后台进程中执行。
- (void)myPluginMethod:(CDVInvokedUrlCommand*)command
{
// Check command.arguments here.
[self.commandDelegate runInBackground:^{
NSString* payload = nil;
// Some blocking logic...
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:payload];
// The sendPluginResult method is thread-safe.
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}