gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:24:31 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com