ios-唯一标识符及Keychain共享


title: ios-唯一标识符及Keychain共享
categories: Ios
tags: [ios, 唯一标识符, UDID, Keychain]
date: 2021-02-13 14:50:38
comments: false
mathjax: true
toc: true

ios-唯一标识符及Keychain共享


前篇

iOS7中已经完全的禁用了 UDID, 推荐使用 [IDFA & IDFV ](#IDFA & IDFV )

结合 Keychain 持久化的特性, 达到 唯一标识符 的持久可用性.


唯一标识符

  • 貌似比较多的是建议 使用KeyChain来提升持久性
  • 史上最全的iOS各种设备信息获取总结(iPhone 12已更新) - https://www.jianshu.com/p/b23016bb97af
  • iOS唯一设备ID - https://www.cnblogs.com/kekec/p/12560850.html
  • iOS - 广告标识符(IDFA & IDFV) - https://www.jianshu.com/p/8e2846dc8a03

UDID

UDID(Unique Device Identifier Description)是苹果IOS设备的唯一识别码,由40个字符的字母和数字组成。

在很多需要限制一台设备一个账号的应用中经常会用到。在iOS5中可以获取到设备的UDID,但在iOS7中已经完全的禁用了它。因此系统的 UDID 已经不可用.


IDFA & IDFV

  • IDFA - Identifier For Advertising(广告标识符)

    • 同一手机获取该值都相同
    • 重装应用, 不会改变
    • 用户可手动重置这个值. 重置广告 id: 设置 -> 隐私 -> 广告 -> 重置广告 id (中国区的可能看不到这个, 模拟器可以看到)
  • IDFV - Identifier For Vendor(应用开发商标识符)

    • 重装应用, 会改变.

使用代码

#import <UIKit/UIKit.h>
#import <AdSupport/AdSupport.h>

+(NSString*)getDeviceId{
    // IDFA - Identifier For Advertising(广告标识符)
    BOOL isOk = [[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled];
    NSLog(@"--- ad TrackingEnabled: %d", isOk);
    if (isOk) {
        NSString *idfa = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
        NSLog(@"--- idfa: %@", idfa);
        return idfa;
    }
    
    // IDFV - Identifier For Vendor(应用开发商标识符)
    NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    NSLog(@"--- idfv: %@", idfv);
    return @"sdfsdf";
}

Keychain

  • iOS Keychain(钥匙串)原理及使用 - https://juejin.cn/post/6844903921765318669
  • iOS KeyChain的使用- https://www.jianshu.com/p/3afc39f6b9a8
  • ios 开发笔记—UIKeyChainStore使用 - https://www.jianshu.com/p/1c8591ba076a

Keychain的特点

  • 数据并不存放在App的Sanbox中,即使删除了App,资料依然保存在keychain中。如果重新安装了app,还可以从keychain获取数据。
  • keychain的数据可以通过group方式,让程序可以在App间共享。不过得要相同TeamID
  • keychain的数据是经过加密的

网上找到的 大部分使用的是 KeychainItemWrapper, 需要将 KeychainItemWrapper 的头文件和实现文件 (传送门) 拷入项目, 发现很多编译报错, 已经不适用了.

使用第三方库 UICKeyChainStore, https://github.com/kishikawakatsumi/UICKeyChainStore

  1. 模块引入 Security.framework 系统动态库

  2. Podfile 文件中引入 UICKeyChainStore

    pod 'UICKeyChainStore'
    
  3. cd 到 Podfile 文件所在目录, 安装

    $ pod install --verbose
    -> Installing UICKeyChainStore (2.2.1)
    ...
    
  4. 打开 Pods.xcodeproj 工程, 构建所需 cpu 架构的 静态库, 丢到能被项目引入的地方

  5. 代码使用

    #import <UICKeyChainStore/UICKeyChainStore.h>
    
    NSString* key = @"name";
    NSString* value = @"hi, wilker";
    
    // set key value
    [UICKeyChainStore setString:kc.value forKey:kc.key service:@"you bundle id"];
    
    // get value by key
    NSString* value02 = [UICKeyChainStore stringForKey:key service:@"you bundle id"];
    NSLog(@"--- value02: %@", value02);
    

多 app 共享 keychain 数据

  • 如何在iOS应用程序之间共享钥匙串数据 - https://www.thinbug.com/q/4115744

前置条件:

  1. app 都要使用同一个 TeamID 构建出来.
  2. Keychain Groups 相同.

流程

  1. 添加 Keychain Sharing, 会生成 .entitlements 文件, 并添加一个 group (点 + 号默认添加 bundle id 的 group)

    生成的 .entitlements 文件内容

  2. 把这个 .entitlements 文件丢到 同一个 TeamId 的其他 app 下, 就可以共享数据了.


猜你喜欢

转载自blog.csdn.net/yangxuan0261/article/details/113801704