TARGETS-->Capabilities-->KeyChain Sharing 设置为ON 添加KeyChain Groups 一般为Bundle Identifier即可
然后配置entitlements文件 不过一般创建之后都是已经配置好的
entitlements-->Keychain Access Groups-->item0:$(AppIdentifierPrefix)com.szdtoo.situoTest
钥匙串使用 SSKeychain 简单易用
GetUUID:
#define PLService [NSBundle mainBundle].bundleIdentifier
#define PLAccount @"$(AppIdentifierPrefix)bundleIdentifier"//这里根据自己项目配置
+(NSString *)getUUID
{
CFUUIDRef uuid_ref=CFUUIDCreate(nil);
CFStringRef uuid_string_ref=CFUUIDCreateString(nil, uuid_ref);
CFRelease(uuid_ref);
NSString*uuid=[NSString stringWithString:(__bridge NSString * _Nonnull)(uuid_string_ref)];
CFRelease(uuid_string_ref);
NSString*devicenumber = [SSKeychain passwordForService:PLService account:PLAccount];
if(!devicenumber)
{
devicenumber = uuid;
[SSKeychain setPassword:devicenumber forService:PLService account:PLAccount];
}
return devicenumber;
}
$(AppIdentifierPrefix)的获取
- (NSString *)appIdentifierPrefix {
NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys:
kSecClassGenericPassword, kSecClass,
@"bundleSeedID", kSecAttrAccount,
@"", kSecAttrService,
(id)kCFBooleanTrue, kSecReturnAttributes,
nil];
CFDictionaryRef result = nil;
OSStatus status = SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&result);
if (status == errSecItemNotFound)
status = SecItemAdd((CFDictionaryRef)query, (CFTypeRef *)&result);
if (status != errSecSuccess)
return nil;
NSString *accessGroup = [(__bridge NSDictionary *)result objectForKey:kSecAttrAccessGroup];
NSArray *components = [accessGroup componentsSeparatedByString:@"."];
NSString *bundleSeedID = [[components objectEnumerator] nextObject];
CFRelease(result);
return bundleSeedID;
}