Cocoa touch

  • How to switch the code/build for the device or simulator

    “My Pacakge” uses AdMob library but AdMob library is not compatible with iOS 4 simulator (so many linking error occurrs). I found the way to avoid these errors in the apple document. Switching code with preprocessor macros is the answer.

  • How to trigger MKAnnotationView’s callout view without touching the pin? – Stack Overflow

    But there is a catch to get benvolioT's solution to work, the code for (id currentAnnotation in mapView.annotations) { if ([currentAnnotation isEqual:annotationToSelect]) { [mapView selectAnnotation:currentAnnotation animated:FALSE]; } }

  • Functions for CGRect

    In the cocoa (cocoa touch) framework, CGRect structure are used in many places because its rendering system depends on Core Graphics library. Here are the useful functions for CGRect structure.

  • How to change the carrier name in the iPhone simulator

    iPhone simulator shows “Carrier” as default carrier name. You can change this name whatever text you want.

  • Open URL from application

    Pretty simple. This call terminates the application and starts Mobile Safari browser with specified URL. [[UIApplication sharedApplication] openURL: [NSURL URLWithString: @”http://www.apple.com/”]];

  • How to remember the setting (user defaults) easily

    NSUserDefautls object automatically loads and saves small information. Along with primitive value like BOOL, integer or float, NSUserDefaults can handle objects like NSArray, NSData, NSDictionary, NSString. // Save a default [[NSUserDefaults standardUserDefaults] setObject:@”NO” forKey:@”ShowWarning”]; // Read BOOL warning value from NSUserDefaults BOOL showWarning = [[NSUserDefaults standardUserDefaults] boolForKey:@”ShowWarning”];

  • Capture view contents into an UIImage object

    By combining UIGraphicsBeginImageContext() and -renderInContext in CALayer, you can draw contents into a image (UIImage) object. It is like screen capture. UIGraphicsBeginImageContext(view.frame.size); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();

  • Set border around the view

    There is no border property in UIView…. classes but we can access CALayer class to set border. Since layer property is defined in the UIView class, all of UIView inherited class can use this property to set border. It needs to have #import <QuartzCore/QuartzCore.h> to access CALayer. view.layer.borderWidth = 1; view.layer.borderColor = [[UIColor grayColor]…

  • Accessing AppDelegate instance

    AppDelegate object can access from any class in the same application like below. Methods or properties shared between controllers can be defined under application delegates. SomeAppDelegate *appDelegate = (SomeAppDelegate *)[[UIApplication sharedApplication] delegate];

  • How to access special directory on the iphone device

    Use NSSearchPathForDirectoriesInDomains(). First argument “NSDocumentDirecotry” is the key to specify special directory, in this case Document directory under the application. The method returns absolute path expression. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSLog(@”NSDocumentDirectory is %@”, documentsDirectory);