1. Create a new project and take a look at your Bundle Id. This Bundle Id must match the Bundle Id on the certificate when you use the real machine to test.
For example, mine is house.xianrou.xianrou
2.Target - Capabilities - Keychain Sharing - ON
The main purpose of this step is to turn on Keychain Sharing and change it from gray OFF to blue ON.
The changes after opening are as follows:
The directory on the left will automatically generate the Entitlements file, so there is no need to create it yourself.
In other words, the first element of the Bundle Identifier, the Keychain Groups of Keychain Sharing, and the Keychain Access Groups of the Entitlements file must maintain the consistency shown in the above figure.
After setting up, you can run the program. If there is no problem, you can proceed to the next step.
3.uuid class and keychain class
Since Apple's keychain method crashes and is a bit complicated, if we only save one uuid we can use the following simple method:
(This is also my own Baidu keychain copied from others, and then modified)
UUID.h
#import
@interface UUID : NSObject
+(NSString *)getUUID;
@end
UUID.m
#import "UUID.h"
#import "KeyChainStore.h"
@implementation UUID
+(NSString *)getUUID
{
}
@end
KeyChainStore.h
#import 尖括号(Foundation/Foundation.h)
@interface KeyChainStore : NSObject
+ (void)save:(NSString *)service data:(id)data;
+ (id)load:(NSString *)service;
+ (void)deleteKeyData:(NSString *)service;
@end
KeyChainStore.m
#import "KeyChainStore.h"
@implementation KeyChainStore
+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
}
+ (void)save:(NSString *)service data:(id)data {
}
+ (id)load:(NSString *)service {
}
+ (void)deleteKeyData:(NSString *)service {
}
@end
#ifndef PrefixHeader_pch
#define PrefixHeader_pch
#define
#define
#define
#endif
$(SRCROOT)/$(PROJECT_NAME)/PrefixHeader.pch
,其中$(PROJECT_NAME)
是相对工程名,比上面的方法更便捷.
5.在viewcontroller.m里面执行如下代码