Pages

Convert integer to string (char * or char[] ) & vice versa in C ?

  • Converting integer to string 

We can convert string to integer very easily in C but to convert integer to string stdlib do not provide any handy function. So we have to do it ourself , It is not that difficult lets write out own functions 'itoa' which will convert integer to C string.


Note: memory allocated to buffer should be released by caller of 'itoa' function.
  • Converting string to integer

It is very simple . We have in built function atoi(const char *) included in stdlib.h which converts string representation in numeric value.



Above snippets converts string "10" to numeric value 10. you can check live example HERE.

Two way communication between Javascript and ObjC in UIWebView


Webkit on OS x suports WebView which provides hundreds of APIs for interacting with html DOM objet in HTML page directly...

I think it's something simple enough that you might give it a try yourself. I personally did exactly this when I needed to do that. You might also create a simple library that suits your needs.

1. Executing JS methods from Objective-C

This is really just one line of code.


1
NSString *returnValue = [webView stringByEvaluatingJavaScriptFromString:@"javascript code string here"];

More details on the official UIWebView Documentation.

2. Execute Objective-C methods from JS

This is unfortunately slightly more complex, because there isn't the same windowScriptObject property (and class) that exists on Mac OSX allowing complete communication between the two.

However, you can easily call from javascript custom-made URLs, like:

window.location = yourscheme://callfunction/parameter1/parameter2?parameter3=value
And intercept it from Objective-C with this:


1
2
3
4
5
6
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
   NSURL *URL = [request URL]; 
   if ([[URL scheme] isEqualToString:@"customScheme"]) {
       // parse the rest of the URL object and execute functions
   } 
}

This is not as clean as it should be (or by using windowScriptObject) but it works.

3. Listen to native JS events from Objective-C (for example DOM ready event)

From the above explanation, you see that if you want to do that, you have to create some JavaScript code, attach it to the event you want to monitor and call the correct window.location call to be then intercepted.

Not so clean, But works !