IOS PhoneGap项目调用NATIVE

1.今天项目需要为PhoneGap项目,添加推送功能,网上搜了一大片,折腾了一会,总算折腾出来了,记录一下

分析一下思路,1,需要获得deviceToken,2,js获取TOKEN

①.注册推送

IOS PhoneGap项目调用NATIVE
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
//注册推送
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
IOS PhoneGap项目调用NATIVE

②.获得token,并且放入UserDefault中

1
2
3
4
5
6
7
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{<br>  //得到token {xxxxx-xxxx-xxx}
   NSString *newToken = [deviceToken description];<br>  //截取中间部分
   NSString *token =  [newToken substringWithRange:NSMakeRange(1, newToken.length-2)];<br>  //去掉空格
   NSString *temp =  [token stringByReplacingOccurrencesOfString:@" " withString:@""];<br>  //持久化token
   [[NSUserDefaults standardUserDefaults] setObject:temp forKey:@"token"];
}

二、调用Navite代码,获取token

 ①.创建插件,可以再plugins文件夹下创建

 

IOS PhoneGap项目调用NATIVE

②.MyPlugin.h

IOS PhoneGap项目调用NATIVE
#import <Cordova/CDVPlugin.h>

@interface MyPlugin : CDVPlugin

- (void) getToken:(CDVInvokedUrlCommand*) command;

@end
IOS PhoneGap项目调用NATIVE

  MyPlugin.m

IOS PhoneGap项目调用NATIVE
#import "MyPlugin.h"

@implementation MyPlugin

- (void) getToken:(CDVInvokedUrlCommand*) command{
    CDVPluginResult* pluginResult = nil;
//    NSString* echo = [command.arguments objectAtIndex:0];

//    if (echo != nil && [echo length] > 0) {
//        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];
//    } else {
//        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
//    }
    
  //从UserDefault中取的token,并且封装成结果返回
  //如果是别的逻辑处理,记得吧结果封装就可以了
pluginResult
= [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[[NSUserDefaults standardUserDefaults] objectForKey:@"token"]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; }
IOS PhoneGap项目调用NATIVE

③.在config.xml中配置我们的插件

IOS PhoneGap项目调用NATIVE
    <!-- Deprecated plugins element. REmove in 3.0 -->
    <plugins>
     //name为JS调用时的名字 <feature name="myPlugin">
        //name不能改变 value为我们刚才创建的插件名字 <param name="ios-package" value="MyPlugin" /> </feature> </plugins>
IOS PhoneGap项目调用NATIVE

④.封装一下调用方法

  创建一个通用JS,MyPlugin.js

  

IOS PhoneGap项目调用NATIVE
var MyIOSPlugin = {
    /**
     * 调用IOS方法
     * @param method 要调用IOS插件的方法名
     * @param parameter 参数[数组]
     * @param success 成功回调
     * @param fail 失败回调
     * @returns {*}
     */
    nativeFunction: function(method, parameter, success, fail) {
        return Cordova.exec(success, fail, "myPlugin", method, parameter);
    }
};
IOS PhoneGap项目调用NATIVE

⑤.测试一下

  在index.html页面中引入刚才的MyPlugin.js

  然后index.html写

  

IOS PhoneGap项目调用NATIVE
<script>
//    //等待加载PhoneGap
    document.addEventListener("deviceready", onDeviceReady, false);
    // PhoneGap加载完成
  //调用native,必须是在设备准备好之后才能调用
function onDeviceReady() { var echo = MyIOSPlugin.nativeFunction("getToken",[‘‘], function(result) { }, function(error) { } );
     //echo就是我们通过调用navite获取我们刚才得到的设备ID window.device
=echo; } </script>
IOS PhoneGap项目调用NATIVE

 

使用phoneGap打包时候,target=7.0一下的时候遇到了问题,这样解决

Property edgesForExtendedLayout not found on object of type LAViewController *
Use of undeclared identifier UIRectEdgeNone

 

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000

#define IOS7_SDK_AVAILABLE 1

#endif

    

#ifdef IOS7_SDK_AVAILABLE

  //把这段代码包起来,如果系统是ios7则执行这个方法

    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {

        self.edgesForExtendedLayout = UIRectEdgeNone;

    }

#endif

 

IOS PhoneGap项目调用NATIVE,布布扣,bubuko.com

IOS PhoneGap项目调用NATIVE

上一篇:Android ListView 与 adapter的使用


下一篇:Android 动画配置文件