Sizing labels with -[NSString sizeWithFont:...] – WRONG

Right:

CGSize maxSize = CGSizeMake(200.0f, CGFLOAT_MAX);
CGSize requiredSize = [label sizeThatFits:maxSize];
label.frame = CGRectMake(10, 10, size.width, size.height);

More details

Customize UIPopoverController

Popovers are very common within the iPad user interface but you were restricted to the design provided by Apple. With iOS 5 came a little known class called UIPopoverBackgroundView which allows you to provide a custom border and arrow for the popover.

Full tutorial with sample code here.

Android Service SecurityException when starting

Start service always this way:

context.startService(new Intent(MusicService.ACTION_PAUSE, null, context, MusicService.class));

iPhone – transparent imageview tint

Drop-in solution for a tintable/blendable “imageview”:

The .h file:

#import <UIKit/UIKit.h>

@interface UIBlendableImageView : UIView

- (id)initWithImage:(UIImage*)image andTintColor:(UIColor*)tintColor;

@end

The .m file:

#import "UIBlendableImageView.h"
#import <QuartzCore/QuartzCore.h>

@implementation UIBlendableImageView

- (id)initWithImage:(UIImage*)image andTintColor:(UIColor*)tintColor
{
    self = [super initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    if (self) {
        UIImageView *maskImageView = [[[UIImageView alloc] initWithImage:image] autorelease];
        [maskImageView setFrame:[self bounds]];
        [[self layer] setMask:[maskImageView layer]];
        [self setBackgroundColor:tintColor];
    }
    return self;
}

@end

Remove all accents in NSString

// convert to a data object, using a lossy conversion to ASCII
NSData *asciiEncoded = [yourOriginalString dataUsingEncoding:NSASCIIStringEncoding
                         allowLossyConversion:YES];

// take the data object and recreate a string using the lossy conversion
NSString *other = [[NSString alloc] initWithData:asciiEncoded
                                        encoding:NSASCIIStringEncoding];
// relinquish ownership
[other autorelease];

Via Stackoverflow.com

Preventing backup to iCloud

5.1 and up

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
 
    NSError *error = nil;
    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];
    if(!success){
        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    }
    return success;
}

5.0.1

#import <sys/xattr.h>
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
 
    const char* filePath = [[URL path] fileSystemRepresentation];
 
    const char* attrName = "com.apple.MobileBackup";
    u_int8_t attrValue = 1;
 
    int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
    return result == 0;
}

Source

Android unmarshalling from Parcel

When unmarshalling from a byte array into a Parcel object, be sure to reset (setDataPosition(0)) it before reading out any object.

Example:

Parcel p = Parcel.obtain();
p.unmarshall(buff, 0, length);
p.setDataPosition(0);
return p.readBundle();

Android zoomable-pannable ImageView

PhotoView aims to help produce an easily usable implementation of a zooming Android ImageView.
Features

  • Out of the box zooming, using multi-touch and double-tap.
  • Scrolling, with smooth scrolling fling.
  • Works perfectly when using used in a scrolling parent (such as ViewPager).
  • Allows the application to be notified when the displayed Matrix has changed. Useful for when you need to update your UI based on the current zoom/scroll position.
  • Allows the application to be notified when the user taps on the Photo.
  • PhotoView GitHub site

    Runtime iOS Version Checking

    Simple preprocessor macros to detect the current iOS version at runtime.

    The NSNumericSearch compare option is very clever and can evaluate various types of numeric strings, including period separated integer strings!

    Source

    /*
     *  System Versioning Preprocessor Macros
     */ 
    
    #define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
    #define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
    #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
    #define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
    #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
    
    /*
     *  Usage
     */ 
    
    if (SYSTEM_VERSION_LESS_THAN(@"4.0")) {
        ...
    }
    
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"3.1.1")) {
        ...
    }

    Reachability with ARC and GCD

    This is a drop-in replacement for Apples Reachability class. It is ARC compatible, uses the new GCD methods to notify of network interface changes.

    In addition to the standard NSNotification it supports the use of Blocks for when the network becomes reachable and unreachable.

    Finally you can specify wether or not a WWAN connection is considered “reachable”.

    Go to GitHub page