IOS PhoneGap项目调用NATIVE

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

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

①.注册推送

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
//注册推送
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}

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

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

二、调用Navite代码,获取token

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

 

②.MyPlugin.h

#import <Cordova/CDVPlugin.h>

@interface MyPlugin : CDVPlugin

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

@end

  MyPlugin.m

#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]; }

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

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

④.封装一下调用方法

  创建一个通用JS,MyPlugin.js

  

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);
    }
};

⑤.测试一下

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

  然后index.html写

  

<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>

 

使用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

 

转载于:https://www.cnblogs.com/wangdongBlog/p/3682529.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值