Flipkart Search

Search This Blog

Thursday, July 29, 2010

Put double quotes in NSString

Here is a simple way to put  double quotes in your NSString

NSString *myString = @"I say \"Hello!\"";
The output will be
I say "Hello!"

Thursday, July 8, 2010

Generate Random Color

Here is a simple method to generate a random color :-
just put this method in any class you want and put the definition in the header file

in .h file
+ (UIColor *) randomColor;

in .m file
+ (UIColor *) randomColor {
    CGFloat red =  (CGFloat)random()/(CGFloat)RAND_MAX;
    CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;
    CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}

get random color by calling this method like this....

//Class Name is the class where you have put the method
UIColor *randColor = [ClassName randomColor];

UIKeyboardBoundsUserInfoKey is deprecated, what to use instead?

From the documentation for UIKeyboardBoundsUserInfoKey:
The key for an NSValue object containing a CGRect that identifies the bounds rectangle of the keyboard in window coordinates. This value is sufficient for obtaining the size of the keyboard. If you want to get the origin of the keyboard on the screen (before or after animation) use the values obtained from the user info dictionary through the UIKeyboardCenterBeginUserInfoKey or UIKeyboardCenterEndUserInfoKey constants. Use the UIKeyboardFrameBeginUserInfoKey or UIKeyboardFrameEndUserInfoKey key instead.
Apple recommends implementing a convenience routine such as this (which could be implemented as a category addition to UIScreen):
+ (CGRect) convertRect:(CGRect)rect toView:(UIView *)view {
    UIWindow *window = [view isKindOfClass:[UIWindow class]] ? (UIWindow *) view : [view window];
    return [view convertRect:[window convertRect:rect fromWindow:nil] fromView:nil];
}
 
to recover 
window-adjusted keyboard frame size properties.I took a different approach, which involves checking the device 
orientation: 
CGRect _keyboardEndFrame;
[[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&_keyboardEndFrame];
CGFloat _keyboardHeight;
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) {
    _keyboardHeight = _keyboardEndFrame.size.height;
}
else {
    _keyboardHeight = _keyboardEndFrame.size.width;
}
 
Thanks to Alex Reynolds for the answer
 

Tuesday, July 6, 2010

Opting Out of Background Execution in iPhone OS 4.0

If you do not want your application to remain in the background when it is quit, you can explicitly opt out of the background execution model by adding the UIApplicationExitsOnSuspend key to your application’s Info.plist file and setting its value to YES. When an application opts out, it cycles between the not running, inactive, and active states and never enters the background or suspended states. When the user taps the Home button to quit the application, the applicationWillTerminate: method of the application delegate is called and the application has approximately five seconds to clean up and exit before it is terminated and moved back to the not running state.
Opting out of background execution may be preferable for certain types of applications. Specifically, if coding for the background may require adding significant complexity to your application, terminating the application may be a simpler solution. Also, if your application consumes a large amount of memory, the system might need to terminate your application quickly anyway to make room for other applications. Thus, opting to terminate, instead of switch to the background, might yield the same results and save you development time and effort.