Posts

Showing posts with the label Objective-C

Static variable in Objective-C

From time to time you will want to declare a static object which hold your data and won't be changed when your app is still running. In iOS SDK, there's no easy way to get your variable to static, however, you can you singleton design-pattern and set its instance to be static. Below are a code snippet for Static Object IOS Static:  //DataClass.h       @interface DataClass : NSObject  {         NSString *str;      }     @property(nonatomic,retain)NSString *str;     +(DataClass*)getInstance;     @end   //DataClass.m     @implementation DataClass     @synthesize str;     static DataClass *instance =nil;     +(DataClass *)getInstance     {         @synchronized(self)         {             if(instance==nil)       ...

Set UIWebView size to fit screen size

When developing iOS app for ios 7 you will have some problem with screen size (because iphone 5 is preloaded with ios 7, the screen size of iphone 5 is different with any iphone 4, iphone 3GS...). Here is a little trick to help you set any UIView to fit the screen size for example here is an UIWebView, normally I would d this: webview = [[ UIWebView alloc ] initWithFrame : CGRectMake ( 0 , 0 , 320 , 480 )]; The code above will give you a webview with a black space at the bottom of the screen when run on Iphone 5. So I will use this code to set the frame of my webview to screen size: webview . frame = CGRectMake ( 0 , 0 , self . view . frame . size . width , self . view . frame . size . height );

Show Alert Message snippet using UIAlertView

Use this when you just want to alert user without any callback for action - ( void ) showAlert: ( NSString *) title withMessage: ( NSString *) message isRedText: ( BOOL ) isRedText withButton: ( NSString *) btnText {     // open a alert with an OK     UIAlertView *alertView = [[ UIAlertView alloc ] init ];     [alertView setDelegate : nil ];     [alertView setTitle : title];     [alertView setMessage : message];     [alertView addButtonWithTitle : btnText];          UILabel *theBody = [alertView valueForKey : @"_bodyTextLabel" ];     if (isRedText) {         [theBody setTextColor :[ UIColor redColor ]];     }          [alertView show ]; } Notice that delegate of alertView is nil, if you want some callback when user press button, set delegate to self (or anything response for action), in .h file u...

IOS UniqueIdentifier in ios 7 (xCode 5)

Sometime when creating app for ios, you will want to get an UniqueIdentifier for each device (which is usually called IMEI in other phone), however in iOS, you cannot get the device IMEI easily, instead we will use the UniqueIdentifier with this syntax: NSString *UDID = [[UIDevice currentDevice] uniqueIdentifier]; This method is simple, but when Apple release an iOS 7 (and xCode 5), the code above will not work, instead it give us an compiler error like this: No visible @interface for 'UIDevice' declares the selector 'uniqueIdentifier' This is annoying and sad that Apple is such an asshole, developing app for ios is far, far more complicated compare to Android (in my opinion) due to it sand-box rule. And every time it update, there's a chance we will have to fix our app, remove some functions, change some methods...etc... The solution I use temporary is this line of code: NSString *UDID = [[UIDevice currentDevice] performSelector:@selector(uniqueIdentifi...