标签:art 项目 option configure 自己 send delegate and 分享
之前分享过集成友盟推送的方法, 需要的朋友可以查看一下链接:
http://www.cnblogs.com/zhouxihi/p/6533058.html
一般开发中我们比较多使用的三方有友盟推送, 友盟分享, 友盟登录, 微信支付, 支付宝支付, 融云等等...等等...
光集成一个友盟推送就要好几十行代码, 如果多集成几个AppDelegate就会变得臃肿不堪, 也降低了可读性
为了解决这个问题, 目前想到以Category的方式给AppDelegate添加新的类别去完成这些三方集成
先以友盟推送为例
具体方法为先创建一个类别AppDelegate+UMengPush.h
给类别添加一个userInfo属性用来临时存放接收到的推送消息,
@property (nonatomic, strong) NSDictionary *userInfo;
以及一个配置友盟的方法
/** 配置友盟推送SDK @param launchOptions App launchOptions */ - (void)configureUMessageWithLaunchOptions:(NSDictionary *)launchOptions;
因为类别增加的属性不能直接赋值和取值, 还要再专门增加getter / setter方法
/** 给类别属性赋值 @param userInfo 推送消息字典 */ - (void)zx_setUserInfo:(NSDictionary *)userInfo; /** 获取类别属性值 @return 暂存的推送消息 */ - (NSDictionary *)zx_getUserInfo;
实现文件直接给大家看吧, 注释的很清楚
// // AppDelegate+UMengPush.m // UMengPushDemo // // Created by Jackey on 2017/7/3. // Copyright ? 2017年 com.zhouxi. All rights reserved. // #import "AppDelegate+UMengPush.h" #import "UMessage.h" #import <objc/runtime.h> static char UserInfoKey; @implementation AppDelegate (UMengPush) #pragma mark - Configure UMessage SDK - (void)configureUMessageWithLaunchOptions:(NSDictionary *)launchOptions { // 设置AppKey & LaunchOptions [UMessage startWithAppkey:UMessageAppKey launchOptions:launchOptions]; // 注册 [UMessage registerForRemoteNotifications]; // 开启Log [UMessage setLogEnabled:YES]; // 检查是否为iOS 10以上版本 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) { // 如果检查到时iOS 10以上版本则必须执行以下操作 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; UNAuthorizationOptions types10 = UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound; [center requestAuthorizationWithOptions:types10 completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { // 点击允许 // 这里可以添加一些自己的逻辑 } else { // 点击不允许 // 这里可以添加一些自己的逻辑 } }]; } } #pragma mark - UMessage Delegate Methods - (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo { // 关闭友盟自带的弹出框 [UMessage setAutoAlert:NO]; [UMessage didReceiveRemoteNotification:userInfo]; [self zx_setUserInfo:userInfo]; // 定制自己的弹出框 if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:userInfo[@"aps"][@"alert"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alertView show]; } } // iOS 10新增: 处理前台收到通知的代理方法 - (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{ //应用处于后台时的本地推送接受 } } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { [UMessage sendClickReportForRemoteNotification:[self zx_getUserInfo]]; } - (void)zx_setUserInfo:(NSDictionary *)userInfo { objc_setAssociatedObject(self, &UserInfoKey, userInfo, OBJC_ASSOCIATION_COPY_NONATOMIC); } - (NSDictionary *)zx_getUserInfo { if (objc_getAssociatedObject(self, &UserInfoKey)) { return objc_getAssociatedObject(self, &UserInfoKey); } else { return nil; } } @end
注意需要在PCH中设置UMessage AppKey
#define UMessageAppKey @"xxxxxxxxxxxxxxxxxxxxx"
这样当们有项目需要继承友盟推送的时候, 只有配置好key, 在AppDelegate中只要简单一句话就完成了
#import "AppDelegate.h" #import "AppDelegate+UMengPush.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // 配置UMessage [self configureUMessageWithLaunchOptions:launchOptions]; return YES; }
附上Demo: https://github.com/zhouxihi/ThirdPartDemo
后面会再陆续完成友盟分享, 友盟登录, 支付宝/微信支付等的内容, 欢迎大家指出不足
如果对大家有帮忙, 还请不吝帮忙star
标签:art 项目 option configure 自己 send delegate and 分享
原文地址:http://www.cnblogs.com/zhouxihi/p/7113511.html