TARGETS-->Capabilities-->KeyChain Sharing 設(shè)置為ON? 添加KeyChain Groups ?一般為Bundle Identifier即可
然后配置entitlements文件 ?不過(guò)一般創(chuàng)建之后都是已經(jīng)配置好的
entitlements-->Keychain Access Groups-->item0:$(AppIdentifierPrefix)com.szdtoo.situoTest
鑰匙串使用 SSKeychain 簡(jiǎn)單易用
GetUUID:
#define PLService [NSBundle mainBundle].bundleIdentifier
#define PLAccount @"$(AppIdentifierPrefix)bundleIdentifier"http://這里根據(jù)自己項(xiàng)目配置
+(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;
}