iOS优化代码

1.优化、简化了 AppDelegate+JPush.m 和 JPushPlugin.m 中的代码。
2.整理了JPushPlugin.h中的方法与注释。
This commit is contained in:
E.B 2016-03-08 16:46:12 +08:00
parent 5c5d7c7848
commit afe2230ed9
4 changed files with 293 additions and 419 deletions

View File

@ -10,6 +10,4 @@
@interface AppDelegate (JPush) @interface AppDelegate (JPush)
//@property(nonatomic,strong)NSDictionary *luanchOption;
@end @end

View File

@ -7,87 +7,42 @@
// //
#import "AppDelegate+JPush.h" #import "AppDelegate+JPush.h"
#import <objc/runtime.h>
#import "JPushPlugin.h" #import "JPushPlugin.h"
#import "JPUSHService.h" #import "JPUSHService.h"
//static char launchNotificationKey;
@implementation AppDelegate (JPush) @implementation AppDelegate (JPush)
+(void)load{ -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[JPushPlugin setLaunchOptions:launchOptions];
Method origin; return [super application:application didFinishLaunchingWithOptions:launchOptions];
Method swizzle;
origin=class_getInstanceMethod([self class],@selector(init));
swizzle=class_getInstanceMethod([self class], @selector(init_plus));
method_exchangeImplementations(origin, swizzle);
} }
-(instancetype)init_plus{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidLaunch:)
name:@"UIApplicationDidFinishLaunchingNotification"
object:nil];
return [self init_plus];
}
-(void)applicationDidLaunch:(NSNotification *)notification{
if (notification) {
[JPushPlugin setLaunchOptions:notification.userInfo];
}
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[JPUSHService registerDeviceToken:deviceToken]; [JPUSHService registerDeviceToken:deviceToken];
} }
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
[JPUSHService handleRemoteNotification:userInfo]; [JPUSHService handleRemoteNotification:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:kJPushPluginReceiveNotification [[NSNotificationCenter defaultCenter] postNotificationName:kJPushPluginReceiveNotification object:userInfo];
object:userInfo];
} }
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
[JPUSHService handleRemoteNotification:userInfo]; [JPUSHService handleRemoteNotification:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:kJPushPluginReceiveNotification [[NSNotificationCenter defaultCenter] postNotificationName:kJPushPluginReceiveNotification object:userInfo];
object:userInfo];
completionHandler(UIBackgroundFetchResultNewData); completionHandler(UIBackgroundFetchResultNewData);
} }
- (void)application:(UIApplication *)application - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
didReceiveLocalNotification:(UILocalNotification *)notification {
[JPUSHService showLocalNotificationAtFront:notification identifierKey:nil]; [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];
} }
- (void)applicationWillEnterForeground:(UIApplication *)application { - (void)applicationWillEnterForeground:(UIApplication *)application {
// [application setApplicationIconBadgeNumber:0]; // [application setApplicationIconBadgeNumber:0];
// [application cancelAllLocalNotifications]; // [application cancelAllLocalNotifications];
} }
- (void)applicationDidEnterBackground:(UIApplication *)application { - (void)applicationDidEnterBackground:(UIApplication *)application {
// [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; // [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
} }
//delegate
//-(NSDictionary *)luanchOption{
// return objc_getAssociatedObject(self, &launchNotificationKey);
//}
//-(void)setLuanchOption:(NSDictionary *)luanchOption{
// objc_setAssociatedObject(self, &launchNotificationKey, luanchOption, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
//}
//-(void)dealloc{
// self.luanchOption=nil;
//}
@end @end

View File

@ -15,24 +15,46 @@
} }
+(void)setLaunchOptions:(NSDictionary *)theLaunchOptions; +(void)setLaunchOptions:(NSDictionary *)theLaunchOptions;
//以下为js中可调用接口
//设置标签、别名
-(void)setTagsWithAlias:(CDVInvokedUrlCommand*)command; -(void)setTagsWithAlias:(CDVInvokedUrlCommand*)command;
-(void)setTags:(CDVInvokedUrlCommand*)command; -(void)setTags:(CDVInvokedUrlCommand*)command;
-(void)setAlias:(CDVInvokedUrlCommand*)command; -(void)setAlias:(CDVInvokedUrlCommand*)command;
//获取 RegistrationID
-(void)getRegistrationID:(CDVInvokedUrlCommand*)command; -(void)getRegistrationID:(CDVInvokedUrlCommand*)command;
//页面统计
-(void)startLogPageView:(CDVInvokedUrlCommand*)command; -(void)startLogPageView:(CDVInvokedUrlCommand*)command;
-(void)stopLogPageView:(CDVInvokedUrlCommand*)command; -(void)stopLogPageView:(CDVInvokedUrlCommand*)command;
-(void)beginLogPageView:(CDVInvokedUrlCommand*)command;
// 设置角标到服务器, 服务器下一次发消息时,会设置成这个值 //设置角标到服务器,服务器下一次发消息时,会设置成这个值
//本接口不会改变应用本地的角标值. //本接口不会改变应用本地的角标值.
-(void)setBadge:(CDVInvokedUrlCommand*)command; -(void)setBadge:(CDVInvokedUrlCommand*)command;
//相当于 [setBadge:0] //相当于 [setBadge:0]
-(void)resetBadge:(CDVInvokedUrlCommand*)command; -(void)resetBadge:(CDVInvokedUrlCommand*)command;
//应用本地的角标值设置/获取
//改变应用本地的角标值.
-(void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand*)command; -(void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand*)command;
//获取应用本地的角标值.
-(void)getApplicationIconBadgeNumber:(CDVInvokedUrlCommand*)command; -(void)getApplicationIconBadgeNumber:(CDVInvokedUrlCommand*)command;
//停止与恢复推送
-(void)stopPush:(CDVInvokedUrlCommand*)command;
-(void)resumePush:(CDVInvokedUrlCommand*)command;
-(void)isPushStopped:(CDVInvokedUrlCommand*)command;
//开关日志
-(void)setDebugModeFromIos:(CDVInvokedUrlCommand*)command;
-(void)setLogOFF:(CDVInvokedUrlCommand*)command;
/*
* js中可监听到的事件
* jpush.openNotification app
* jpush.setTagsWithAlias
* jpush.receiveMessage
* jpush.receiveNotification
*/
@end @end

View File

@ -10,69 +10,24 @@
#import "JPUSHService.h" #import "JPUSHService.h"
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
static NSString *const JM_APP_KEY = @"APP_KEY"; static NSString *const JM_APP_KEY = @"APP_KEY";
static NSString *const JM_APP_CHANNEL = @"CHANNEL"; static NSString *const JM_APP_CHANNEL = @"CHANNEL";
static NSString *const JM_APP_ISPRODUCTION = @"IsProduction"; static NSString *const JM_APP_ISPRODUCTION = @"IsProduction";
static NSString *const JPushConfigFileName = @"PushConfig";
static NSString *const JMessageConfigFileName = @"PushConfig";
static NSDictionary *_luanchOptions = nil; static NSDictionary *_luanchOptions = nil;
@implementation JPushPlugin @implementation JPushPlugin
#pragma mark-
+(void)setLaunchOptions:(NSDictionary *)theLaunchOptions{
_luanchOptions=theLaunchOptions;
[JPUSHService setDebugMode];
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
} else {
//categories nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
//read appkey and channel from JMessageConfig.plist
NSString *plistPath = [[NSBundle mainBundle] pathForResource:JMessageConfigFileName ofType:@"plist"];
if (plistPath == nil) {
NSLog(@"error: PushConfig.plist not found");
assert(0);
}
NSMutableDictionary *plistData = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
NSString * appkey = [plistData valueForKey:JM_APP_KEY];
NSString * channel = [plistData valueForKey:JM_APP_CHANNEL];
NSNumber * isProduction = [plistData valueForKey:JM_APP_ISPRODUCTION];
if (!appkey || appkey.length == 0) {
NSLog(@"error: app key not found in JMessageConfig.plist ");
assert(0);
}
[JPUSHService setupWithOption:_luanchOptions appKey:appkey
channel:channel apsForProduction:[isProduction boolValue] ];
}
-(void)stopPush:(CDVInvokedUrlCommand*)command{ -(void)stopPush:(CDVInvokedUrlCommand*)command{
[[UIApplication sharedApplication]unregisterForRemoteNotifications]; [[UIApplication sharedApplication]unregisterForRemoteNotifications];
} }
-(void)resumePush:(CDVInvokedUrlCommand*)command{ -(void)resumePush:(CDVInvokedUrlCommand*)command{
[JPushPlugin registerForRemoteNotification];
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1 +(void)registerForRemoteNotification{
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//categories //categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
@ -80,46 +35,31 @@ static NSDictionary *_luanchOptions=nil;
UIUserNotificationTypeAlert) UIUserNotificationTypeAlert)
categories:nil]; categories:nil];
} else { } else {
//categories nil #ifndef __IPHONE_8_0
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
#else
//categories nil //categories nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound | UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert) UIRemoteNotificationTypeAlert)
categories:nil]; categories:nil];
#endif #endif
}
} }
-(void)isPushStopped:(CDVInvokedUrlCommand*)command{ -(void)isPushStopped:(CDVInvokedUrlCommand*)command{
NSNumber *result; NSNumber *result;
if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) { if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) {
result = @(0); result = @(0);
}else{ }else{
result = @(1); result = @(1);
} }
CDVPluginResult * pushResult=[self pluginResultForValue:result]; [self hanleResultWithValue:result command:command];
if (pushResult) {
[self succeedWithPluginResult:pushResult withCallbackID:command.callbackId];
} else {
[self failWithCallbackID:command.callbackId];
} }
}
-(void)initial:(CDVInvokedUrlCommand*)command{ -(void)initial:(CDVInvokedUrlCommand*)command{
//do nithng,because Cordova plugin use lazy load mode. //do nithng,because Cordova plugin use lazy load mode.
} }
#ifdef __CORDOVA_4_0_0 #ifdef __CORDOVA_4_0_0
- (void)pluginInitialize { - (void)pluginInitialize {
@ -133,7 +73,6 @@ static NSDictionary *_luanchOptions=nil;
NSLog(@"### initWithWebView "); NSLog(@"### initWithWebView ");
if (self=[super initWithWebView:theWebView]) { if (self=[super initWithWebView:theWebView]) {
[self initNotifications]; [self initNotifications];
} }
return self; return self;
} }
@ -141,10 +80,7 @@ static NSDictionary *_luanchOptions=nil;
#endif #endif
-(void)initNotifications { -(void)initNotifications {
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self [defaultCenter addObserver:self
selector:@selector(networkDidReceiveMessage:) selector:@selector(networkDidReceiveMessage:)
@ -164,7 +100,6 @@ static NSDictionary *_luanchOptions=nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:&error]; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:&error];
NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
if (!error) { if (!error) {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.openNotification',%@)",jsonString]]; [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.openNotification',%@)",jsonString]];
}); });
@ -175,95 +110,51 @@ static NSDictionary *_luanchOptions=nil;
} }
} }
-(void)setTagsWithAlias:(CDVInvokedUrlCommand*)command{ -(void)setTagsWithAlias:(CDVInvokedUrlCommand*)command{
NSArray *arguments = command.arguments; NSArray *arguments = command.arguments;
NSString *alias;
NSArray *tags;
if (!arguments || [arguments count] < 2) { if (!arguments || [arguments count] < 2) {
NSLog(@"#### setTagsWithAlias param is less"); NSLog(@"#### setTagsWithAlias param is less");
return ; return ;
}else{
alias = arguments[0];
tags = arguments[1];
} }
NSString *alias=[arguments objectAtIndex:0];
NSArray *arrayTags=[arguments objectAtIndex:1];
NSLog(@"#### setTagsWithAlias alias is %@, tags is %@",alias,arrayTags); NSLog(@"#### setTagsWithAlias alias is %@, tags is %@",alias,tags);
NSSet* set=[NSSet setWithArray:arrayTags]; [JPUSHService setTags:[NSSet setWithArray:tags]
[JPUSHService setTags:set
alias:alias alias:alias
callbackSelector:@selector(tagsWithAliasCallback:tags:alias:) callbackSelector:@selector(tagsWithAliasCallback:tags:alias:)
object:self]; object:self];
} }
-(void)setTags:(CDVInvokedUrlCommand *)command{ -(void)setTags:(CDVInvokedUrlCommand *)command{
NSArray *tags = command.arguments;
NSArray *arguments=[command arguments];
NSString *tags=[arguments objectAtIndex:0];
NSLog(@"#### setTags %@",tags); NSLog(@"#### setTags %@",tags);
NSArray *array=[tags componentsSeparatedByString:@","]; [JPUSHService setTags:[NSSet setWithArray:tags]
[JPUSHService setTags:[NSSet setWithArray:array]
callbackSelector:@selector(tagsWithAliasCallback:tags:alias:) callbackSelector:@selector(tagsWithAliasCallback:tags:alias:)
object:self]; object:self];
} }
-(void)setAlias:(CDVInvokedUrlCommand *)command{ -(void)setAlias:(CDVInvokedUrlCommand *)command{
NSArray *arguments=[command arguments]; NSLog(@"#### setAlias %@",command.arguments);
NSLog(@"#### setAlias %@",arguments); [JPUSHService setAlias:command.arguments[0]
[JPUSHService setAlias:[arguments objectAtIndex:0]
callbackSelector:@selector(tagsWithAliasCallback:tags:alias:) callbackSelector:@selector(tagsWithAliasCallback:tags:alias:)
object:self]; object:self];
} }
-(void)getRegistrationID:(CDVInvokedUrlCommand*)command{ -(void)getRegistrationID:(CDVInvokedUrlCommand*)command{
NSString* registrationID = [JPUSHService registrationID]; NSString* registrationID = [JPUSHService registrationID];
NSLog(@"### getRegistrationID %@",registrationID); NSLog(@"### getRegistrationID %@",registrationID);
[self hanleResultWithValue:registrationID command:command];
CDVPluginResult *result=[self pluginResultForValue:registrationID];
if (result) {
[self succeedWithPluginResult:result withCallbackID:command.callbackId];
} else {
[self failWithCallbackID:command.callbackId];
} }
}
-(void)tagsWithAliasCallback:(int)resultCode tags:(NSSet *)tags alias:(NSString *)alias{
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:resultCode],@"resultCode",
tags==nil?[NSNull null]:[tags allObjects],@"tags",
alias==nil?[NSNull null]:alias,@"alias",nil];
NSMutableDictionary *data = [NSMutableDictionary dictionary];
[data setObject:[NSNumber numberWithInt:resultCode] forKey:@"resultCode"];
[data setObject:tags==nil?[NSNull null]:[tags allObjects] forKey:@"tags"];
[data setObject:alias==nil?[NSNull null]:alias forKey:@"alias"];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
[self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.setTagsWithAlias',%@)",jsonString]];
// [self writeJavascript:[NSString stringWithFormat:@"window.plugins.jPushPlugin.pushCallback('%@')",jsonString]];
});
}
-(void)startLogPageView:(CDVInvokedUrlCommand*)command{ -(void)startLogPageView:(CDVInvokedUrlCommand*)command{
NSArray *arguments = command.arguments; NSArray *arguments = command.arguments;
@ -271,60 +162,52 @@ static NSDictionary *_luanchOptions=nil;
NSLog(@"startLogPageView argument error"); NSLog(@"startLogPageView argument error");
return ; return ;
} }
NSString * pageName=[arguments objectAtIndex:0]; NSString * pageName = arguments[0];
if (pageName) { if (pageName) {
[JPUSHService startLogPageView:pageName]; [JPUSHService startLogPageView:pageName];
} }
} }
-(void)stopLogPageView:(CDVInvokedUrlCommand*)command{ -(void)stopLogPageView:(CDVInvokedUrlCommand*)command{
NSArray *arguments = command.arguments; NSArray *arguments = command.arguments;
if (!arguments || [arguments count] < 1) { if (!arguments || [arguments count] < 1) {
NSLog(@"stopLogPageView argument error"); NSLog(@"stopLogPageView argument error");
return ; return ;
} }
NSString * pageName=[arguments objectAtIndex:0]; NSString * pageName = arguments[0];
if (pageName) { if (pageName) {
[JPUSHService stopLogPageView:pageName]; [JPUSHService stopLogPageView:pageName];
} }
} }
-(void)beginLogPageView:(CDVInvokedUrlCommand*)command{ -(void)beginLogPageView:(CDVInvokedUrlCommand*)command{
NSArray *arguments = command.arguments; NSArray *arguments = command.arguments;
if (!arguments || [arguments count] < 2) { if (!arguments || [arguments count] < 2) {
NSLog(@"beginLogPageView argument error"); NSLog(@"beginLogPageView argument error");
return ; return ;
} }
NSString * pageName=[arguments objectAtIndex:0]; NSString * pageName = arguments[0];
int duration=[[arguments objectAtIndex:0]intValue]; int duration = [arguments[0] intValue];
if (pageName) { if (pageName) {
[JPUSHService beginLogPageView:pageName duration:duration]; [JPUSHService beginLogPageView:pageName duration:duration];
} }
} }
-(void)setBadge:(CDVInvokedUrlCommand*)command{ -(void)setBadge:(CDVInvokedUrlCommand*)command{
NSArray *argument = command.arguments; NSArray *argument = command.arguments;
if ([argument count] < 1) { if ([argument count] < 1) {
NSLog(@"setBadge argument error!"); NSLog(@"setBadge argument error!");
return; return;
} }
NSNumber *badge=[argument objectAtIndex:0]; NSNumber *badge = argument[0];
[JPUSHService setBadge:[badge intValue]]; [JPUSHService setBadge:[badge intValue]];
} }
-(void)resetBadge:(CDVInvokedUrlCommand*)command{ -(void)resetBadge:(CDVInvokedUrlCommand*)command{
[JPUSHService resetBadge]; [JPUSHService resetBadge];
} }
-(void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand *)command{ -(void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand *)command{
// //
NSArray *argument = command.arguments; NSArray *argument = command.arguments;
@ -336,74 +219,93 @@ static NSDictionary *_luanchOptions=nil;
[UIApplication sharedApplication].applicationIconBadgeNumber = [badge intValue]; [UIApplication sharedApplication].applicationIconBadgeNumber = [badge intValue];
} }
-(void)getApplicationIconBadgeNumber:(CDVInvokedUrlCommand *)command { -(void)getApplicationIconBadgeNumber:(CDVInvokedUrlCommand *)command {
NSInteger num = [UIApplication sharedApplication].applicationIconBadgeNumber; NSInteger num = [UIApplication sharedApplication].applicationIconBadgeNumber;
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:num]; NSNumber *number = [NSNumber numberWithInteger:num];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; [self hanleResultWithValue:number command:command];
} }
-(void)setDebugModeFromIos:(CDVInvokedUrlCommand*)command{ -(void)setDebugModeFromIos:(CDVInvokedUrlCommand*)command{
[JPUSHService setDebugMode]; [JPUSHService setDebugMode];
} }
-(void)setLogOFF:(CDVInvokedUrlCommand*)command{ -(void)setLogOFF:(CDVInvokedUrlCommand*)command{
[JPUSHService setLogOFF]; [JPUSHService setLogOFF];
} }
#pragma mark-
+(void)setLaunchOptions:(NSDictionary *)theLaunchOptions{
_luanchOptions = theLaunchOptions;
[JPUSHService setDebugMode];
- (void)failWithCallbackID:(NSString *)callbackID { [JPushPlugin registerForRemoteNotification];
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
[self.commandDelegate sendPluginResult:result callbackId:callbackID]; //read appkey and channel from PushConfig.plist
NSString *plistPath = [[NSBundle mainBundle] pathForResource:JPushConfigFileName ofType:@"plist"];
if (plistPath == nil) {
NSLog(@"error: PushConfig.plist not found");
assert(0);
} }
NSMutableDictionary *plistData = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
NSString * appkey = [plistData valueForKey:JM_APP_KEY];
NSString * channel = [plistData valueForKey:JM_APP_CHANNEL];
NSNumber * isProduction = [plistData valueForKey:JM_APP_ISPRODUCTION];
if (!appkey || appkey.length == 0) {
- (void)succeedWithPluginResult:(CDVPluginResult *)result withCallbackID:(NSString *)callbackID { NSLog(@"error: app key not found in PushConfig.plist ");
[self.commandDelegate sendPluginResult:result callbackId:callbackID]; assert(0);
} }
[JPUSHService setupWithOption:_luanchOptions appKey:appkey
channel:channel apsForProduction:[isProduction boolValue] ];
}
#pragma mark js
-(void)hanleResultWithValue:(id)value command:(CDVInvokedUrlCommand*)command{
CDVPluginResult *result = nil;
CDVCommandStatus status = CDVCommandStatus_OK;
- (CDVPluginResult *)pluginResultForValue:(id)value {
CDVPluginResult *result;
if ([value isKindOfClass:[NSString class]]) { if ([value isKindOfClass:[NSString class]]) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK value = [value stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
messageAsString:[value stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
} else if ([value isKindOfClass:[NSNumber class]]) {
CFNumberType numberType = CFNumberGetType((CFNumberRef)value);
//note: underlyingly, BOOL values are typedefed as char
if (numberType == kCFNumberIntType || numberType == kCFNumberCharType) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:[value intValue]];
} else {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDouble:[value doubleValue]];
}
} else if ([value isKindOfClass:[NSArray class]]) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:value];
} else if ([value isKindOfClass:[NSDictionary class]]) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:value];
} else if ([value isKindOfClass:[NSNull class]]) { } else if ([value isKindOfClass:[NSNull class]]) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; value = nil;
}
if ([value isKindOfClass:[NSObject class]]) {
result = [CDVPluginResult resultWithStatus:status messageAsString:value];//NSObject
} else { } else {
NSLog(@"Cordova callback block returned unrecognized type: %@", NSStringFromClass([value class])); NSLog(@"Cordova callback block returned unrecognized type: %@", NSStringFromClass([value class]));
return nil; result = nil;
}
return result;
} }
if (!result) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
#pragma mark
-(void)tagsWithAliasCallback:(int)resultCode tags:(NSSet *)tags alias:(NSString *)alias{
NSDictionary *dict = @{@"resultCode":[NSNumber numberWithInt:resultCode],
@"tags" :tags == nil ? [NSNull null] : [tags allObjects],
@"alias" :alias == nil ? [NSNull null] : alias
};
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
[self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.setTagsWithAlias',%@)",jsonString]];
});
}
- (void)networkDidReceiveMessage:(NSNotification *)notification { - (void)networkDidReceiveMessage:(NSNotification *)notification {
if (notification) {
NSDictionary *userInfo = [notification userInfo]; NSDictionary *userInfo = [notification userInfo];
//NSLog(@"%@",userInfo); //NSLog(@"%@",userInfo);
@ -419,12 +321,9 @@ static NSDictionary *_luanchOptions=nil;
[self.commandDelegate evalJs:[NSString stringWithFormat:@"window.plugins.jPushPlugin.receiveMessageIniOSCallback('%@')",jsonString]]; [self.commandDelegate evalJs:[NSString stringWithFormat:@"window.plugins.jPushPlugin.receiveMessageIniOSCallback('%@')",jsonString]];
}); });
} }
}
-(void)networkDidReceiveNotification:(id)notification{ -(void)networkDidReceiveNotification:(id)notification{
@ -433,6 +332,7 @@ static NSDictionary *_luanchOptions=nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:&error]; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:&error];
NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"%ld",(long)[UIApplication sharedApplication].applicationState);
switch ([UIApplication sharedApplication].applicationState) { switch ([UIApplication sharedApplication].applicationState) {
case UIApplicationStateActive: case UIApplicationStateActive:
{ {
@ -457,5 +357,4 @@ static NSDictionary *_luanchOptions=nil;
} }
@end @end