iOS SDK 集成指南SDK說明適用版本本文匹配的 SDK版本:r2.1.5 以后鸿摇。查看最近更新了解最新的SDK更新情況。使用Xcode 6及以上版本可以使用新版Push SDK捐康,Xcode 5環(huán)境下需要運(yùn)行舊版本SDK(1.7.4)資源文件包名為JPush-iOS-SDK-{版本號(hào)}lib文件夾:包含頭文件 JPUSHService.h,靜態(tài)庫文件jpush-ios-x.x.x.a 咒钟,支持的iOS版本為 6.0 及以上版本吹由。(請(qǐng)注意:模擬器不支持APNs)pdf文件:集成指南demo文件夾:示例創(chuàng)建應(yīng)用在 JPush的管理Portal 上創(chuàng)建應(yīng)用并上傳APNs證書。如果對(duì)APNs證書不太了解 請(qǐng)參考: iOS 證書設(shè)置指南jpush_ios創(chuàng)建成功后自動(dòng)生成 AppKey 用以標(biāo)識(shí)該應(yīng)用朱嘴。jpush_ios配置工程導(dǎo)入SDK將SDK包解壓倾鲫,在Xcode中選擇“Add files to 'Your project name'...”,將解壓后的lib子文件夾(包含JPUSHService.h萍嬉、jpush-ios-x.x.x.a)添加到你的工程目錄中乌昔。添加FrameworkCFNetwork.frameworkCoreFoundation.frameworkCoreTelephony.frameworkSystemConfiguration.frameworkCoreGraphics.frameworkFoundation.frameworkUIKit.frameworkSecurity.frameworklibz.tbd (Xcode7以下版本是libz.dylib)AdSupport.framework (獲取IDFA需要;如果不使用IDFA壤追,請(qǐng)不要添加)UserNotifications.framework (Xcode8及以上)libresolv.tbd (JPush 2.2.0及以上版本需要, Xcode7以下版本是libresolv.dylib)Build Settings如果你的工程需要支持小于7.0的iOS系統(tǒng)磕道,請(qǐ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ā)疯特,請(qǐng)開啟Application Target的Capabilities->Push Notifications選項(xiàng),如圖:jpush_ios允許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。如圖:jpush_ios選擇2:全局配置NSAppTransportSecurityNSAllowsArbitraryLoads添加頭文件請(qǐng)將以下代碼添加到 AppDelegate.m 引用頭文件的位置趣效。// 引入JPush功能所需頭文件#import "JPUSHService.h"http:// iOS10注冊(cè)APNs所需頭文件#ifdef NSFoundationVersionNumber_iOS_9_x_Max#import#endif// 如果需要使用idfa功能所需要引入的頭文件(可選)#import添加Delegate為AppDelegate添加Delegate瘦癌。參考代碼:@interface AppDelegate ()@end
添加初始化代碼
2.1.0版本開始,API類名為JPUSHService,不再使用原先的APService跷敬。
添加初始化APNs代碼
請(qǐng)將以下代碼添加到
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//Required
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
}
else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//可以添加自定義categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
}
else {
//categories 必須為nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
添加初始化JPush代碼
請(qǐng)將以下代碼添加到
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// Optional
// 獲取IDFA
// 如需使用IDFA功能請(qǐng)?zhí)砑哟舜a并在初始化方法的advertisingIdentifier參數(shù)中填寫對(duì)應(yīng)值
NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
// Required
// init Push
// notice: 2.1.5版本的SDK新增的注冊(cè)方法讯私,改成可上報(bào)IDFA,如果沒有使用IDFA直接傳nil
// 如需繼續(xù)使用pushConfig.plist文件聲明appKey等配置內(nèi)容西傀,請(qǐng)依舊使用[JPUSHService setupWithOption:launchOptions]方式初始化斤寇。
[JPUSHService setupWithOption:launchOptions appKey:appKey
channel:channel
apsForProduction:isProduction
advertisingIdentifier:advertisingId];
部分參數(shù)說明:
appKey
填寫管理Portal上創(chuàng)建應(yīng)用后自動(dòng)生成的AppKey值。請(qǐng)確保應(yīng)用內(nèi)配置的 AppKey 與 Portal 上創(chuàng)建應(yīng)用后生成的 AppKey 一致拥褂。
channel
指明應(yīng)用程序包的下載渠道娘锁,為方便分渠道統(tǒng)計(jì),具體值由你自行定義饺鹃,如:App Store莫秆。
apsForProduction
1.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缝驳。
注冊(cè)APNs成功并上報(bào)DeviceToken
請(qǐng)?jiān)贏ppDelegate.m實(shí)現(xiàn)該回調(diào)方法并添加回調(diào)方法中的代碼
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/// Required - 注冊(cè) DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
實(shí)現(xiàn)注冊(cè)APNs失敗接口(可選)
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
添加處理APNs通知回調(diào)方法
請(qǐng)?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的自定義消息功能拼弃,請(qǐng)參考文檔來實(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)行中遇到問題請(qǐng)參考:iOS SDK 調(diào)試指南
高級(jí)功能
關(guān)于IDFA
r2.1.5版本增加一個(gè)上傳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; // 注冊(cè)成功
extern NSString *const kJPFNetworkFailedRegisterNotification; //注冊(cè)失敗
extern NSString * const kJPFNetworkDidLoginNotification; // 登錄成功
溫馨提示:
Registration id 需要添加注冊(cè)kJPFNetworkDidLoginNotification通知的方法里獲取,也可以調(diào)用[registrationIDCompletionHandler:]方法医男,通過completionHandler獲取
extern NSString * const kJPFNetworkDidReceiveMessageNotification; // 收到自定義消息(非APNs)
其中砸狞,kJPFNetworkDidReceiveMessageNotification傳遞的數(shù)據(jù)可以通過NSNotification中的userInfo方法獲取,包括標(biāo)題镀梭、內(nèi)容刀森、extras信息等
請(qǐng)參考文檔:iOS SDK API