iOS-实现多个app共享keychain功能

最近开发中遇到这么一个问题:公司多个app使用的是同一套账户数据库而且都实现游客账号功能。

先分解下需求:首先是游客账号,我们是要将获取的用户设备唯一标识关联为我们数据库中的一个游客账号,但因为苹果在iOS5之后就不允许获取UDID (「Unique Device Identifier Description」是由子母和数字组成的40个字符串的序号,用来区别每一个唯一的iOS设备);其次呢,是多个app间使用同一套账户数据库,也就是要保证我们多个app在同一设备上,只有一个游客账户存在我们的数据库中.


说下我的做法:

获取用户设备的IDFV(identifierForVendor,是给Vendor供应商标识用户用的,每个设备在所属同一个Vender的应用里,都有相同的值)并保存到keychain钥匙串(keychain中的信息不会因为卸载/重装app而丢失),然后发送到数据库进行注册,这样就获得了游客账号.因为keychain钥匙串是独立于app之外的,所以我们可以使用app后台中的keychain sharing,指定group共享组来实现多个app共享keychain中的某个共同数据,这样的结果就是只要一个app注册/使用过了游客账号功能,之后再使用其他app,都会先获得keychain钥匙串中保存的IDFV值,也就是同一个游客账号了.

注意:再解释下IDFV,其中的Vender是指应用提供商,但准确点说,是通过BundleID的反转的前两部分进行匹配,如果相同就是同一个Vender,例如对于com.taobao.app1, com.taobao.app2 这两个BundleID来说,就属于同一个Vender,共享同一个idfv的值。com.taobao.app1和com.baidu.app1就属于两个Vender.如果用户将属于此Vender的所有App卸载,则idfv的值会被重置,即再重装此Vender的App,idfv的值和之前不同。


具体方法:

1.去targets中开启keychain sharing功能,并指定keychain group.

注意:需要共享keychain的多个app必须添加相同的group


iOS-实现多个app共享keychain功能_第1张图片

2.获取用户设备的IDFV并保存到keychain钥匙串中,保存信息到钥匙串.我是使用的UICKeyChainStore第三方框架

代码:

#pragma mark - app_login游客登录

-(NSString*)getUUID{

// 获取app组共享中的keychain

NSString *perfix = [self bundleSeedID];

NSString *groupString = [NSString stringWithFormat:@"%@.com.zk120.aportal",perfix];

NSString *deviceId = [UICKeyChainStore stringForKey:@"deviceId" service:@"Devices" accessGroup:groupString];

if (deviceId == nil) {

deviceId = [[UIDevice currentDevice].identifierForVendor UUIDString];

[UICKeyChainStore setString:deviceId forKey:@"deviceId" service:@"Devices" accessGroup:groupString];

}

return deviceId;

}

// 获取AppIdentifierPrefix

- (NSString *)bundleSeedID {

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;

}

这样就搞定了!

你可能感兴趣的:(iOS-实现多个app共享keychain功能)