*注意这个问题不是如何在swift中注册远程通知,我的问题是如何在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
对于以下用于在Betas 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显示的代码是否有问题?
答案 0 :(得分:4)
首先,检查iOS版本号。然后,分别为每个版本设置推送通知。我确实需要清洁'我的构建文件夹以及使用opt-shift-cmd-K,因为Martin R建议解决链接错误。
这是我的最终代码:
// 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/
答案 1 :(得分:0)
我在Objective-C中有类似的工作:
if (registerForRemoteNotificationsMethodExists) {
[application registerForRemoteNotifications];
} else {
[application registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge|
UIRemoteNotificationTypeAlert|
UIRemoteNotificationTypeSound];
}
这符合Xcode 6 B5。还没试过B6。
答案 2 :(得分:0)
我相信这是在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])
}