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.

2

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I have edited your answer to actually include the link to the Github repository for EasyJSWebView.
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...

1 Comment

How does this answer the question? Have you read the question? -1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.