Monday, December 5, 2011

iPhone: prevent auto-lock screen.

[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

iOS: hide keyboard when touch on background.

To close keyboard when user touch on background or touch outside a component: UITapGestureRecognizer
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
// For selecting cell.
gestureRecognizer.cancelsTouchesInView = NO;
[self.tableView addGestureRecognizer:gestureRecognizer];
[gestureRecognizer release];
}

- (void) hideKeyboard {
[self.view endEditing:YES];
}



If not using a table, replace self.tableView by self.view that will help to close keyboard when touch on background.

iPhone: easy to save a file.

To save a file in folder 'Documents':
NSData *dat = [[NSData alloc] init]; // init data.
UIImage *image = [[UIImage alloc] initWithData:dat];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex: 0];
NSString * pathFile = [NSString stringWithFormat:@"image_%s.png", [[[NSDate date] descriptionWithCalendarFormat:@"%H_%M_%S" timeZone:nil locale: [NSUserDefaults standardUserDefaults] dictionaryRepresentation]] UTF8String]];
NSString *docFile = [docDir stringByAppendingPathComponent: pathFile];

NSLog(@"+++++++++++++++++++ %@", docFile);

NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:docFile atomically:NO];

iOS: To find what is the first responder.

Notes: this is used for test only, do not use this codes in App because it is not allowed by Apple.
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; 
UIView   *firstResponder = [keyWindow performSelector:@selector(firstResponder)]; 

Hi there!

This is where to share my notes, :)