iOS SHA512 Hex String

Here's an Objective C method that'll return a SHA512 hex of a given string:

  1.  
  2. -(NSString*) sha512:(NSString*)input {
  3. const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
  4. NSData *data = [NSData dataWithBytes:cstr length:input.length];
  5.  
  6. uint8_t digest[CC_SHA512_DIGEST_LENGTH];
  7.  
  8. CC_SHA512(data.bytes, data.length, digest);
  9.  
  10. NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2];
  11.  
  12. for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++)
  13. [output appendFormat:@"%02x", digest[i]];
  14.  
  15. return output;
  16.  
  17. }
  18.  

You'll need to include:

  1.  
  2. #include <CommonCrypto/CommonDigest.h>
  3.  

iOS: Show Users the Reason You Require Their Location

Update: iOS 6 deprecated the purpose property and it now has to be set in your Info.plist with the key NSLocationUsageDescription which is displayed in Xcode as Privacy - Location Usage Description. If you're still supporting iOS 5 and earlier make sure you include both.

A great, but underused location feature on iOS is the purpose property of the CLLocationManager.

So many apps ask for your location - give your users confidence in sharing their location by telling them why you need it!

Example:

  1. locationManager.purpose = @"We'll only use your location to show you local events nearby.";

You need to do this before you start the location monitoring, so:

  1. CLLocationManager *locationManager = [[CLLocationManager alloc] init];
  2. locationManager.purpose = @"We'll only use your location to show you local events nearby.";
  3. locationManager.delegate = self;
  4. [locationManager startUpdatingLocation];

Your users will now get a pleasant reason when you request their permission:
iOS iPhone Screen Location Prompt

I've no idea why this is so rarely used, it's been available since iOS 3.2.

Tip from the Apple docs:

You must set the value of this property prior to starting any location services. Because the string is ultimately displayed to the user, you should always load it from a localized strings file.

Using Boolean Values with TouchJSON

When creating an NSDictionary to serialize as JSON with TouchJSON you can't set a BOOL value in the dictionary because it requires an object.

Use an NSNumber, which will be converted to true or false by the TouchJSON library.

Eg:

  1.  
  2. NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
  3. [dictionary setObject:[NSNumber numberWithBool:YES forKey:@"shouldBeOn"];
  4. [dictionary setObject:[NSNumber numberWithBool:NO forKey:@"shouldBeOff"];
  5. NSError *error = NULL;
  6. NSData *jsonData = [[CJSONSerializer serializer] serializeObject:dictionary error:&error];
  7.  

Produces:

  1. {
  2. "shouldBeOn": true,
  3. "shouldBeOff": false
  4. }