2017-04-17 45 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