創(chuàng)建應(yīng)用
在 JPush的管理Portal 上創(chuàng)建應(yīng)用并上傳APNs證書预明。如果對(duì)APNs證書不太了解 請參考: iOS 證書設(shè)置指南
創(chuàng)建成功后自動(dòng)生成 AppKey 用以標(biāo)識(shí)該應(yīng)用。
配置工程
導(dǎo)入SDK
將SDK包解壓谜悟,在Xcode中選擇“Add files to 'Your project name'...”,將解壓后的lib子文件夾(包含JPUSHService.h、jpush-ios-x.x.x.a,jcore-ios-x.x.x.a)添加到你的工程目錄中痹升。
添加
FrameworkCFNetwork.framework
CoreFoundation.framework
CoreTelephony.framework
SystemConfiguration.framework
CoreGraphics.framework
Foundation.framework
UIKit.framework
Security.framework
libz.tbd (Xcode7以下版本是libz.dylib)
AdSupport.framework (獲取IDFA需要;如果不使用IDFA畦韭,請不要添加)
UserNotifications.framework (Xcode8及以上)
libresolv.tbd (JPush 2.2.0及以上版本需要, Xcode7以下版本是libresolv.dylib)
Build Settings
如果你的工程需要支持小于7.0的iOS系統(tǒng)疼蛾,請到Build Settings 關(guān)閉 bitCode 選項(xiàng),否則將無法正常編譯通過艺配。
設(shè)置 Search Paths 下的 User Header Search Paths 和 Library Search Paths察郁,比如SDK文件夾(默認(rèn)為lib)與工程文件在同一級(jí)目錄下,則都設(shè)置為"$(SRCROOT)/{靜態(tài)庫所在文件夾名稱}"即可转唉。
Capabilities
如使用Xcode8及以上環(huán)境開發(fā)皮钠,請開啟Application Target的Capabilities->Push Notifications選項(xiàng),如圖:
允許Xcode7支持Http傳輸方法
如果您使用的是2.1.9及以上的版本則不需要配置此步驟如果用的是Xcode7或更新版本赠法,需要在App項(xiàng)目的plist手動(dòng)配置下key和值以支持http傳輸:
選擇1:根據(jù)域名配置
在項(xiàng)目的info.plist中添加一個(gè)Key:NSAppTransportSecurity麦轰,類型為字典類型。
然后給它添加一個(gè)NSExceptionDomains砖织,類型為字典類型款侵;
把需要的支持的域添加給NSExceptionDomains。其中jpush.cn作為Key侧纯,類型為字典類型新锈。
每個(gè)域下面需要設(shè)置2個(gè)屬性:NSIncludesSubdomains、NSExceptionAllowsInsecureHTTPLoads眶熬。 兩個(gè)屬性均為Boolean類型妹笆,值分別為YES、YES娜氏。
如圖:
選擇2:全局配置
<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>
添加頭文件
請將以下代碼添加到 AppDelegate.m 引用頭文件的位置晾浴。
// 引入JPush功能所需頭文件
#import "JPUSHService.h"
// iOS10注冊APNs所需頭文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
// 如果需要使用idfa功能所需要引入的頭文件(可選)
#import <AdSupport/AdSupport.h>
添加Delegate
為AppDelegate添加Delegate。
參考代碼:
@interface AppDelegate ()<JPUSHRegisterDelegate>
@end
添加初始化代碼
2.1.0版本開始,API類名為JPUSHService牍白,不再使用原先的APService。
添加初始化APNs代碼
請將以下代碼添加到
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//Required
//notice: 3.0.0及以后版本注冊可以這樣寫抖棘,也可以繼續(xù)用之前的注冊方式
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
// 可以添加自定義categories
// NSSet<UNNotificationCategory *> *categories for iOS10 or later
// NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
}
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
添加初始化JPush代碼
請將以下代碼添加到-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// Optional
// 獲取IDFA
// 如需使用IDFA功能請?zhí)砑哟舜a并在初始化方法的advertisingIdentifier參數(shù)中填寫對(duì)應(yīng)值
NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
// Required
// init Push
// notice: 2.1.5版本的SDK新增的注冊方法茂腥,改成可上報(bào)IDFA狸涌,如果沒有使用IDFA直接傳nil
// 如需繼續(xù)使用pushConfig.plist文件聲明appKey等配置內(nèi)容,請依舊使用[JPUSHService setupWithOption:launchOptions]方式初始化最岗。
[JPUSHService setupWithOption:launchOptions appKey:appKey channel:channel apsForProduction:isProduction advertisingIdentifier:advertisingId];
部分參數(shù)說明:
appKey 填寫管理Portal上創(chuàng)建應(yīng)用后自動(dòng)生成的AppKey值帕胆。請確保應(yīng)用內(nèi)配置的 AppKey 與 Portal 上創(chuàng)建應(yīng)用后生成的 AppKey 一致。
channel 指明應(yīng)用程序包的下載渠道般渡,為方便分渠道統(tǒng)計(jì)懒豹,具體值由你自行定義,如:App Store驯用。
apsForProduction1.3.1版本新增脸秽,用于標(biāo)識(shí)當(dāng)前應(yīng)用所使用的APNs證書環(huán)境。
0 (默認(rèn)值)表示采用的是開發(fā)證書蝴乔,1 表示采用生產(chǎn)證書發(fā)布應(yīng)用记餐。
注:此字段的值要與Build Settings的Code Signing配置的證書環(huán)境一致。
advertisingIdentifier詳見關(guān)于IDFA薇正。
注冊APNs成功并上報(bào)DeviceToken
請?jiān)贏ppDelegate.m實(shí)現(xiàn)該回調(diào)方法并添加回調(diào)方法中的代碼
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/// Required - 注冊 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
實(shí)現(xiàn)注冊APNs失敗接口(可選)
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
添加處理APNs通知回調(diào)方法
請?jiān)贏ppDelegate.m實(shí)現(xiàn)該回調(diào)方法并添加回調(diào)方法中的代碼
#pragma mark- JPUSHRegisterDelegate
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
// Required
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(UNNotificationPresentationOptionAlert); // 需要執(zhí)行這個(gè)方法片酝,選擇是否提醒用戶,有Badge挖腰、Sound雕沿、Alert三種類型可以選擇設(shè)置
}
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
// Required
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(); // 系統(tǒng)要求執(zhí)行這個(gè)方法
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Required, iOS 7 Support
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// Required,For systems with less than or equal to iOS6
[JPUSHService handleRemoteNotification:userInfo];
}
添加處理JPush自定義消息回調(diào)方法
如需使用JPush的自定義消息功能,請參考文檔來實(shí)現(xiàn)自定義消息的處理回調(diào)方法猴仑。
成功運(yùn)行
真機(jī)調(diào)試該項(xiàng)目审轮,如果控制臺(tái)輸出以下日志則代表您已經(jīng)集成成功。
2016-08-19 17:12:12.745823 219b28[1443:286814] | JPUSH | I - [JPUSHLogin]
----- login result -----
uid:5460310207
registrationID:171976fa8a8620a14a4
如果調(diào)試運(yùn)行中遇到問題請參考:iOS SDK 調(diào)試指南
高級(jí)功能
關(guān)于IDFA
+ (void)setupWithOption:(NSDictionary *)launchingOption
appKey:(NSString *)appKey
channel:(NSString *)channel
apsForProduction:(BOOL)isProduction
advertisingIdentifier:(NSString *)advertisingId;
如果不使用IDFA宁脊,仍可使用接口
+ (void)setupWithOption:(NSDictionary *)launchingOption
appKey:(NSString *)appKey
channel:(NSString *)channel
apsForProduction:(BOOL)isProduction;
JPush SDK 相關(guān)事件監(jiān)聽
建議開發(fā)者加上API里面提供的以下類型的通知:
extern NSString *const kJPFNetworkIsConnectingNotification; // 正在連接中
extern NSString * const kJPFNetworkDidSetupNotification; // 建立連接
extern NSString * const kJPFNetworkDidCloseNotification; // 關(guān)閉連接
extern NSString * const kJPFNetworkDidRegisterNotification; // 注冊成功
extern NSString *const kJPFNetworkFailedRegisterNotification; //注冊失敗
extern NSString * const kJPFNetworkDidLoginNotification; // 登錄成功
溫馨提示:
Registration id 需要添加注冊kJPFNetworkDidLoginNotification通知的方法里獲取断国,
也可以調(diào)用[registrationIDCompletionHandler:]方法,通過completionHandler獲取
extern NSString * const kJPFNetworkDidReceiveMessageNotification; // 收到自定義消息(非APNs)
其中榆苞,kJPFNetworkDidReceiveMessageNotification傳遞的數(shù)據(jù)可以通過NSNotification中的userInfo方法獲取稳衬,包括標(biāo)題、內(nèi)容坐漏、extras信息等
請參考文檔:iOS SDK API
下面是推送證書相關(guān)設(shè)置
也就是最上面第一個(gè)圖里提到的推送證書薄疚,這里我先放的是開發(fā)環(huán)境下的推送證書
看看是否push notifications 開啟,如果未開啟赊琳,點(diǎn)擊下面的Edit進(jìn)行編輯
這里需要的推送證書需要提前準(zhǔn)備好街夭,然后在這里選擇證書
然后去鑰匙串里生成
![Uploading Paste_Image_128597.png . . .]
點(diǎn)擊下載備用
然后在剛才app id的配置里選擇這個(gè)推送證書就可以了