【问题标题】:iOS7 app backward compatible with iOS5 regarding unique identifieriOS7 应用程序在唯一标识符方面向后兼容 iOS5
【发布时间】:2013-09-17 04:25:18
【问题描述】:

我的应用与 iOS5 和 iOS6 兼容。 到现在为止,我使用没有问题:

NSString DeviceID = [[UIDevice currentDevice] uniqueIdentifier];

现在使用 iOS7 和 uniqueIdentifier 不再工作,我改为:

NSString DeviceID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

问题是,这不适用于 iOS5。

如何实现与 iOS5 的向后兼容?

我试过了,没有运气:

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
    // iOS 6.0 or later
    NSString DeviceID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
#else
    // iOS 5.X or earlier
    NSString DeviceID = [[UIDevice currentDevice] uniqueIdentifier];
#endif

【问题讨论】:

  • openUDID 可能是您的合适替代品吗?
  • openUDID 是错误的答案。 blog.appsfire.com/…
  • 您不能为此使用编译时指令,您需要运行时检查。

标签: ios objective-c uniqueidentifier


【解决方案1】:

Apple 推荐的最佳选择是:

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

将它用于 5.0 以上的所有设备。

对于 5.0,您必须使用 uniqueIdentifier。检查它是否可用的最佳方法是:

if (!NSClassFromString(@"ASIdentifierManager"))

结合起来会给你:

- (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];
}

【讨论】:

  • 感谢 Grzegorz Krukowski。仍然得到:“'UIDevice' 没有可见的@interface 声明选择器'uniqueIdentifier'”错误,因为 Apple 从 UIDevice 类中删除了 uniqueIdentifier 属性。
  • 可以修改那部分:return [[UIDevice currentDevice] uniqueIdentifier];在这种情况下,对于其他一些 UID :) 要么自己生成,要么在这种情况下使用 UUID,或者获取 MACAddress - 更新我的答案
  • 好的,我找到了另一种更安全的方法来使用 respondsToSelector 和 performSelector - 这应该不会引发错误。
  • 感谢 Grzegorz Krukowski!
  • Apple 不再允许ASIdentifierManager 的用户在应用中没有任何广告。如果您使用ASIdentifierManager 而不显示横幅,您将被拒绝。
【解决方案2】:

为什么不使用 CFUUIDRef 并独立于 iOS 版本?

CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);

self.uuidString = (NSString *)CFUUIDCreateString(NULL,uuidRef);

CFRelease(uuidRef);

当然还记得在钥匙串中计算出的uuidString(以防应用程序被删除)?

Here is written how to use keychain

【讨论】:

    【解决方案3】:

    作为静态宏的硬替换,你可以尝试动态if语句来检查它。

    UIDevice 具有名为“systemVersion”的属性,您可以查看this

    【讨论】:

      猜你喜欢
      • 2010-09-18
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多