本文匹配的 SDK版本:r2.1.5 之后。
查看最近更新了解最新的SDK更新状况。
使用Xcode 6及以上版本可使用新版Push SDK,Xcode 5环境下须要运行旧版本SDK(1.7.4)ios
包名为JPush-iOS-SDK-{版本号}api
选择1:Cocoapods导入xcode
pod 'JPush'
pod 'JPush', '3.0.2'
选择2:手动导入app
注意: 若是集成JPush 3.0.1及以上版本, 且同时集成极光其余SDK(如:JMessage 3.0.0及以上版本)
1. Cocoapods导入,建议都更新为线上最新版本,来避免Jcore版本不一致致使的冲突。
2. 手动导入,在工程中只需保留一个最新版本的jcore-ios-x.x.x.a静态库文件。less
若是你的工程须要支持小于7.0的iOS系统,请到Build Settings 关闭 bitCode 选项,不然将没法正常编译经过。dom
如使用Xcode8及以上环境开发,请开启Application Target的Capabilities->Push Notifications选项,如图:ide
若是您使用的是2.1.9及以上的版本则不须要配置此步骤
若是用的是Xcode7或更新版本,须要在App项目的plist手动配置下key和值以支持http传输:fetch
选择1:根据域名配置ui
如图:debug
选择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>
为AppDelegate添加Delegate。
参考代码:
@interface AppDelegate ()<JPUSHRegisterDelegate> @end
2.1.0版本开始,API类名为JPUSHService,再也不使用原先的APService。
请将如下代码添加到
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//Required //notice: 3.0.0及之后版本注册能够这样写,也能够继续用以前的注册方式 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];
请将如下代码添加到
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// Optional // 获取IDFA // 如需使用IDFA功能请添加此代码并在初始化方法的advertisingIdentifier参数中填写对应值 NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; // Required // init Push // notice: 2.1.5版本的SDK新增的注册方法,改为可上报IDFA,若是没有使用IDFA直接传nil // 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。 [JPUSHService setupWithOption:launchOptions appKey:appKey channel:channel apsForProduction:isProduction advertisingIdentifier:advertisingId];
请在AppDelegate.m实现该回调方法并添加回调方法中的代码
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { /// Required - 注册 DeviceToken [JPUSHService registerDeviceToken:deviceToken]; }
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { //Optional NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); }
请在AppDelegate.m实现该回调方法并添加回调方法中的代码
#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); // 须要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型能够选择设置 } // 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(); // 系统要求执行这个方法 } - (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的自定义消息功能,请参考文档来实现自定义消息的处理回调方法。
真机调试该项目,若是控制台输出如下日志则表明您已经集成成功。
2016-08-19 17:12:12.745823 219b28[1443:286814] | JPUSH | I - [JPUSHLogin] ----- login result ----- uid:5460310207 registrationID:171976fa8a8620a14a4
若是调试运行中遇到问题请参考:iOS SDK 调试指南
r2.1.5版本增长一个上传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;
建议开发者加上API里面提供的如下类型的通知:
extern NSString *const kJPFNetworkIsConnectingNotification; // 正在链接中
extern NSString * const kJPFNetworkDidSetupNotification; // 创建链接
extern NSString * const kJPFNetworkDidCloseNotification; // 关闭链接
extern NSString * const kJPFNetworkDidRegisterNotification; // 注册成功
extern NSString *const kJPFNetworkFailedRegisterNotification; //注册失败
extern NSString * const kJPFNetworkDidLoginNotification; // 登陆成功
舒适提示:
Registration id 须要添加注册kJPFNetworkDidLoginNotification通知的方法里获取,也能够调用[registrationIDCompletionHandler:]方法,经过completionHandler获取
extern NSString * const kJPFNetworkDidReceiveMessageNotification; // 收到自定义消息(非APNs)
其中,kJPFNetworkDidReceiveMessageNotification传递的数据能够经过NSNotification中的userInfo方法获取,包括标题、内容、extras信息等