1. 程式人生 > >AppDelegate減負之常用三方封裝 - 友盟推送篇

AppDelegate減負之常用三方封裝 - 友盟推送篇

uia 臃腫 ant mic span 接收 常用 epo nil

之前分享過集成友盟推送的方法, 需要的朋友可以查看一下鏈接:

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

AppDelegate減負之常用三方封裝 - 友盟推送篇