本篇文章采用Xcode手动集成JPush

证书

参考网址:https://docs.jiguang.cn//jpush/client/iOS/ios_cer_guide/

下载SDK

下载网址:https://docs.jiguang.cn//jpush/resources/

  • SDK目录

导入SDK

  • 将Lib中的.h文件与.a文件导入项目

  • .h文件

  • .a文件

手动导入

  • 添加框架

    CFNetwork.framework

    CoreFoundation.framework

    CoreTelephony.framework

    SystemConfiguration.framework

    CoreGraphics.framework

    Foundation.framework

    UIKit.framework

    Security.framework

    libz.tbd(Xcode 7以下版本是libz.dylib)

    AdSupport.framework(获取IDFA需要;如果不使用IDFA,请不要添加)

    UserNotifications.framework(Xcode 8及以上)

    libresolv.tbd(JPush 2.2.0及以上版本需要,Xcode 7以下版本是libresolv.dylib)

构建设置

如果你的工程需要支持小于7.0的iOS系统,请到构建设置关闭bitCode选项,否则将无法正常编译通过。

设置搜索路径下的用户头搜索路径和库搜索路径,比如SDK文件夹(默认为lib)与工程文件在同一级目录下,则都设置为“$(SRCROOT)/ {静态库所在文件夹名称} “即可。

功能

如使用Xcode 8及以上环境开发,请开启Application Target的功能 - >推送通知选项,如图:

允许Xcode 7支持Http传输方法

  • 在项目的info.plist中添加一个Key:NSAppTransportSecurity,类型为字典类型。
  • 然后给它添加一个NSExceptionDomains,类型为字典类型;
  • 把需要的支持的域添加给NSExceptionDomains。其中jpush.cn作为Key,类型为字典类型。
  • 每个域下面需要设置2个属性:NSIncludesSubdomains,NSExceptionAllowsInsecureHTTPLoads。两个属性均为布尔类型,值分别为YES,YES。

代码配置

添加头文件

  • 将以下代码添加到AppDelegate.m引用头文件的位置。
// 引入 JPush 功能所需头文件
#import "JPUSHService.h"
// iOS10 注册 APNs 所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
  • 添加代表
@interface AppDelegate ()<JPUSHRegisterDelegate>

@end

添加初始化APNs代码

  • 请将以下代码添加到 - (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  //Required
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
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)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  // Required
// init Push
// notice: 2.1.5 版本的 SDK 新增的注册方法,改成可上报 IDFA,如果没有使用 IDFA 直接传 nil
// 如需继续使用 pushConfig.plist 文件声明 appKey 等配置内容,请依旧使用 [JPUSHService setupWithOption:launchOptions] 方式初始化。
[JPUSHService setupWithOption:launchOptions appKey:@"你的appkey"
channel:@"Publish channel"
apsForProduction:NO
advertisingIdentifier:nil];
  • 部分参数说明:

  • APPKEY

    • 选择Web Portal上的应用,点击“设置”获取其appkey值。请确保应用内配置的appkey与Portal上创建应用后生成的appkey一致。
  • 渠道
    • 指明应用程序包的下载渠道,为方便分渠道统计,具体值由你自行定义,如:App Store。
  • apsForProduction
    • 1.3.1版本新增,用于标识当前应用所使用的APNs证书环境。
    • 0(默认值)表示采用的是开发证书,1表示采用生产证书发布应用。
    • 注:此字段的值要与Build Settings的Code Signing配置的证书环境一致。
  • advertisingIdentifier

注册APNs成功并上报DeviceToken

  • 在AppDelegate.m实现该回调方法并添加回调方法中的代码
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { /// Required - 注册 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}

实现注册APNs失败接口

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}

添加处理APNs通知回调方法

  • 在AppDelegate.m实现该回调方法并添加回调方法中的代码
// iOS 12 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{
if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//从通知界面直接进入应用
}else{
//从通知设置界面进入应用
}
} // 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 iOS 6
[JPUSHService handleRemoteNotification:userInfo];
}

成功运行

  • 真机调试该项目,如果控制台输出以下日志则代表您已经集成成功。
2016-08-19 17:12:12.745823 219b28[1443:286814]  | JPUSH | I - [JPUSHLogin]
----- login result -----
uid:5460310207
registrationID:171976fa8a8620a14a4

消息推送

参考上一篇文章(JAVA集成JPush):https://www.cnblogs.com/maggieq8324/p/11414823.html

最新文章

  1. git-入门
  2. jquery深拷贝和浅拷贝
  3. IOS 代码提示有问题
  4. cal 命令
  5. 湘潭1247 Pair-Pair(树状数组)
  6. C# - 函数参数的传递
  7. C#操作MYSQL遇到0000-00-00日期报错的原因
  8. 【linux】内核+文件系统下载到开发板
  9. OCA读书笔记(6) - 配置Oracle网络环境
  10. 2道acm简单题(2010):1.猜数字游戏;2.字符串提取数字并求和;
  11. Python内置函数(29)——slice
  12. Loadrunner测试数据库性能,测试SQL语句的脚本例子
  13. 用6个案例说明如何恢复PXC集群
  14. openstack ovs实现vlan组网
  15. mybatis 映射器(mappers) 配置说明 加载映射文件方式
  16. [LeetCode&amp;Python] Problem 746. Min Cost Climbing Stairs
  17. StringBuffer类和String类(原文地址 : http://www.cnblogs.com/springcsc/archive/2009/12/03/1616330.html)
  18. Vue项目开发之打包后背景图片路径错误的坑
  19. AWT中文译为抽象窗口工具包
  20. .Net Core集成Office Web Apps(二)

热门文章

  1. SpringBoot:如何优雅地处理全局异常?
  2. Pipeline 模型
  3. MQ服务器端和客户端通信浅谈
  4. RE最全面的正则表达式----数字篇
  5. Flutter学习笔记(16)--Scaffold脚手架、AppBar组件、BottomNavigationBar组件
  6. python变量前的单下划线(私有变量)和双下划线()
  7. Linux下Kafka下载与安装教程
  8. odoo添加顶部按钮实现自定义方法
  9. SharpMap&#160;V1.1&#160;For&#160;Web教程系列之——地图展示
  10. VScode 插件推荐与C/C++配置