分享

获取IOS唯一设备标识码

 ____YY____ 2015-12-24

获取IOS唯一设备标识码

 

最开始使用的是苹果官方KeychainItemWrapper,下载地址为https://developer.apple.com/library/ios/samplecode/GenericKeychain/Listings/Classes_KeychainItemWrapper_m.html,使用过程中,会有报错*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn't add the Keychain Item.',在网上没有找到合适的解决方法,最终放弃这种方式

 

现在使用的是SSKeyChains,下载地址为项目地址:https://github.com/samsoffes/sskeychain

 

步骤:

  1. 在工程中加入Security.framework框架。
  2. SSKeychain.hSSKeychain.m加到项目文件夹。
  3. 获取UUID(使用了ARC模式,非ARC模式的代码文件加入 -fno-objc-arc标签)

UUID.h

#import <Foundation/Foundation.h>

 

@interface UUID : NSObject

 

+ (NSString *) gen_uuid;

 

@end

UUID.m

#import "UUID.h"

 

@implementation UUID

 

+(NSString *) gen_uuid

{

    CFUUIDRef uuid_ref=CFUUIDCreate(nil);

    CFStringRef uuid_string_ref=CFUUIDCreateString(nil, uuid_ref);

    CFRelease(uuid_ref);

    NSString *uuid=[NSString stringWithString:uuid_string_ref];

    CFRelease(uuid_string_ref);

    return uuid;

}

@end

 

  1. 获取UUID,保存在钥匙串中(蓝色部分为标识,最好为app唯一标识,通过此标识查找保存的钥匙串

#define kService [NSBundle mainBundle].bundleIdentifier

#define kAccount @"K626P85663.com.tzd.mofi2"

 

    //获取设备唯一码

    if (![SSKeychain passwordForService:kService account:kAccount])

    {

        NSString *uuid = [UUID gen_uuid];

        [SSKeychain setPassword:uuid forService:kService account:kAccount];

    }

   

    NSString *devicenumber = [SSKeychain passwordForService:kService account:kAccount];



注意(已下全部验证):

(1)   app卸载重新安装,保存的唯一码不改变

(2)   IOS系统升级,保存的唯一码不改变

(3)   通用—>还原—>还原所有设置,保存的唯一码不改变

(4)   通用—>还原—>抹掉所有内容和设置,保存的唯一码也被抹掉

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多