Throwing an exception in iOS

February 26, 2013 in iOS Snippets


[NSException raise:@"Invalid parameter" format:@"Object %@ is incorrect", obj];

Clearing all notifications an object is observing

February 16, 2013 in iOS Snippets


[[NSNotificationCenter defaultCenter] removeObserver:self];

 

Clearing the selection of a table view

February 16, 2013 in iOS Snippets


[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];

How to easily tell if a device has access to the Internet

February 16, 2013 in iOS Snippets

Use this full-featured library: Reachability - an improved version of Apple’s classic Reachability code sample.

Showing an error popover with UIAlertView

February 14, 2013 in iOS Snippets


UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"640KB is not enough"
     delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
[alert show]

Opening a URL in Safari

February 14, 2013 in iOS Snippets


[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];

Getting an object’s class name… and back!

February 10, 2013 in iOS Snippets


NSString *className = NSStringFromClass([obj class]);

Class class = NSClassFromString(className);

Copying attributes from an object to another

February 9, 2013 in iOS Snippets

[destinationObject setValuesForKeysWithDictionary:
  [sourceObject dictionaryWithValuesForKeys:[[NSArray alloc] initWithObjects:@"attr1", @"attr2", nil]]];

Creating an empty UIImage of a given size and color programmatically

February 9, 2013 in iOS Snippets


+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size

{

CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);

UIGraphicsBeginImageContext(rect.size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [color CGColor]);

CGContextFillRect(context, rect);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;

}

Lighten or darken a UIColor

February 9, 2013 in iOS Snippets

// [MYClass changeBrightness:someColor amount:1.1] returns a color that is 10% brighter than the original.
// 0.9 darkens by 10%. The percentage is relative to white, so 10% will lighten/darken the same amount
// regardless of how dark or light the color originally was. iOS5+

+ (UIColor*)changeBrightness:(UIColor*)color amount:(CGFloat)amount
{

    CGFloat hue, saturation, brightness, alpha;
    if ([color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha]) {
        brightness += (amount-1.0);
        brightness = MAX(MIN(brightness, 1.0), 0.0);
        return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:alpha];
    }

    CGFloat white;
    if ([color getWhite:&white alpha:&alpha]) {
        white += (amount-1.0);
        white = MAX(MIN(white, 1.0), 0.0);
        return [UIColor colorWithWhite:white alpha:alpha];
    }

    return nil;
}