ios - 如何在 Swift 中为 iOS7 和 iOS8 调用 registerForRemoteNotifications?

标签 ios swift xcode6-beta6

* 请注意,这个问题不是如何在 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

对于以下用于在 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 版本号。然后,分别为每个版本设置推送通知。我确实必须按照 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/

关于ios - 如何在 Swift 中为 iOS7 和 iOS8 调用 registerForRemoteNotifications?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25409867/

相关文章:

ios - 为什么堆栈展开在 CFRunLoopRunSpecific 处停止

xcode - 类型 'AnyObject' 不符合协议(protocol) 'SequenceType'

swift - 根据帐户凭据复制 Storyboard View

ios - 如何在 iOS 中制作自定义对话框屏幕?

Swift:如何访问 UITableView 中的单元格

ios - UICollectionView 没有出现在模拟器中

xcode - 在设备上解包时出现 sendSynchronousRequest 错误,在模拟器中有效

ios - iPad 版 iOS App

ios - iOS LAPolicyDeviceOwnerAuthentication“取消”按钮

ios - swift : "Attempt to present UIAlertController whose view is not in the window hierarchy!"