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);
}
Related
I am getting following error but how to resolve it ?
Error is highlighted with green circle "Reference counted object is used after it is released"
Edited: I am using following method
+ (NSString *)GetUUID
{
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
NSString *str = (__bridge NSString *)string;
CFRelease(string);
return str;
}
Edited: Resolved by using vijay's following simple code
NSUUID *UUID = [NSUUID UUID];
NSString* stringUUID = [UUID UUIDString];
I hope, you are getting this error because of [DBManager GetUUID] method, where you would release the CFRelease(cfUuid).
To get the UUID, try this simplified API
+ (NSString *)GetUUID
{
NSUUID *UUID = [NSUUID UUID];
NSString* stringUUID = [UUID UUIDString];
return stringUUID;
}
After CFUUIDCreateString, you get a string you own. By using __bridge, you set str to the same string. So when you CFRelease(string) you do not own the memory backing str anymore...
To avoid this, either use a Cocoa method like #vijay says, or remove the CFRelease and use __bridge_transfer NSString* instead of __bridge. This tells the compiler you're transferring a CF object you own into the ARC world.
Per the documentation:
__bridge_transfer or CFBridgingRelease moves a non-Objective-C pointer to Objective-C and also transfers ownership to ARC. ARC is responsible
for relinquishing ownership of the object.
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);
Can we retrieve the iDevice Model like it is shown in
Settings->General->About->Model = MD662F ?
Unfortunately, no. UIDevice has a "model" property, but will only return #”iPhone” and #”iPod touch”, etc.
UIDevice *currentDevice = [UIDevice currentDevice];
NSString *model = [currentDevice model];
NSLog(#"Device Model: %#", model);
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? ?? ""
I am trying to localize the text "System Name:" in the code below:
NSString *systemName = [NSString stringWithFormat:#"System Name: %#", [[UIDevice
currentDevice] systemName];
I do this by changing the code to this:
NSString *systemName = NSLocalizedString(#"SystemNameKey", #"System Name Info");
In my Localizable.strings file, I add the following code:
"SystemNameKey" = "System Name: %#", [[UIDevice currentDevice] systemName];
Of course, this will not work because UIKit is not imported into the Localizable.stings, and not surprisingly, when I add the import code, it does not work. I am sure there is an alternate way of doing this that I'm just not thinking of at the moment. Any ideas? I feel like I'm missing something really obvious.
You should try doing it this way:
NSString *systemNameLocalized = NSLocalizedString(#"SystemNameKey", #"System Name Info");
NSString *systemName = [NSString stringWithFormat:systemNameLocalized, [[UIDevice currentDevice] systemName]];
and in your Localizable.string file:
"SystemNameKey" = "System Name: %#";