在Webview中创建唯一标识

时间:2016-02-29 18:34:39

标签: ios swift uiwebview

对我轻松我是I OS和Swift的新手:)。我正在尝试使用swift创建一个IOS应用程序。我有一个正常工作的Web视图显示,显示网站。好极了!!  我现在需要做的是创建一个本地存储的唯一标识符,并在打开应用程序时将其发送到远程服务器。我知道我可以用这个......

 UIDevice.currentDevice().identifierForVendor!.UUIDString

但是,我希望将其存储在本地以备将来使用,并在每次打开应用程序时将其发送到远程服务器。我已对此进行了研究,并为其他对象提供了答案,而不仅仅是Web视图。

如果有人知道此解决方案的教程或示例代码,我将非常感激。

更新

  let uuid = UIDevice.currentDevice().identifierForVendor!.UUIDString

和使用

的网址
  let url= NSURL (string:"https://example.com");

我可以这样做吗?或者喜欢它?

let url= NSURL (string:"https://example.com");

  let requestobj= NSURLRequest(URL:url! ADD VAR HERE? );

其中ADD VAR HERE是传递给服务器的uuid,我可以用php脚本捕获它?

最新更新..

我很难将其集成到现有代码中。放在哪里最好的地方?

 import UIKit


class ViewController: UIViewController {

let uuid = UIDevice.currentDevice().identifierForVendor!.UUIDString

@IBOutlet weak var WebView: UIWebView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

           let url = NSURL (string: "https://example.com");
    let requestObj = NSURLRequest(URL: url?)

    WebView.loadRequest(requestObj);
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


 }

3 个答案:

答案 0 :(得分:1)

这是我正在寻找的答案。谢谢大家的帮助!

 let device_uuid =  UIDevice.currentDevice().identifierForVendor!.UUIDString

 let api_host = "https://example.com?uuid=" + device_uuid
    let url = NSURL(string: api_host)
    let req = NSURLRequest(URL: url!)
    WebView.loadRequest(req);

显然我需要做的是将我的URL构建到变量中。然后我可以使用NSURL构建它并从那里使用它。这个指南帮助了我。如果那不是你正在做的事情,请忽略轨道部分的红宝石。

http://ericlondon.com/2015/12/09/sending-messages-between-a-swift-webview-and-a-rails-backend-using-javascript.html

答案 1 :(得分:0)

您需要检查网络服务器端以确认您需要传递的确切内容 - 但如果您正在开发该方面,那么您应该控制: - )

应该是这样的 - 请注意,你不需要;快速

let request= NSURLRequest(URL:url)
var bodyData = "myUUID=\(uuid)&otherData=value1"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);

答案 2 :(得分:0)

请记住,如果用户卸载应用程序,此标识符将会更改。如果您需要保留它,那么我建议将它存储在钥匙串上,这样即使卸载了应用程序,同一部手机的ID也始终相同。

检查另一个问题:How to preserve identifierForVendor in ios after uninstalling ios app on device?