1

* 请注意,这个问题不是如何在 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 显示的代码有问题吗?

4

3 回答 3

4

首先,检查 iOS 版本号。然后,分别为每个版本设置推送通知。正如 Martin 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/

于 2014-10-16T03:37:50.940 回答
0

我在 Objective-C 中有类似的工作:

if (registerForRemoteNotificationsMethodExists) {
        [application registerForRemoteNotifications];
    } else {
        [application registerForRemoteNotificationTypes:
         UIRemoteNotificationTypeBadge|
         UIRemoteNotificationTypeAlert|
         UIRemoteNotificationTypeSound];
    }

这符合 Xcode 6 B5。还没试过B6。

于 2014-08-20T19:57:02.673 回答
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])
    }
于 2016-08-24T14:57:09.520 回答