[英]How to call registerForRemoteNotifications for both iOS7 and iOS8 in Swift?
*請注意,此問題不是如何快速注冊遠程通知,我的問題是如何快速編寫可在同時運行iOS8和iOS7的設備上運行的代碼。 我發布的代碼以前在Xcode beta 1至5中使用,但在beta 6中現在生成了鏈接器錯誤。 所以我的問題是如何更改內容以解決Beta 6中的新鏈接器錯誤。
Xcode Beta 6出現以下鏈接錯誤
Undefined symbols for architecture arm64:
"__TFSsoi1oUSs17_RawOptionSetType_USs21BitwiseOperationsTypeSs9Equatable__FTQ_Q__Q_", referenced from:
__TFC12My_cWireless11AppDelegate29registerForRemoteNotificationfS0_FT_T_ in AppDelegate.o
對於以下用於在Beta 1到5上進行編譯/鏈接/執行而不會出現問題的代碼。
func registerForRemoteNotification()
{
let registerForRemoteNotificationsMethodExists = UIApplication.sharedApplication().respondsToSelector(Selector("registerForRemoteNotifications"))
if registerForRemoteNotificationsMethodExists
{
UIApplication.sharedApplication()?.registerForRemoteNotifications()
}
else
{
// Fall back to using iOS7 as the code is not running on an iOS 8 device
UIApplication.sharedApplication()?.registerForRemoteNotificationTypes(UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound | UIRemoteNotificationType.Alert)
}
}
為什么它停止了與最新Beta的鏈接? Xcode Beta 6中揭示的代碼是否存在問題?
首先,檢查iOS版本號。 然后,分別為每個版本設置推送通知。 正如馬丁R建議解決鏈接錯誤一樣,我也必須使用opt-shift-cmd-K“清理”我的構建文件夾。
這是我的最終代碼:
// Check to see if this is an iOS 8 device.
let iOS8 = floor(NSFoundationVersionNumber) > floor(NSFoundationVersionNumber_iOS_7_1)
if iOS8 {
// Register for push in iOS 8
let settings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
} else {
// Register for push in iOS 7
UIApplication.sharedApplication().registerForRemoteNotificationTypes(UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound | UIRemoteNotificationType.Alert)
}
檢查iOS版本的參考-http: //www.andrewcbancroft.com/2014/09/17/swift-ios-version-check/
我在Objective-C中有類似的工作:
if (registerForRemoteNotificationsMethodExists) {
[application registerForRemoteNotifications];
} else {
[application registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge|
UIRemoteNotificationTypeAlert|
UIRemoteNotificationTypeSound];
}
這符合Xcode 6 B5。 還沒嘗試過B6。
我相信這是在Swift 2中檢查iOS版本的推薦方法。
if #available(iOS 8.0, *) {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil))
} else {
application.registerForRemoteNotificationTypes([UIRemoteNotificationType.Alert, UIRemoteNotificationType.Badge, UIRemoteNotificationType.Sound])
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.