最近在用swift重构公司的项目,首先踩的就是第三方SDK的坑,因为友盟没有swift集成的相关文档,所以只能摸着石头过河了,再次记录一下自己踩的坑,同时也希望能给需要的人带来帮助
上代码,首先是友盟app统计
1 2 3 4 5 6 7 8 9 10
| OC [UMAnalyticsConfig sharedInstance].appKey = UMENGAPPKEY; [MobClick startWithConfigure:[UMAnalyticsConfig sharedInstance]]; [MobClick setCrashReportEnabled:YES];
Swift let obj=UMAnalyticsConfig.init(); obj.appKey=key; MobClick.setLogEnabled(true); MobClick.start(withConfigure: obj);
|
这个是从友盟论坛上面挖出来的,一个小哥骚扰了人工2个多小时得出来的成果,本人连接了半个多小时人工受理也没有连上…
下面是坑点有点多的友盟推送
这里是appDelegate中的代理方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| OC #pragma mark - GetNotification //iOS10以下使用这个方法接收通知 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ //关闭友盟自带的弹出框 [UMessage setAutoAlert:NO]; [UMessage didReceiveRemoteNotification:userInfo]; } }
//iOS10新增:处理前台收到通知的代理方法 -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{ NSDictionary * userInfo = notification.request.content.userInfo; if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { //应用处于前台时的远程推送接受 //关闭友盟自带的弹出框 [UMessage setAutoAlert:NO]; //必须加这句代码 [UMessage didReceiveRemoteNotification:userInfo]; }else{ //应用处于前台时的本地推送接受 } //当应用处于前台时提示设置,需要哪个可以设置哪一个 completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert); }
//iOS10新增:处理后台点击通知的代理方法 -(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{ NSDictionary * userInfo = response.notification.request.content.userInfo; if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { //应用处于后台时的远程推送接受 //必须加这句代码 [UMessage didReceiveRemoteNotification:userInfo]; }else{ //应用处于后台时的本地推送接受 } }
Swift //iOS10以下使用这个方法接收通知 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) { UMessage.setAutoAlert(true) UMessage.didReceiveRemoteNotification(userInfo) } @available(iOS 10.0, *) func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void){ let userInfo = notification.request.content.userInfo if (notification.request.trigger?.isKind(of: UNPushNotificationTrigger.superclass()!))! { //应用处于前台时的远程推送接受 //关闭友盟自带的弹出框 UMessage.setAutoAlert(false) //必须加这句代码 UMessage.didReceiveRemoteNotification(userInfo) }else{ //应用处于前台时的本地推送接受 } //当应用处于前台时提示设置,需要哪个可以设置哪一个 completionHandler([.sound,.alert,.badge]) } @available(iOS 10.0, *) func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void){ let userInfo = response.notification.request.content.userInfo if (response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.superclass()!))! { //应用处于前台时的远程推送接受 //必须加这句代码 UMessage.didReceiveRemoteNotification(userInfo) }else{ //应用处于前台时的本地推送接受 } }
|
我只想说@available(iOS 10.0, *)
很重要!@available(iOS 10.0, *)
很重要!@available(iOS 10.0, *)
很重要!很重要!重要!要!不然除非能一字不差把方法名写出来,等着IDE提示补全@available(iOS 10.0, *)
这个是推送注册的方法,可以自行封装,@available(iOS 10.0, *)
的使用依然很重要
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| Swift UMessage.start(withAppkey: key, launchOptions: launchOptions) UMessage.registerForRemoteNotifications() if #available(iOS 10.0, *) { let center:UNUserNotificationCenter = UNUserNotificationCenter.current() center.delegate = theDelegate let type:UNAuthorizationOptions = [.badge, .alert, .sound] center.requestAuthorization(options: type, completionHandler: { (granted, error) in if granted{ }else{ } }) let action1_ios10 = UNNotificationAction.init(identifier: "action1_ios10_identifier", title: "打开应用", options: .foreground) let action2_ios10 = UNNotificationAction.init(identifier: "action2_ios10_identifier", title: "忽略", options: .foreground) let category1_ios10 = UNNotificationCategory.init(identifier: "category101", actions: [action1_ios10,action2_ios10], intentIdentifiers: [], options: .customDismissAction) let categories_ios10 = NSSet.init(object: category1_ios10) center.setNotificationCategories(categories_ios10 as! Set<UNNotificationCategory>) } else { //如果你期望使用交互式(只有iOS 8.0及以上有)的通知,请参考下面注释部分的初始化代码 let action1 = UIMutableUserNotificationAction() action1.identifier = "action1_identifier" action1.title = "打开应用" action1.activationMode = .foreground let action2 = UIMutableUserNotificationAction() action2.identifier = "action2_identifier" action2.title = "忽略" action2.activationMode = .background action2.isAuthenticationRequired = true action2.isDestructive = true let actionCategory1 = UIMutableUserNotificationCategory() actionCategory1.identifier = "category1"//这组动作的唯一标示 actionCategory1.setActions([action1,action2], for: .default) // NSSet *categories = [NSSet setWithObjects:actionCategory1, nil]; let categories = NSSet.init(object: actionCategory1) UMessage.register(forRemoteNotifications: categories as! Set<UIUserNotificationCategory>) } //如果对角标,文字和声音的取舍,请用下面的方法 //UIRemoteNotificationType types7 = UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound; //UIUserNotificationType types8 = UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge; //[UMessage registerForRemoteNotifications:categories withTypesForIos7:types7 withTypesForIos8:types8]; UMessage.setLogEnabled(true)
|
咳咳咳,不知道大家注意到这句没有
1
| let type:UNAuthorizationOptions = [.badge, .alert, .sound]
|
OC中对应的是
1
| UNAuthorizationOptions types=UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound;
|
这个是逻辑或的写法,有的小伙伴没有很仔细看swift的基础语法,就容易到这里卡主(比如我),还有就是swift3.0更新不少,目的一个字!简洁!
UNAuthorizationOptionBadge变成了.badge其他的大家自行脑补吧!
下面是分享
1 2 3 4 5 6
| let umengShareSevice = UMSocialManager.default() umengShareSevice?.umSocialAppkey = UMENGKEY umengShareSevice?.setPlaform(.wechatSession, appKey: WECHATAPPKEY, appSecret: WECHATAPPSECRET , redirectURL: WECHATREDIRECTURL) umengShareSevice?.setPlaform(.wechatTimeLine, appKey: WECHATAPPKEY, appSecret: WECHATAPPSECRET , redirectURL: WECHATREDIRECTURL) umengShareSevice?.setPlaform(.QQ, appKey: QQAPPKEY, appSecret: nil , redirectURL: QQREDIRECTURL) umengShareSevice?.setPlaform(.sina, appKey: SINAAPPKEY, appSecret: SINAAPPSECRET , redirectURL: SINAREDIRECTURL)
|
友盟的分享还是给出了swift版本的demo
1
| UMSocialUIManager.setPreDefinePlatforms([NSNumber(value:UMSocialPlatformType.wechatSession.rawValue),NSNumber(value:UMSocialPlatformType.wechatTimeLine.rawValue),NSNumber(value:UMSocialPlatformType.QQ.rawValue),NSNumber(value:UMSocialPlatformType.sina.rawValue)])
|
上面是我在自定义平台时候的一个问题,setPreDefinePlatforms方法的参数是一个数组,虽然提示是[Any]!类型但是里面还是要元素为NSNumber类型的数组
大致就这些吧,希望能给大家带来帮助