我正在尝试为对讲推送通知注册一个应用。他们在这里有说明:https://docs.intercom.io/Install-on-your-mobile-product/enabling-push-notifications-with-intercom-for-ios
他们在obj c中提供此代码,但我的应用程序很快,这对我来说看起来像胡言乱语:
- (void)applicationDidBecomeActive:(UIApplication *)application {
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]){ // iOS 8 (User notifications)
[application registerUserNotificationSettings:
[UIUserNotificationSettings settingsForTypes:
(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil]];
[application registerForRemoteNotifications];
} else { // iOS 7 (Remote notifications)
[application registerForRemoteNotificationTypes:
(UIRemoteNotificationType)
(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
}
}
有人可以解释如何在swift中执行此过程吗?
答案 0 :(得分:1)
以下代码通过使用添加到Swift 2的新可用性功能在Xcode 7.1上运行
if #available(iOS 8, *) {
application.registerUserNotificationSettings(
UIUserNotificationSettings(forTypes: UIUserNotificationType(rawValue: UIUserNotificationType.Badge.rawValue |
UIUserNotificationType.Sound.rawValue |
UIUserNotificationType.Alert.rawValue),
categories: nil))
} else {
application.registerForRemoteNotificationTypes(
UIRemoteNotificationType(rawValue: UIRemoteNotificationType.Badge.rawValue |
UIRemoteNotificationType.Alert.rawValue |
UIRemoteNotificationType.Sound.rawValue))
}