UIDevice uniqueIdentifier deprecated - What to do now?

后端 未结 30 2729
日久生厌
日久生厌 2020-11-21 06:07

It has just come to light that the UIDevice uniqueIdentifier property is deprecated in iOS 5 and unavailable in iOS 7 and above. No alternative method or pr

30条回答
  •  孤街浪徒
    2020-11-21 06:28

    A working way to get UDID:

    1. Launch a web server inside the app with two pages: one should return specially crafted MobileConfiguration profile and another should collect UDID. More info here, here and here.
    2. You open the first page in Mobile Safari from inside the app and it redirects you to Settings.app asking to install configuration profile. After you install the profile, UDID is sent to the second web page and you can access it from inside the app. (Settings.app has all necessary entitlements and different sandbox rules).

    An example using RoutingHTTPServer:

    import UIKit
    import RoutingHTTPServer
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
        var bgTask = UIBackgroundTaskInvalid
        let server = HTTPServer()
    
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            application.openURL(NSURL(string: "http://localhost:55555")!)
            return true
        }
    
        func applicationDidEnterBackground(application: UIApplication) {
            bgTask = application.beginBackgroundTaskWithExpirationHandler() {
                dispatch_async(dispatch_get_main_queue()) {[unowned self] in
                    application.endBackgroundTask(self.bgTask)
                    self.bgTask = UIBackgroundTaskInvalid
                }
            }
        }
    }
    
    class HTTPServer: RoutingHTTPServer {
        override init() {
            super.init()
            setPort(55555)
            handleMethod("GET", withPath: "/") {
                $1.setHeader("Content-Type", value: "application/x-apple-aspen-config")
                $1.respondWithData(NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("udid", ofType: "mobileconfig")!)!)
            }
            handleMethod("POST", withPath: "/") {
                let raw = NSString(data:$0.body(), encoding:NSISOLatin1StringEncoding) as! String
                let plistString = raw.substringWithRange(Range(start: raw.rangeOfString("")!.endIndex))
                let plist = NSPropertyListSerialization.propertyListWithData(plistString.dataUsingEncoding(NSISOLatin1StringEncoding)!, options: .allZeros, format: nil, error: nil) as! [String:String]
    
                let udid = plist["UDID"]! 
                println(udid) // Here is your UDID!
    
                $1.statusCode = 200
                $1.respondWithString("see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/iPhoneOTAConfiguration/ConfigurationProfileExamples/ConfigurationProfileExamples.html")
            }
            start(nil)
        }
    }
    

    Here are the contents of udid.mobileconfig:

    
    
    
        
            PayloadContent
            
                URL
                http://localhost:55555
                DeviceAttributes
                
                    IMEI
                    UDID
                    PRODUCT
                    VERSION
                    SERIAL
                
            
            PayloadOrganization
            udid
            PayloadDisplayName
            Get Your UDID
            PayloadVersion
            1
            PayloadUUID
            9CF421B3-9853-9999-BC8A-982CBD3C907C
            PayloadIdentifier
            udid
            PayloadDescription
            Install this temporary profile to find and display your current device's UDID. It is automatically removed from device right after you get your UDID.
            PayloadType
            Profile Service
        
    
    

    The profile installation will fail (I didn't bother to implement an expected response, see documentation), but the app will get a correct UDID. And you should also sign the mobileconfig.

提交回复
热议问题