Swift - 在将其加载到 UIWebView 之前将参数附加到 NSURL
Swift - Append parameters to NSURL before loading it to the UIWebView
我正在尝试将 deviceToken
附加到 webView loadRequest
之前的 URL 以便我可以 GET
使用 [= 的值26=] 脚本。我的问题是,保存 deviceToken 值的函数 didFailToRegisterForRemoteNotificationsWithError
最后是 fired/runs。 deviceToken
附加在 之后 loadRequest
,这不是我需要的方式。
有没有办法让它在加载前通过?
以下是我解决问题的方法:
注意:
以下代码可能不如您的代码完美。我还在开始学习 Swift 所以请原谅我 "messy" 代码。
这是我的 ViewController:
override func viewDidLoad(){
super.viewDidLoad()
//Reference to my AppDelegate
let ad = UIApplication.sharedApplication().delegate as! AppDelegate
ad.regDevTok()
loadAddressURL()
//reload
let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 5 * Int64(NSEC_PER_SEC))
dispatch_after(time, dispatch_get_main_queue()) {
var url = NSURL(string: "http://yourSite/sampleIndex.php?token=\(token)")
var requestObj = NSURLRequest(URL: url!,
cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 3.0)
self.webView.loadRequest(requestObj)
}
}
//Setting up GUI of webview
func loadAddressURL() {
var myURL = NSURL(string: "http://www.yourSite.com/sampleIndex.php")
var reqURL = NSURLRequest(URL : myURL!)
webView.loadRequest(reqURL)
NSLog("loadAddressURL")
}
这是我的 AppDelegate:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
//var token was initiated as a global variable
token = deviceToken.hexString()
}
func regDevTok(){
// Device Token
let deviceToken = defaults.objectForKey("DeviceToken") as! String?
if (deviceToken == nil) {
let type: UIUserNotificationType = [UIUserNotificationType.Badge, UIUserNotificationType.Alert, UIUserNotificationType.Sound]
let setting = UIUserNotificationSettings(forTypes: type, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(setting)
UIApplication.sharedApplication().registerForRemoteNotifications()
}
}
感谢@Ted Huinink 先生和@Mahesh 先生给我提出解决方案的想法。
我正在尝试将 deviceToken
附加到 webView loadRequest
之前的 URL 以便我可以 GET
使用 [= 的值26=] 脚本。我的问题是,保存 deviceToken 值的函数 didFailToRegisterForRemoteNotificationsWithError
最后是 fired/runs。 deviceToken
附加在 之后 loadRequest
,这不是我需要的方式。
有没有办法让它在加载前通过?
以下是我解决问题的方法:
注意:
以下代码可能不如您的代码完美。我还在开始学习 Swift 所以请原谅我 "messy" 代码。
这是我的 ViewController:
override func viewDidLoad(){
super.viewDidLoad()
//Reference to my AppDelegate
let ad = UIApplication.sharedApplication().delegate as! AppDelegate
ad.regDevTok()
loadAddressURL()
//reload
let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 5 * Int64(NSEC_PER_SEC))
dispatch_after(time, dispatch_get_main_queue()) {
var url = NSURL(string: "http://yourSite/sampleIndex.php?token=\(token)")
var requestObj = NSURLRequest(URL: url!,
cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 3.0)
self.webView.loadRequest(requestObj)
}
}
//Setting up GUI of webview
func loadAddressURL() {
var myURL = NSURL(string: "http://www.yourSite.com/sampleIndex.php")
var reqURL = NSURLRequest(URL : myURL!)
webView.loadRequest(reqURL)
NSLog("loadAddressURL")
}
这是我的 AppDelegate:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
//var token was initiated as a global variable
token = deviceToken.hexString()
}
func regDevTok(){
// Device Token
let deviceToken = defaults.objectForKey("DeviceToken") as! String?
if (deviceToken == nil) {
let type: UIUserNotificationType = [UIUserNotificationType.Badge, UIUserNotificationType.Alert, UIUserNotificationType.Sound]
let setting = UIUserNotificationSettings(forTypes: type, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(setting)
UIApplication.sharedApplication().registerForRemoteNotifications()
}
}
感谢@Ted Huinink 先生和@Mahesh 先生给我提出解决方案的想法。