ios-Unique Identifier and Keychain Sharing


title: ios-Unique Identifier and Keychain shared
categories: Ios
tags: [ios, Unique Identifier, UDID, Keychain]
date: 2021-02-13 14:50:38
comments: false
mathjax: true
toc: true

ios-Unique Identifier and Keychain Sharing


Prequel

in iOS7 has been completely disabled UDID , recommended [IDFA & IDFV] (# IDFA & IDFV)

Combined Keychain persistence characteristics, to achieve a unique identifier lasting availability.


Unique identifier

  • Seemingly more it is recommended to use to enhance the durability of KeyChain
  • The most complete summary of iOS device information acquisition in history (iPhone 12 has been updated)-https://www.jianshu.com/p/b23016bb97af
  • iOS unique device ID-https://www.cnblogs.com/kekec/p/12560850.html
  • iOS-Advertising Identifier (IDFA & IDFV)-https://www.jianshu.com/p/8e2846dc8a03

UDID

UDID (Unique Device Identifier Description) is the unique identification code of Apple's IOS device, consisting of 40 characters of letters and numbers.

It is often used in many applications that need to restrict one device to one account. The UDID of the device can be obtained in iOS5, but it has been completely disabled in iOS7. Therefore, the UDID of the system is no longer available.


IDFA & IDFV

  • IDFA-Identifier For Advertising

    • The same mobile phone gets the same value
    • Reinstall the application, will not change
    • The user can reset this value manually. Reset the advertising id: Settings -> Privacy -> Advertising -> Reset the advertising id (You may not see this in China, but you can see it on the emulator)
  • IDFV-Identifier For Vendor (application developer identifier)

    • Reinstall the application, it will change.

Use code

#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

  • Principle and use of iOS Keychain-https://juejin.cn/post/6844903921765318669
  • Use of iOS KeyChain-https://www.jianshu.com/p/3afc39f6b9a8
  • ios development notes-UIKeyChainStore use-https://www.jianshu.com/p/1c8591ba076a

Features of Keychain

  • The data is not stored in the Sanbox of the App. Even if the App is deleted, the data is still stored in the keychain. If you reinstall the app, you can also get data from the keychain.
  • Keychain data can be grouped so that programs can be shared between apps. But it has to be the same TeamID
  • Keychain data is encrypted

Most of the ones found on the Internet use KeychainItemWrapper , you need to copy the header file and implementation file ( portal ) of KeychainItemWrapper into the project, and found that many compilation errors are reported, which are no longer applicable.

Use the third-party library UICKeyChainStore , https://github.com/kishikawakatsumi/UICKeyChainStore

  1. The module introduces the Security.framework system dynamic library

  2. Introduce the UICKeyChainStore library in the Podfile file

    pod 'UICKeyChainStore'
    
  3. cd to the directory where the Podfile file is located, install

    $ pod install --verbose
    -> Installing UICKeyChainStore (2.2.1)
    ...
    
  4. Open the Pods.xcodeproj project, build the static library of the required cpu architecture, and drop it where it can be introduced by the project

  5. Code usage

    #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);
    

Multi-app sharing keychain data

  • How to share keychain data between iOS apps-https://www.thinbug.com/q/4115744

Precondition:

  1. All apps must be built with the same TeamID.
  2. Keychain Groups are the same.

Process

  1. Add Keychain Sharing , it will generate .entitlements file, and add a group (click + sign to add bundle id group by default)

    The content of the generated .entitlements file

  2. Drop this .entitlements file to other apps with the same TeamId , and you can share data.


Guess you like

Origin blog.csdn.net/yangxuan0261/article/details/113801704