Thursday, January 17, 2013

javascript to android injection


step 1 ) register you class with an interface , open eclipse project and write

Launchs _accessors = new Launchs();

 web.addJavascriptInterface(new Launchs(), "test_callback");

this will register interface


now define your class

class Launchs{
              
            public void nativeLaunch(String url){
               
               
               
                Log.d("aaa",url);
            }
               
            }



#############

now to call native method from javascript   just use this code in html file.( this html is loaded in webview)

html code

window.test_callback.nativeLaunch("paramater"); / this will call native method  in android....

Cheers :)


Javascript to Obj-c Injection (iphone)

To control in UIwebview  via javascript  Injection

Here we are overriding window.open method so that we can launch  this on separate browser rather than opening in webview .

- (void)webViewDidFinishLoad:(UIWebView *)myWebView {
    [self.webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
    
     "script.text = \"var open_=window.open ;window.open=function (url ,name)  { var prefix='ns://'; "
     "open_(prefix+url);return (prefix+url)"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
  
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
      
   
}




 And then handle the special case in

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
   
   
    if ([[[inRequest URL] scheme] isEqualToString:@"ns"] == YES)
    {
        [self.webView stopLoading];
               NSLog(@"it works do what ever you want ");
      

}



FEEDJIT Live