xiongzhu il y a 3 ans
Parent
commit
8cbfde2e9f
2 fichiers modifiés avec 78 ajouts et 22 suppressions
  1. 4 12
      src/ios/AppDelegate+Umeng.m
  2. 74 10
      src/ios/UmengPlugin.m

+ 4 - 12
src/ios/AppDelegate+Umeng.m

@@ -23,18 +23,8 @@
 - (BOOL)application:(UIApplication *)application customDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     [self application:application customDidFinishLaunchingWithOptions:launchOptions];
     NSLog(@"DidFinishLaunchingWithOptions");
-    // Push组件基本功能配置
-    UMessageRegisterEntity * entity = [[UMessageRegisterEntity alloc] init];
-    //type是对推送的几个参数的选择,可以选择一个或者多个。默认是三个全部打开,即:声音,弹窗,角标
-    entity.types = UMessageAuthorizationOptionBadge|UMessageAuthorizationOptionSound|UMessageAuthorizationOptionAlert;
+    [[NSUserDefaults standardUserDefaults] setObject:launchOptions forKey:@"appLaunchOptions"];
     [UNUserNotificationCenter currentNotificationCenter].delegate = self;
-    [UMessage registerForRemoteNotificationsWithLaunchOptions:launchOptions Entity:entity completionHandler:^(BOOL granted, NSError * _Nullable error) {
-        if (granted) {
-            NSLog(@"registerForRemoteNotifications granted");
-        }else{
-            NSLog(@"registerForRemoteNotifications not granted");
-        }
-    }];
     return YES;
 }
 
@@ -53,7 +43,7 @@
 }
 
 //iOS10新增:处理后台点击通知的代理方法
--(void)userNotificationCenter:(UNUserNotificationCenter*)center didReceiveNotificationResponse:(UNNotificationResponse*)response withCompletionHandler:(void(^)())completionHandler{
+-(void)userNotificationCenter:(UNUserNotificationCenter*)center didReceiveNotificationResponse:(UNNotificationResponse*)response withCompletionHandler:(void(^)(void))completionHandler{
     NSDictionary* userInfo = response.notification.request.content.userInfo;
     if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]){
         //必须加这句代码
@@ -75,6 +65,8 @@
     //1.2.7版本开始不需要用户再手动注册devicetoken,SDK会自动注册
     //传入的devicetoken是系统回调didRegisterForRemoteNotificationsWithDeviceToken的入参,切记
     //[UMessage registerDeviceToken:deviceToken];
+    [[NSUserDefaults standardUserDefaults] setObject:hexToken forKey:@"deviceToken"];
+    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"deviceToken" object:hexToken]];
 }
 
 @end

+ 74 - 10
src/ios/UmengPlugin.m

@@ -31,6 +31,7 @@ static NSString* umengAppMasterSecret = @"";
 
 - (void)pluginInitialize
 {
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveDeviceToken:) name:@"deviceToken" object:nil];
     [UMConfigure setLogEnabled:true];
     [UMConfigure initWithAppkey:umengKey channel:nil];
     [UMCommonHandler setVerifySDKInfo:umengSecret complete:^(NSDictionary * _Nonnull resultDic) {
@@ -105,15 +106,78 @@ static NSString* umengAppMasterSecret = @"";
 
 - (void)initPush:(CDVInvokedUrlCommand*)command
 {
-//    // Push组件基本功能配置
-//    UMessageRegisterEntity * entity = [[UMessageRegisterEntity alloc] init];
-//    //type是对推送的几个参数的选择,可以选择一个或者多个。默认是三个全部打开,即:声音,弹窗,角标
-//    entity.types = UMessageAuthorizationOptionBadge|UMessageAuthorizationOptionSound|UMessageAuthorizationOptionAlert;
-//    [UNUserNotificationCenter currentNotificationCenter].delegate = self.appDelegate;
-//    [UMessage registerForRemoteNotificationsWithLaunchOptions:nil Entity:entity completionHandler:^(BOOL granted, NSError * _Nullable error) {
-//        if (granted) {
-//        }else{
-//        }
-//    }];
+    [[NSUserDefaults standardUserDefaults] setObject:command.callbackId forKey:@"initPushCallbackId"];
+    NSDictionary* launchOptions = [[NSUserDefaults standardUserDefaults] objectForKey:@"appLaunchOptions"];
+    // Push组件基本功能配置
+    UMessageRegisterEntity * entity = [[UMessageRegisterEntity alloc] init];
+    //type是对推送的几个参数的选择,可以选择一个或者多个。默认是三个全部打开,即:声音,弹窗,角标
+    entity.types = UMessageAuthorizationOptionBadge|UMessageAuthorizationOptionSound|UMessageAuthorizationOptionAlert;
+    [UMessage registerForRemoteNotificationsWithLaunchOptions:launchOptions Entity:entity completionHandler:^(BOOL granted, NSError * _Nullable error) {
+        if (granted) {
+            NSLog(@"registerForRemoteNotifications granted");
+        }else{
+            NSLog(@"registerForRemoteNotifications not granted");
+        }
+    }];
+}
+
+- (void)receiveDeviceToken:(NSNotification *)notification
+{
+    NSString* deviceToken = notification.object;
+    NSString* callbackId = [[NSUserDefaults standardUserDefaults] objectForKey:@"initPushCallbackId"];
+    if(callbackId){
+        [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:deviceToken] callbackId:callbackId];
+    }
+}
+
+- (void) shareImg:(CDVInvokedUrlCommand*)command
+{
+    UMSocialMessageObject* messageObject = [UMSocialMessageObject messageObject];
+    
+    NSDictionary* data = command.arguments[0];
+    NSString* imgUrl = data[@"imgUrl"];
+    NSString* thumb = data[@"thumb"];
+    NSString* platform = data[@"platform"];
+        
+    UMShareImageObject* shareObject = [self toImg:imgUrl];
+    if (thumb) {
+        shareObject.thumbImage = [self toImg:thumb];
+    }
+    messageObject.shareObject = shareObject;
+    UMSocialPlatformType platformType = UMSocialPlatformType_WechatSession;
+    if([@"WEIXIN" isEqualToString:platform]){
+        platformType = UMSocialPlatformType_WechatSession;
+    }else if([@"WEIXIN_CIRCLE" isEqualToString:platform]){
+        platformType = UMSocialPlatformType_WechatTimeLine;
+    }else if([@"WEIXIN_FAVORITE" isEqualToString:platform]){
+        platformType = UMSocialPlatformType_WechatFavorite;
+    }else if([@"QQ" isEqualToString:platform]){
+        platformType = UMSocialPlatformType_QQ;
+    }else if([@"QZONE" isEqualToString:platform]){
+        platformType = UMSocialPlatformType_Qzone;
+    }else if([@"SINA" isEqualToString:platform]){
+        platformType = UMSocialPlatformType_Sina;
+    }
+    //调用分享接口
+    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self.viewController completion:^(id data,NSError* error){
+        if(error){
+            NSLog(@"************Share fail with error %@*********",error);
+        }else{
+            NSLog(@"response data is %@",data);
+        }
+    }];
+}
+
+- (UMShareImageObject*) toImg:(NSString*)imgUrl{
+    UMShareImageObject*shareObject =[[UMShareImageObject alloc] init];
+    NSRegularExpression* regxp = [NSRegularExpression regularExpressionWithPattern:@"^(http|https)$" options:NSRegularExpressionCaseInsensitive error:nil];
+    if ([regxp numberOfMatchesInString:imgUrl options:0 range:NSMakeRange(0, [imgUrl length])]) {
+        [shareObject setShareImage:imgUrl];
+    }else{
+        NSRange range = [imgUrl rangeOfString:@","];
+        NSString* base64 = [imgUrl substringFromIndex:range.location+1];
+        [shareObject setShareImage:[[NSData alloc] initWithBase64EncodedString:base64 options:0]];
+    }
+    return shareObject;
 }
 @end