Posts

Showing posts from January, 2014

IOS: Call javascript function from native code

Today I've some work with UIWebview and Javascript. I'm using NativeBridge to call ios function from Javascript. It's quite simple, so I've decided to take note for any further use in the future. NSString *Data = @"Data You Want To Pass To Javascript Function"; [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"jsFunc('%@')",Data]]; Notice that when you're passing a string to a javascript function, you must put that data inside a pair of ' sign.

No visible @interface for class declares the selector

This is one of the most annoying bug I have with XCode (there're many, but this one is annoying as hell). I've to google for solution without any result for 2 times for this same bug. There won't be the third. This bug happens when user declare new function in .h file, then goes to another class to use it and the error occur. I've check all the syntax, since it's a very simple getter, the code is just 3 lines long. I've cleaned the project, tried to restart xCode...etc...etc... and everything is correct . And the solution is dead simple: - Go to the Using class, remove the import line. - Import .h file of the used class again, and voila, xCode let it ride the wind again (although there's nothing change). Note: This issue is specific to the case when xCode is fucking you up. In many cases, this error will indicate that your code is wrong somewhere. Hope this solution can help you all.

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...