Search code examples
objective-cregexcocoaudid

How can I recognize whether a string is a UDID for an iOS device?


I'm working on an iOS Cocoa app that has a fallback - if the user doesn't enter a certain piece of data, the device's UDID is used instead, as a default. I need to add a check in our server code (written in Ruby) to recognize whether the value being sent up is a UDID (the user's default) or a custom string that they've set.

It seems that the right solution to my problem is a regular expression. I'm comfortable writing regex'es, but I need to be certain that this regex is 100% guaranteed to recognize a UDID.

I retrieve the UDID in code using:

[[UIDevice currentDevice] uniqueIdentifier]

And in the simulator, I get back this value:

6938CA7D-ECE2-53A4-B293-961A8D07AFB1

From this I might infer that I can just search for a string of hex characters that matches the pattern 8-4-4-4-12. But I want to be certain this works for every UDID.

I can't find anything about this in Apple's documentation and was wondering if anyone could give me a definitive answer to this question... thanks!


Solution

  • Why not send another bit of data indicating that it is a UDID? Or just use a different parameter name (UDID=6938...).

    Then, in your server, you can test it like so if you really want to:

    # somewhere outside the controller action, maybe the top of the file
    UDID_PATTERN = /\A[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}\Z/
    
    # inside the controller action you're sending info to
    udid = params[:UDID]
    if udid && (udid =~ UDID_PATTERN)
      # do something with the UDID in place of the other
      # bit of info they could have provided
    end