iOS如何获取Keychain(钥匙串)的teamID,又名AppIdentifierPrefix

在iOS中,使用Keychain可以把我们要存储的数据以加密的形式存储在独立于App的位置。

通过当前项目的Capabilities>Keychain Sharing>On开启Keychain后,会自动生成一个对应的Keychain Groups,以及一个entitlements文件,文件里会有一个Keychain Access Groups,这里存储了所有想要共享部分Keychain数据的App对应的keychain groups的ID,默认第一项名为“$(teamID).你的bundleID“。这个$(teamID)就是你开发者账号的teamID,xcode之前的版本也写做${AppIdentifierPrefix}。

那么怎么获取这个teamID,从而避免硬编码呢。示例代码如下:

+ (NSString *)teamID{
    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 = [(NSDictionary *)result objectForKey:kSecAttrAccessGroup];
    NSArray *components = [accessGroup componentsSeparatedByString:@"."];
    NSString *teamID = [[components objectEnumerator] nextObject];
    CFRelease(result);
    return teamID;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值