Why SSKeychain doesn't work? - ios

I wanna use SSKeychain to save CFUUID. But ever time I get CFUUID from SSKeychain is nil... I have read doc on github but still don't know what's wrong with it. Waiting for help~ please~~
NSString *retrieveuuid = [SSKeychain passwordForService:#"com.game.userinfo "account:#"uuid"];
if ( retrieveuuid == nil || [retrieveuuid isEqualToString:#""])
{
CFUUIDRef uuid = CFUUIDCreate(NULL);
assert(uuid != NULL);
CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid);
retrieveuuid = [NSString stringWithFormat:#"%#", uuidStr];
[SSKeychain setPassword: retrieveuuid
forService:#"com.game.userinfo"account:#"uuid"];
}

You have a trailing space character in "com.game.userinfo " on the first line (when looking up the UUID), but not in the last line when setting it. These strings should be identical.

Related

How to resolve error of "Reference counted object is used after it is released"?

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.

iOS - [stringObject class] returns "(null)" from phone contacts? How is this possible?

So I have let's say:
NSString *stringObject = (__bridge NSString*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
NSLog(#"%#", [stringObject class]); - RETURNS "(null)"
How is this even possible? And how do you work around something like this?
I am trying to return an empty string in case that string is "(null)".
I've tried:
NSString *lastName = ![stringObject isEqual:#"(null)"] ? stringObject : [NSString string];
OR
NSString *lastName = ![stringObject isEqualToString:#"(null)"] ? stringObject : [NSString string];
OR
NSString *lastName = ![stringObject isEqual:[NSNull null]] ? stringObject : [NSString string];
OR
NSString *lastName = [stringObject isKindOfClass:[NSString class]] ? [NSString string] : stringObject ;
OR
NSString *lastName = (stringObject == nil) ? [NSString string] : stringObject ;
Has anyone other suggestions?
Thank you in advance.
This contact has no last name, and therefore stringObject is nil. Verify with some code:
NSLog(#"Last name='%#'", stringObject);
EDIT How to verify:
Simply test if the object is nil and better still check if the string has length > 0, which you can do as simply as:
NSString *stringObject = (__bridge NSString*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
if ([stringObject length]) {
NSLog(#"Contact last name = '%#'", stringObject);
} else {
NSLog(#"Contact has no last name; they must play for Brazil");
}
You can try this
NSString *lastName = [stringObject isKindOfClass:[NSNull class]] ? [NSString string] : stringObject ;

UUID v1 Objective-C implementation

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 compare two ID

I have two Ids
85816465-FA7B-48B1-8AD3-7FB0A1B6C011 - 85816465-fa7b-48b1-8ad3-7fb0a1b6c011
As you can see, they are almost the same, but there is difference )
85816465-FA7B-48B1-8AD3-7FB0A1B6C011 this code i'm compile by this code
CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
NSString * uuidString = (__bridge NSString*)CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
CFRelease(newUniqueId);
after this insert it into database (Postgres) and database converts it onto this
85816465-fa7b-48b1-8ad3-7fb0a1b6c011
When i'm selecting this inserted Id and trying to compare it with old, Xcode gives me that they are not equal ...
any suggestions?
when you are comparing the strings convert them to Uppercase if that is the only diffrence using the method
uuidString=[uuidString uppercaseString];
Please try to use this one ...I hope it may help you
NSString *str1 = #"85816465-FA7B-48B1-8AD3-7FB0A1B6C011";
NSString *str2 = #"85816465-fa7b-48b1-8ad3-7fb0a1b6c011";
str1 = [str1 stringByReplacingOccurrencesOfString:#"-" withString:#""];
str2 = [str2 stringByReplacingOccurrencesOfString:#"-" withString:#""];
if( [str1 caseInsensitiveCompare:str2] == NSOrderedSame )
NSLog(#"ITS EQUAL");
else
NSLog(#"ITS NOT EQUAL");
Try this
NSRange r = [udidstring1 rangeOfString:udidstring2 options:NSCaseInsensitiveSearch];
if(r.location != NSNotFound)
{
NSLog(#"Both UDID are same");
}
or you can try this
if ([udidstring1 caseInsensitiveCompare:udidstring2]==NSOrderedSame) {
NSLog(#"Both UDID are same");
}

How to generate UUID in ios

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? ?? ""

Resources