Ios 在加载webview之前,如何获取设备id和设备令牌?
使用swift 3在Xcode 8上运行 下面是我的ViewController.swiftIos 在加载webview之前,如何获取设备id和设备令牌?,ios,swift,xcode,Ios,Swift,Xcode,使用swift 3在Xcode 8上运行 下面是我的ViewController.swift class ViewController: UIViewController { @IBOutlet weak var TestWebview: UIWebView! override func viewDidLoad() { super.viewDidLoad() let deviceid = UIDevice.current.identifierF
class ViewController: UIViewController {
@IBOutlet weak var TestWebview: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let deviceid = UIDevice.current.identifierForVendor!.uuidString
//I want to pass device id and device token to this testURL
let testURL = URL(string: "http://www.test.com/user.html?device=ios&deviceid=" + deviceid + "&devicetoken=")
let testURLRequest = URLRequest(url: testURL!)
TestWebview(testURLRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
下面是我的AppDelegate.swift文件
import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .badge], completionHandler: { granted, error in
if granted {
print("Yes。")
}
else {
print("No!")
}
})
application.registerForRemoteNotifications()
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
print("===== deviceTokenString =====")
print(deviceTokenString)
}
}
现在,我可以在ViewController中获取设备id,也可以在AppDelegate文件中获取设备令牌
但是,如何将设备令牌传递给ViewController中的testURL,或者如何将设备id和设备令牌传递给testURL?使
DeviceTokennstring
成为AppDelegate
类中的属性
class AppDelegate: UIResponder, UIApplicationDelegate {
...
var deviceTokenString: String?
...
...
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// feed your property
deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
print("===== deviceTokenString =====")
print(deviceTokenString)
}
}
现在,您可以从任何一个控制器获得设备Tokensting
如下所示:
override func viewDidLoad() {
super.viewDidLoad()
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
guard let deviceTokenString = appDelegate.deviceTokenString else {
// deviceTokenString isn't available
return
}
// deviceTokenString is available here
let deviceid = UIDevice.current.identifierForVendor!.uuidString
// I want to pass device id and device token to this testURL
// Concat various types into string like below
let testURL = URL(string: "http://www.test.com/user.html?device=ios&deviceid=\(deviceid)&devicetoken=\(deviceTokenString)")
let testURLRequest = URLRequest(url: testURL!)
TestWebview(testURLRequest)
}
创建一个名为
deviceTokenString
的属性。然后在使用reduce
方法的地方输入此属性。然后从任何视图控制器中,获取AppDelegate
实例并访问devicetokensting
。(即:guard让appDelegate=UIApplication.shared.delegate作为?appDelegate else{return}让deviceTokenString=appDelegate.deviceTokenString
)谢谢!但是我无法获取我的deviceTokenString,它运行到else块。您是否从AppDelegate获取这些行的日志:print(==deviceTokenString===”)print(deviceTokenString)
?感谢您的回复。是的,在我的调试区域中,最后一行是====deviceTokenString====和我的设备令牌,但它似乎将运行guard让deviceTokenString=appDelegate.deviceTokenString else{print(“此处首先”)返回},然后使用我的设备令牌打印===deviceTokenString=,我想这就是为什么我不能在view controller.swift中获取设备令牌的原因