1.1很多时候,我们需要拿到手机唯一识别号来进行网络请求处理风控,下面介绍一个拿到手机唯一识别号的方法
1.1.2 首先将三方库SAMKeychain导入到项目中,然后新建工具类,提供一个类方法
+ (NSString *) generateUUID {
NSString *uuid = [WXTool getDeviceId];
if (![NSString isNullOrEmpty:uuid]) {
return uuid;
}
NSError *error;
SAMKeychainQuery *query = [[SAMKeychainQuery alloc] init];
uuid = [[NSUUID UUID] UUIDString];
// New item
query = [[SAMKeychainQuery alloc] init];
query.password = uuid;
query.service = kSSKeychainServiceName;
query.account = kSSKeychainAccountName;
query.label = kSSKeychainLabel;
if (![query save:&error]) {
DLog(@"Unable to save item: %@", error);
}
return uuid;
}
+ (NSString *)getDeviceId {
NSError *error;
// Look up
SAMKeychainQuery *query = [[SAMKeychainQuery alloc] init];
query.service = kSSKeychainServiceName;
query.account = kSSKeychainAccountName;
query.password = nil;
[query fetch:&error];
if (error) {
DLog(@"Network -- Unable to fetch keychain item: %@", error);
}
NSString *password = query.password;
if ([NSString isNullOrEmpty:password]) {
password = @"";
}
return password;
}
网友评论