iOS7 app backward compatible with iOS5 regarding unique identifier

后端 未结 3 769
天命终不由人
天命终不由人 2020-12-06 02:35

My app is compatible with iOS5 and iOS6. Until now I had no problem using:

NSString DeviceID = [[UIDevice currentDevice] uniqueIdentifier];

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-06 02:59

    The best and recommend option by Apple is:

     NSString *adId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    

    Use it for every device above 5.0.

    For 5.0 you have to use uniqueIdentifier. The best to check if it's available is:

    if (!NSClassFromString(@"ASIdentifierManager"))
    

    Combining that will give you:

    - (NSString *) advertisingIdentifier
    {
        if (!NSClassFromString(@"ASIdentifierManager")) {
            SEL selector = NSSelectorFromString(@"uniqueIdentifier");
            if ([[UIDevice currentDevice] respondsToSelector:selector]) {
                return [[UIDevice currentDevice] performSelector:selector];
            }
            //or get macaddress here http://iosdevelopertips.com/device/determine-mac-address.html
        }
        return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    }
    

提交回复
热议问题