Airship iOS SDK
Airship iOS SDK 用于 iOS 应用程序简单集成 Airship 服务。
资源
安装
所有项目和静态库都需要 Xcode 11.0+,并且项目必须针对 >= iOS 11。
CocoaPods
请确保已安装 CocoaPods 依赖项管理器。您可以通过执行以下命令来安装:
$ gem install cocoapods
在您的 podfile
中指定 UrbanAirship-iOS-SDK,并选择性地添加 UrbanAirship-iOS-Location pods 使用 use_frameworks! 选项
use_frameworks!
# Airship SDK
target "<Your Target Name>" do
pod 'UrbanAirship-iOS-SDK'
# Optional: uncomment to install AirshipLocationKit
# pod 'UrbanAirship-iOS-Location'
end
使用以下命令进行安装
$ pod install
为了利用iOS 10的通知附件,您需要在主应用旁边创建一个通知服务扩展。大部分工作已经为您完成,但需要创建一个新的目标,因此有一些额外的步骤。首先创建一个新的“通知服务扩展”目标,然后将UrbanAirship-iOS-AppExtensions添加到新的目标中。
use_frameworks!
# Airship SDK
target "<Your Service Extension Target Name>" do
pod 'UrbanAirship-iOS-AppExtensions'
end
使用以下命令进行安装
$ pod install
然后删除新扩展的所有空白源代码,让它从UAMediaAttachmentExtension继承。
import AirshipAppExtensions
class NotificationService: UAMediaAttachmentExtension {
}
Carthage
请确保已安装Carthage。可以使用Homebrew的以下命令安装:
$ brew update
$ brew install carthage
在cartfile中指定Airship iOS SDK。
github "urbanairship/ios-library"
在cartfile所在的目录中执行以下命令:
$ carthage update
为了利用通知附件,您需要在主应用旁边创建一个通知服务扩展。大部分工作已经为您完成,但需要创建一个新的目标,这是一个额外的步骤。
- 在Xcode中创建一个新的iOS目标,选择“通知服务扩展”类型。
- 将新的AirshipAppExtensions.framework拖到您的应用程序项目中。
- 在扩展的构建阶段链接AirshipAppExtensions.framework。
- 为AirshipAppExtensions.framework添加一个Copy Files阶段,并将目的地选为“Frameworks”。
- 删除新扩展的所有空白源代码。
- 在NotificationService中从
UAMediaAttachmentExtension
继承。
其他安装方法
关于其他安装方法,请参阅 get started guide 入门指南。
快速开始
关于位置的重要提示
2019年春季,苹果开始拒绝那些在其 Info.plist
文件中没有提供使用说明的应用程序使用或看似使用 Core Location 服务。在 Airship SDK 11 中,所有指向 CoreLocation 的引用都已从核心库中移除,并被放置在一个独立的定位模块中。无需使用定位服务的开发者可以继续使用 AirshipKit,但对于使用 UALocation
类的开发者,请参阅 定位 部分的更新配置说明。
警告
截至 SDK 10.2 及苹果当前的 App Store 审核政策,若 AirshipKit 中的应用程序在 Info.plist
文件中没有提供定位使用描述,则很可能会被拒绝。如果不需要定位服务,最简单的方法是使用 Airship SDK 11 或更高版本。如果正在构建针对先前 Airship SDK 的应用程序,需要在 NSLocationAlwaysUsageDescription
、NSLocationWhenInUseUsageDescription
和 NSLocationAlwaysAndWhenInUseUsageDescription
键下向您的 Info.plist
文件中添加使用描述字符串。
功能
在主要应用程序目标的功能部分启用推送通知和远程通知后台模式。
添加 Airship 配置文件
该库使用名为 AirshipConfig.plist
的 .plist 配置文件来管理您的生产环境和开发应用程序配置文件。这个文件的示例副本可以在所有样例项目中找到。将此文件放在您的项目中,并根据您在 http://go.urbanairship.com 中的应用程序设置以下值。要查看所有可能的键和值,请参阅 UAConfig 类参考。
您还可以将该文件作为纯文本编辑。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>detectProvisioningMode</key>
<true/>
<key>developmentAppKey</key>
<string>Your Development App Key</string>
<key>developmentAppSecret</key>
<string>Your Development App Secret</string>
<key>productionAppKey</key>
<string>Your Production App Key</string>
<key>productionAppSecret</key>
<string>Your Production App Secret</string>
</dict>
</plist>
当将 detectProvisioningMode
设置为 true
时,库将自动检测生产模式。
高级用户可以在此 .plist 文件中添加脚本或预处理逻辑,以基于构建类型自动从开发环境切换到生产密钥。
调用起飞
要启用推送通知,您需要对应用程序代理进行一些修改。
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Your other application code.....
// Set log level for debugging config loading (optional)
// It will be set to the value in the loaded config upon takeOff
[UAirship setLogLevel:UALogLevelTrace];
// Populate AirshipConfig.plist with your app's info from https://go.urbanairship.com
// or set runtime properties here.
UAConfig *config = [UAConfig defaultConfig];
// You can then programmatically override the plist values:
// config.developmentAppKey = @"YourKey";
// etc.
// Call takeOff (which creates the UAirship singleton)
[UAirship takeOff:config];
// Print out the application configuration for debugging (optional)
UA_LDEBUG(@"Config:\n%@", [config description]);
// Set the icon badge to zero on startup (optional)
[[UAirship push] resetBadge];
// User notifications will not be enabled until userPushNotificationsEnabled is
// set YES on UAPush. Once enabled, the setting will be persisted and the user
// will be prompted to allow notifications. You should wait for a more appropriate
// time to enable push to increase the likelihood that the user will accept
// notifications.
// [UAirship push].userPushNotificationsEnabled = YES;
return YES;
}
要启用应用中的推送功能
// Somewhere in the app, this will enable push (setting it to NO will disable push,
// which will trigger the proper registration or de-registration code in the library).
[UAirship push].userPushNotificationsEnabled = YES;
SDK 开发
确保您已安装 CocoaPods 依赖管理器。您可以通过执行以下命令来实现
$ gem install cocoapods
安装 pods
$ pod install
打开 Airship.xcworkspace
$ open Airship.xcworkspace
更新 Samples AirshipConfig,通过将 AirshipConfig.plist.sample
复制到 AirshipConfig.plist
并更新应用的凭证。现在您应该能够构建、运行测试和运行示例。
可以通过运行 build.sh 脚本来生成发行版
./scripts/build.sh
持续集成在每次 PR 递交时都会运行 scripts/run_ci_tasks.sh