1

Go easy on me I am new to I OS and Swift :). I am trying to create a IOS app using swift. I have a web view display that is working correctly, displaying the website. YAY!! What I need to do now is create a unique identifier that is stored locally and when the app is opened is sent to the remote server. I see i can use this...

 UIDevice.currentDevice().identifierForVendor!.UUIDString

However i would like to store it locally for future use and send it to the remote server every time the app is opened. I have done research on this and have come upon answers for other objects just not a web view.

If someone knows of a tutorial or example code for this solution i would greatly appreciate it.

UPDATE

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

and for the url im using

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

Could i do something like this? Or like it?

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

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

Where ADD VAR HERE is the uuid to pass to the server which i can catch with a php script?

Latest update..

Im having a hard time integrating that into my existing code. Where would be the best place to put it?

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


 }
megphn
  • 63
  • 7
  • why would you need to store this locally? It's always going to be the same value that you get for this device, so you can get it as you've shown – Russell Feb 29 '16 at 19:12
  • Russell, Thanks for clearing that up. So i have updated my question above. will that work? Also what would be the best way to add it into the NSURL? – megphn Feb 29 '16 at 19:21
  • That code will work and it will produce a `String`. As to how to best add it, that depends on how the server you're sending it to expects it to be encoded. – sschale Feb 29 '16 at 19:28
  • Could you point me to the correct syntax i would use? – megphn Feb 29 '16 at 19:32

3 Answers3

1

Here is the answer i was looking for. Thanks for your help everyone!

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

Apparently what i needed to do was build my URL into a variable. Then i can structure it using the NSURL and use it from there. This guide helped me. Just ignore the ruby on rails part if that's not what your doing.

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

megphn
  • 63
  • 7
0

You will need to check on the webserver side to confirm exactly what you need to pass in - but if you are developing that side as well, then you should have control :-)

Should be something like this - please not that you don't need ; in swift

let request= NSURLRequest(URL:url)
var bodyData = "myUUID=\(uuid)&otherData=value1"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
Russell
  • 5,436
  • 2
  • 19
  • 27
  • I am developing the server side too. Thanks for the input! This thread has helped me to get where I am going! – megphn Mar 02 '16 at 03:20
0

Keep in mind that this identifier will change if a user uninstalls the application. If you need to persist it then I'd recommend to store it on the keychain so the id is always the same for the same phone even if the app is uninstalled.

Check this other question: How to preserve identifierForVendor in ios after uninstalling ios app on device?

Community
  • 1
  • 1