gpt4 book ai didi

ios - 为什么我无法从 swift5 中的模拟器获取设备 token ?

转载 作者:行者123 更新时间:2023-11-29 05:26:54 25 4
gpt4 key购买 nike

我正在使用 swift 开发 iOS。我的问题是我无法从模拟器获取设备 token 值。这是正常的吗?我必须做一个推送测试。推送测试只能在物理设备上进行吗?

AppDelegate

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
// Override point for customization after application launch.
//create the notificationCenter
let center = UNUserNotificationCenter.current()
center.delegate = self
// set the type as sound or badge
center.requestAuthorization(options: [.sound,.alert,.badge]) { (granted, error) in
// Enable or disable features based on authorization
}
application.registerForRemoteNotifications()
return true
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.map{ String(format: "%02x", $0) }.joined()
Log.Info("Registration succeeded!")
Log.Info("Token: \(token)")
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
Log.Warning("Registration failed!")
}

最佳答案

首先,您无法在模拟器上测试推送通知。为了测试推送通知,您需要物理设备。

模拟器不提供设备 token 。只有物理设备在通知的委托(delegate)方法中提供设备 token 。

关于ios - 为什么我无法从 swift5 中的模拟器获取设备 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58073381/

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