This question already has answers here:
UIDevice uniqueIdentifier deprecated - What to do now?
(32 answers)
Closed 9 years ago.
Hello everyone how to create a unique identifier for each iphone device of length 15 characters?
Usually developers use CFUUIDRef
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
NSString *uuidString = (NSString *)CFUUIDCreateString(NULL,uuidRef);
CFRelease(uuidRef);
If you need exactly 15?
You can cut this string:
NSString *resultString = [uuidString substringWithRange: NSMakeRange (0, 15)];
Apple announced some days ago that apps would be rejected if they keep using the old deprecated uniqueidentifier method:
[[UIDevice currentDevice] uniqueIdentifier];
It must be changed to:
CFUUIDRef uuid = CFUUIDCreate(NULL);
CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid);
CFRelease(uuid);
Related
This question already has answers here:
CFURLCreateStringByAddingPercentEscapes is deprecated in iOS 9, how do I use "stringByAddingPercentEncodingWithAllowedCharacters"
(3 answers)
Closed 5 years ago.
In my code, the method CFURLCreateStringByAddingPercentEscapes is marked as deprecated as of iOS 9. My code is as follows:
static NSString *encodeByAddingPercentEscapes(NSString *input) {
NSString *encodedValue = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)input, NULL, (CFStringRef)#"!*'();:#&=+$,/?%#[]", kCFStringEncodingUTF8));
return encodedValue;
}
The warning suggests that I use [NSString stringByAddingPercentEncodingWithAllowedCharacters:]
How can I accomplish the same result using the newer method?
you can use [#"" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]].
And there are many set to use:URLPathAllowedCharacterSet、URLQueryAllowedCharacterSet.etc
I made a payed app for Cydia. I like to verify if the user officially bought the application. I already figured out, that there is a Cydia Store API but I have several issues with it. Does anyone ever used it or is there an alternative to verify if the user bought it? The API is using the UDID, which has been made unaccessable in iOS 7, however Cydia and several other application can access the UDID. I figured out that you can calculate the UDID with SHA1 hash of serial + IMEI + wifiMac + bluetoothMac and with the SHA1 hash of serial + ECID + wifiMac + bluetoothMac on the Verizon iPhone 4. I can get the Serial number, but I am not able to get the IMEI, ECID, WiFi MAC and the Bluetooth MAC. Is there a methode to get the UDID on iOS 7?
I really appreciate your help!
Edit
After a long night I figured out how to get the UDID of an device on iOS 7. Its fairly easy.
Add "libMobileGestalt.dlib" and "libMobileGestaltExtensions.dlib" to your "Linked Frameworks and Libraries"
Download MobileGestalt.h and add it to your project
Copy to get the UDID of the device:
CFStringRef value =MGCopyAnswer(kMGUniqueDeviceID);
NSString *udid = (__bridge NSString *)(value);
CFRelease(value);
Now I just have to know how to correctly calculate the signature. This is my current way.
NSString *signature = [self hmacsha1:[[[[NSString stringWithFormat:#"%#", [[dataString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] dataUsingEncoding:NSUTF8StringEncoding]] stringByReplacingOccurrencesOfString:#" " withString:#"+"] stringByReplacingOccurrencesOfString:#"<" withString:#""] stringByReplacingOccurrencesOfString:#">" withString:#""].uppercaseString key:#"abcdef0123456789abcdef0123456789"];
signature = [self toBase64String:signature];
signature = [[[signature stringByReplacingOccurrencesOfString:#"+" withString:#"_"] stringByReplacingOccurrencesOfString:#"/" withString:#"_"] stringByReplacingOccurrencesOfString:#"=" withString:#""];
But somehow the signature is wrong, so the response is: signature=THE_SIGNATURE&error=invalide+signature+MY_QUERY
And I don't know why, because I am hmac_sha1 hashing the text with a key, this is my hashing function:
+(NSString *)hmacsha1:(NSString *)text key:(NSString *)secret {
const char *cKey = [secret cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [text cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC
length:sizeof(cHMAC)];
NSString *hash = [HMAC base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
return hash;
}
i am using the below code to display the UDID of the device.
But its displaying the null value
i.e. 2014-05-12 11:56:06.896 LoginScreen[195:60b] deviceUDID: (null)
NSUUID *deviceId;
deviceId = [UIDevice currentDevice].identifierForVendor;
NSLog(#"deviceUDID: %#",deviceID);
Sorry to all of you. I made a silly mistake
Here NSUUID instance is deviceId and i am printing deviceID in NSLog :)
Now it is working for me. Thanks to everyone
In order to get UUID of the device you can use the following line of code
[[[UIDevice currentDevice] identifierForVendor] UUIDString];
But you have to check whether the app is running on simulator or on device.
hope this helps you.
Apple has hidden the UDID from all public APIs, starting with iOS 7. Any UDID that begins with FFFF is a fake ID.
Objective-C :
NSString *strUUIDValue = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
NSLog(#"strUUIDValue : %#", strUUIDValue);
Swift 2 :
let UUIDValue = UIDevice.currentDevice().identifierForVendor!.UUIDString
print("UUID: \(UUIDValue)")
NSString *string = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
string = [string stringByReplacingOccurrencesOfString:#"-" withString:#""];
string = [NSString stringWithFormat:#"%#%#",#"FFFFFFFF",string];
Try this
NSUUID *deviceId;
#if TARGET_IPHONE_SIMULATOR
deviceId = [NSUUID initWithUUIDString:#"UUID-STRING-VALUE"];
#else
deviceId = [UIDevice currentDevice].identifierForVendor;
#endif
OR
NSString *uuid = nil;
CFUUIDRef theUUID = CFUUIDCreate(kCFAllocatorDefault);
if (theUUID) {
uuid = NSMakeCollectable(CFUUIDCreateString(kCFAllocatorDefault, theUUID));
[uuid autorelease];
CFRelease(theUUID);
}
I want to implement UUID v1 in my iOS App.
I know that it is composed of Mac Address and timestamp as described in
http://en.wikipedia.org/wiki/Universally_unique_identifier#Version_1_.28MAC_address.29
Is there any objective-c implementation for this V1, based on CFUUID functions ?
I already have the mac address and the timestamp.
The UUID v1 description at Wikipedia : "The original (version 1) generation scheme for UUIDs was to concatenate the UUID version with the MAC address of the computer that is generating the UUID, and with the number of 100-nanosecond intervals since the adoption of the Gregorian calendar in the West"
It is also specified at http://www.ietf.org/rfc/rfc4122.txt , but it seems that it will need time to implement it.
I have found this link : http://www.famkruithof.net/guid-uuid-timebased.html who have a simple explanation for the steps to create a v1 UUID. Is there any existing implementation, before I implement it by my self?
I thinks it is common behavior to use framework functions. And that is use CFUUID. For example:
+(NSString*)get {
NSString *deviceID = [[NSUserDefaults standardUserDefaults] objectForKey:#"DeviceID"];
if (!deviceID) {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
deviceID = (NSString*)string;
[[NSUserDefaults standardUserDefaults] setValue:deviceID forKey:#"DeviceID"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
return deviceID;
}
Please try this one. It may be helpful to you
+(NSString*) Create_UDID
{
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
NSString* strString = [NSString stringWithFormat:#"%#", string];
NSString *strValue = [strString stringByReplacingOccurrencesOfString:#"-"withString:#""];
if (strValue == nil) {
strValue = #"";
}
return strValue;
}
How to get a UUID in objective c, like in Java UUID is used to generate unique random numbers which represents 128 bit value.
Try:
CFUUIDRef udid = CFUUIDCreate(NULL);
NSString *udidString = (NSString *) CFUUIDCreateString(NULL, udid);
UPDATE:
As of iOS 6, there is an easier way to generate UUID. And as usual, there are multiple ways to do it:
Create a UUID string:
NSString *uuid = [[NSUUID UUID] UUIDString];
Create a UUID:
[NSUUID UUID]; // which is the same as..
[[NSUUID] alloc] init];
Creates an object of type NSConcreteUUID and can be easily casted to NSString, and looks like this: BE5BA3D0-971C-4418-9ECF-E2D1ABCB66BE
NOTE from the Documentation:
Note: The NSUUID class is not toll-free bridged with CoreFoundation’s CFUUIDRef. Use UUID strings to convert between CFUUID and NSUUID, if needed. Two NSUUID objects are not guaranteed to be comparable by pointer value (as CFUUIDRef is); use isEqual: to compare two NSUUID instances.
Swift version of Raptor's answer:
let uuid = UUID().uuidString
+ (NSString *)uniqueFileName
{
CFUUIDRef theUniqueString = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUniqueString);
CFRelease(theUniqueString);
return [(NSString *)string autorelease];
}
-(NSString*) myUUID()
{
CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueID);
NSString *guid = (__bridge NSString *)newUniqueIDString;
CFRelease(newUniqueIDString);
CFRelease(newUniqueID);
return([guid lowercaseString]);
}
you can use CFUUID for iOS 5 or lower version and NSUUID for iOS 6 and 7.
for making it more secure you can store your UUID in keychain
- (NSString*)generateGUID{
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return [NSString stringWithFormat:#"%#", string];
}
For Swift 5.0, Use this,
let uuidRef = CFUUIDCreate(nil)
let uuidStringRef = CFUUIDCreateString(nil, uuidRef)
let uuid = uuidStringRef as String? ?? ""