0

i've some javascript code on the webpage in the uiwebview that i want to use to call one of my objective c methods.

i found some code online which i decided to use. but it still doesn't seem to be working. can anyone see where the problem is?

javascript code:

function someMethod() {
window.location = "ios:webToNativeCall";
}

objective c code:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([[[request URL] absoluteString] hasPrefix:@"ios:"]) {

        // Call the given selector
        [self performSelector:@selector(webToNativeCall)];
        // Cancel the location change
        return NO;
    }
    return YES;
}

-(void)webToNativeCall
{
    //my code here
}

i'm not sure, how to use this method so it might be that i have implemented it incorrectly.

does anyone have any ideas about what could be causing this?

Thanks in advanced.

ChenSmile
  • 3,401
  • 4
  • 39
  • 69
X0r0N
  • 1,816
  • 6
  • 29
  • 50
  • You might want to check the [old inquiry][1] That can maybe help. [1]: http://stackoverflow.com/questions/1662473/how-to-call-objective-c-from-javascript – jlo Apr 15 '14 at 06:05
  • or this http://stackoverflow.com/questions/9473582/ios-javascript-bridge – geminiCoder Apr 15 '14 at 06:36

2 Answers2

1
  1. This code looks ok, please check whether delegate for UIWebView is set or not.

  2. Otherwise you can use EasyJSWebView download it from Github, it is easy to use.

Popeye
  • 11,839
  • 9
  • 58
  • 91
Martin
  • 846
  • 1
  • 9
  • 23
  • 1
    I have edited your answer to actually include the link to the Github repository for EasyJSWebView. – Popeye Apr 15 '14 at 07:21
0

You must have missed to link the delegate.

Either connect the delegate of the webView to the file owner in the .xib file

or

Use Following code

webView = [[UIWebView alloc] init];
webView.delegate = self;

in your viewDidLoad also write below code

[webView stringByEvaluatingJavaScriptFromString:@"yourJavascriptFunction()"];

Hope it helps you...

Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
Vidhyanand
  • 5,369
  • 4
  • 26
  • 59