在 webview 中创建唯一标识符

Create a unique identifer in a webview

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

 UIDevice.currentDevice().identifierForVendor!.UUIDString

不过我想将其存储在本地以供将来使用,并在每次打开应用程序时将其发送到远程服务器。我已经对此进行了研究,并且找到了其他对象的答案,而不是网络视图。

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

更新

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

并且 url 即时消息使用

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


 }

您将需要在网络服务器端进行检查以确认您需要传入的内容 - 但如果您也在开发该端,那么您应该拥有控制权:-)

应该是这样的-请注意您不需要 swift

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

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

 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 构造它并从那里使用它。本指南帮助了我。如果那不是你所做的,请忽略 rails 部分的 ruby。

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

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

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