/**
write by applzc http://blog.csdn.net/applzc
转载注明出处
**/
cordova-ios跟oc原生代码简单交互
1.材料准备
github下载cordova-ios源码
参考上篇文章mac下cordova-Android自定义插件笔记中的操作
2.新建工程
./create Mytest com.proj.test Mytest
3.Xcode中导入工程
4.配置cordova所需的头文件
Build Settings—>Header Search Paths—>双击
添加:$(SRCROOT)/CordovaLib/Classes
5.新建插件类
头文件:
/**
write by applzc
**/
#import <Foundation/Foundation.h>
#import "CDVPlugin.h"
@interface MyClass : CDVPlugin
- (void)nativeFunction:(CDVInvokedUrlCommand*)command;
@end
实现类:
/**
write by applzc
**/
#import "MyClass.h"
#import "UIDevice+Extensions.h"
@implementation MyClass
- (void) nativeFunction:(CDVInvokedUrlCommand*)command {
CDVPluginResult* pluginResult = nil;
NSString* myarg = [command.arguments objectAtIndex:0];
NSLog(@"command.arguments:%@", command.arguments);
if (![myarg isEqual: @"a"]) {
//获取设备信息
UIDevice *device = [[UIDevice alloc] init];
NSString *name = device.name;
NSString *systemName = device.systemName;
NSString *systemVersion = device.systemVersion;
NSString *identifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
//连接字符串
NSString *string = [[NSString alloc] initWithFormat:@"%@,%@,%@,%@",name, systemName,systemVersion,identifier];
//返回数据给js文件
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:string];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Arg was null"];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
@end
头文件引入报错,检查第4步,并Command+shift+k Clean 再Command+B编译一下看是否报错。
6.新建js文件
/**
write by applzc
**/
$(function(){
init();
});
//插件类
var MyPlugin = {
//函数中的参数需要跟xcode中新建的类一致 类名:"MyClass" 类中方法名:"nativeFunction"
callNativeFunction: function (success, fail, resultType)
{
return Cordova.exec( success, fail,"MyClass","nativeFunction",[resultType]);
}
};
function callNativePlugin( returnSuccess ) {
MyPlugin.callNativeFunction( nativePluginResultHandler, nativePluginErrorHandler, returnSuccess );
}
//插件执行成功回调方法
function nativePluginResultHandler (result) {
var name=result.substring(0,16);
var systemName=result.substring(17,26);
var systemVersion=result.substring(27,30);
var identifier=result.substring(31,67);
$("#MsgPlugin").html("设备名称为:"+name+"</br>")
.append("系统名称:"+systemName+"</br>")
.append("当前系统版本:"+systemVersion+"</br>")
.append("设备ID:"+identifier+"</br>");
}
//插件执行失败回调方法
function nativePluginErrorHandler (error) {
alert("ERROR: \r\n"+error );
}
var init=function(){
console.log("phonegap init!!");
document.addEventListener("deviceready",onDeviceReady,true);
}
var onDeviceReady=function(){
console.log("deviceready event fired");
}
7.新建index.html
<!DOCTYPE html>
<html>
<head>
<title>Device Properties Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" charset="utf-8" src="jquerylib.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8" src="MyPlugin.js"></script>
</head>
<body onload="onBodyLoad()">
<p id="MsgPlugin"></p>
<a href="index.html">返回</a>
<button onclick="callNativePlugin('success');">点击获取设备信息</button>
</body>
</html>
8.配置config.xml
name=“”跟js中exec() 函数中的类名一致,一般跟value值一样, value=“”:工程中的插件类名
<feature name="MyClass">
<param name="ios-package" value="MyClass" />
</feature>
9.编译运行
结束
/**
write by applzc http://blog.csdn.net/applzc
转载注明出处
**/