Paper/a chestnut Wow (Jane book author)
Original link: http://www.jianshu.com/p/65c92cd1c0ee
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
"Brush Coupons" is to brush the group or the public to do the first-order discount of the app coupons, they create a few constraints for the "First order", one of which is the uniqueness of the device-participated in the can no longer participate, it is necessary to obtain the unique identity of the device. This skill once made me eat steak in college and watch movies without money.
A little off-topic, back to the chase. I looked up some information to see how the "Device identity is unique" under iOS. I have to say that iOS does very well in terms of user privacy policies and permissions, the permissions that an app can get (without jailbreaking) are very limited, and many of the previous methods, such as Udid, are deprecated.
Then say that it can be used now, it is common to use UUID + keychain
The combination to achieve this demand.
UUID is universally unique identifier abbreviation, Chinese meaning is universal unique identification code. It is to allow all the elements in the distributed system to have unique identification information, and do not need to through the central control side to do the identification information designation. In this way, everyone can create a UUID that does not clash with others. In this case, there is no need to consider the name duplication problem when the database is established. Apple recommends using a UUID to generate a unique identification string for your app.
First we want to get a UUID, the method is as follows:
-(nsstring*) uuid { = cfuuidcreate (nil); = cfuuidcreatestring (nil, uuid); * result = (NSString *) cfbridgingrelease (Cfstringcreatecopy (NULL, uuidstring)); Cfrelease (UUID); Cfrelease (uuidstring); return result;}
Now that we've got a uuid, although the identity is unique, it's not guaranteed to be unique every time, because every time you call this method or unload the app, the UUID regenerates a different one. At this time keychain played a role. So the whole logic is this: 先从keychain取UUID,如果能取到,就用这个比对,如果取不到就重新生成一个保存起来。
Keychain is independent of the app, and the system is unified level, so you don't have to worry about it hanging off.
Keychain is the password management system in Apple's Mac OS. It is imported in Mac OS 8.6 and is included in all subsequent versions of Mac OS, including Mac OS X. A keychain can contain multiple types of data: passwords (including websites, FTP servers, SSH accounts, network shares, wireless networks, group software, encrypted disk images, etc.), private keys, electronic certificates, and encrypted notes. The iOS side also has a keychain to help us manage these sensitive information.
Children's shoes that have used keychain to save their account password should be very knowledgeable about the tool and do not explain too much here. Use keychain need to import Security.framework
and KeychainItemWrapper.h/.m
, KeychainItemWrapper.h/.m
Search can download down, drag into the project. Save the UUID code as follows:
- (void) Saveuuidwithkeychain {keychainitemwrapper*keychainitem =[[Keychainitemwrapper alloc] Initwithidentifier:@"UUID"Accessgroup:@"com. Qiuyunxiang.www"]; NSString*struuid = [Keychainitem objectforkey: (ID) Ksecvaluedata]; if(Struuid = = Nil | | [Struuid isequaltostring:@""]) {[Keychainitem setobject:[self uuid] Forkey: (ID) Ksecvaluedata]; }}
Note: This parameter in this method accessGroup:
can be shared if some app settings are the same.
Here's how to get the UUID from keychain:
-(NSString *) getkeychain { *keychainitem = [[Keychainitemwrapper alloc] initwithidentifier: @"UUID" accessgroup:@ "com. qiuyunxiang.com"]; *struuid = [Keychainitem objectforkey: (ID) ksecvaluedata]; return Struuid;}
At this point, basically the only way to identify a few methods is finished, you can test, uninstall the application and then reload, read from the keychain UUID or the same as before.
But there is an element of uncertainty, that is, the phone system to restore the factory settings or erase all the data, this method may not work, because it is dependent on the keychain in the survival, the keychain is dead, it also fails.
(go) iOS get device unique identification code