iOS SHA512 Hex String

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

-(NSString*) sha512:(NSString*)input {
    const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:input.length];
    
    uint8_t digest[CC_SHA512_DIGEST_LENGTH];
    
    CC_SHA512(data.bytes, data.length, digest);
    
    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return output;
    
}

You'll need to include:

    #include 

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:

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:

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.purpose = @"We'll only use your location to show you local events nearby.";
locationManager.delegate = self;
[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:

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:[NSNumber numberWithBool:YES forKey:@"shouldBeOn"];
[dictionary setObject:[NSNumber numberWithBool:NO forKey:@"shouldBeOff"];
NSError *error = NULL;
NSData *jsonData = [[CJSONSerializer serializer] serializeObject:dictionary error:&error];

Produces:

{
    "shouldBeOn": true,
    "shouldBeOff": false
}