2017-04-17 90 views
1

与SWIFT 3如何在webview加载之前获取设备标识和设备标记?

下面运行在Xcode中8是我ViewController.swift

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) 
    } 

} 

现在,我可以在视图控制器获取设备ID和也可以在AppDelegate文件中获取设备令牌。

但是,如何将设备令牌传递给ViewController中的testURL,或者如何将设备标识和设备令牌传递给testURL?

+0

将名为'deviceTokenString'财产。然后在你使用'reduce'方法的地方加入这个属性。然后从任何视图控制器,获取'AppDelegate'实例并访问'deviceTokenString'。 (即:'guard let appDelegate = UIApplication.shared.delegate as?AppDelegate else { return } let deviceTokenString = appDelegate.deviceTokenString') – nayem

回答

1

品牌deviceTokenString属于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) 
    } 
} 

,从任何一个控制器,你可以得到你的deviceTokenString,如:

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) 
} 
+0

欣赏!但是我无法得到我的deviceTokenString并且它运行到其他块。 – Dreams

+0

您是否正在从_AppDelegate_获取这些行的日志:print(“===== deviceTokenString =====”)print(deviceTokenString)'? – nayem

+0

感谢您的回复。是的,在我的调试区域中,最后一行是 ===== deviceTokenString =====和我的设备标记 – Dreams