iOS獲取UUID,並使用keychain儲存

2021-07-03 23:30:34 字數 3887 閱讀 6493

udid被棄用,使用uuid來作為裝置的唯一標識。獲取到uuid後,如果用nsuserdefaults儲存,當程式被解除安裝後重灌時,再獲得的uuid和之前就不同了。使用keychain儲存可以保證

程式給大家兩個類:

uuid.h中的**:

#import 尖括號(foundation/foundation.h)

@inte***ce uuid :

nsobject

+(nsstring *)getuuid;

@end

uuid.m中的**:

#import "uuid.h"

#import 

尖括號(

foundation/foundation.h)

@implementation uuid

+(nsstring *)getuuid

return struuid;

}@end

#import 尖括號(uikit/uikit.h)

@property (nonatomic, retain) nsmutabledictionary *keychainitemdata;

@property (nonatomic, retain) nsmutabledictionary *genericpasswordquery;

- (id)initwithidentifier: (nsstring *)identifier accessgroup:(nsstring *) accessgroup;

- (void)setobject:(id)inobject forkey:(id)key;

- (id)objectforkey:(id)key;

- (void)resetkeychainitem;

@end

#import 尖括號(security/security.h)

- (nsmutabledictionary *)secitemformattodictionary:(nsdictionary *)dictionarytoconvert;

- (nsmutabledictionary *)dictionarytosecitemformat:(nsdictionary *)dictionarytoconvert;

- (void

)writetokeychain;

@end

@synthesize keychainitemdata, genericpasswordquery;

- (id)initwithidentifier: (nsstring *)identifier accessgroup:(nsstring *) accessgroup;

[genericpasswordquery setobject:(

id)cfbridgingrelease(

ksecmatchlimitone

) forkey:(id)

ksecmatchlimit];

[genericpasswordquery

setobject

:(id

)kcfbooleantrue forkey

:(id

)ksecreturnattributes];

nsdictionary *tempquery = [nsdictionary

dictionarywithdictionary

:genericpasswordquery];

nsmutabledictionary *outdictionary = nil;

if (! secitemcopymatching((cfdictionaryref)tempquery, (cftyperef*)&outdictionary) == noerr) }

else

[outdictionary release]; }

return self; }

- (void)dealloc

- (void)setobject:(id)inobject forkey:(id)key 

}- (id)objectforkey:(id)key

- (void)resetkeychainitem

else

if(keychainitemdata)

[keychainitemdata

setobject

:@""

forkey

:(id

)ksecattraccount];

[keychainitemdata setobject

:@""

forkey

:(id

)ksecattrlabel];

[keychainitemdata

setobject

:@""

forkey

:(id

)ksecattrdescription]; 

[keychainitemdata

setobject

:@""

forkey

:(id

)ksecvaluedata]; }

- (nsmutabledictionary *)dictionarytosecitemformat:(nsdictionary *)dictionarytoconvert

- (nsmutabledictionary *)secitemformattodictionary:(nsdictionary *)dictionarytoconvert

else

[passworddata release];

return returndictionary;

}- (void)writetokeychain

else

}@end

ps: 1. 

2.使用時要新增

security.framework

3.  尤為注意的是,

uuid類下面這句**中group的設定方法。

*keychainitem = [[

alloc]

initwithidentifier:@"uuid"

accessgroup:@"your_bundle_seed.com.yourcompany.userinfo"];

(1)在專案相同的目錄下建立keychainaccessgroups.plist檔案。

該檔案的結構中最頂層的節點必須是乙個名為「keychain-access-groups」的array,並且該array中每一項都是乙個描述分組的nsstring。your_bundle_seed.com.yourcompany.userinfo就是要設定的組名。

(2)在專案相同的目錄下建立keychainaccessgroups.plist檔案。在target-build settings-code signing欄下的code signing entitlements右側新增keychainaccessgroups.plist,如下圖。

到此,工作就完成了。

首次安裝程式時,列印出乙個uuid,當把程式解除安裝後,再用

getuuid獲得

uuid,列印出來的結果和之前相同。證明達到目的。

測試**:

nsstring * uuid= [uuid

getuuid];

nslog(@"uuid=%@",uuid);

測試結果:

uuid=19aab430-9cb8-4325-acc5-d7d386b68960

iOS 如何獲取 Mach O 的 UUID

import nsstring executableuuid else return nil 複製 可以獲取主 image 檔案的路徑,然後根據路徑去獲取 image 的 index,然後根據這個 index 去獲取對應 image 的header,通過 header 找到 image 的 load...

keychain 生成uuid 使用獲取不到資料

git官網 問題 一直獲取不到資料 之前 let keychain keychainswift keychain.accessgroup x var cuserid string if let userid keychain.get cuserid else print c uuid cuserid...

iOS 獲取使用者訪問許可權描述key彙總

需要您的同意,才能訪問 資料庫 nsbluetoothperipheralusagedescription 需要您的同意,才能訪問藍芽 nscalendarsusagedescription 需要您的同意,才能訪問日曆 nscamerausagedescription 需要您的同意,才能訪問相機 n...