0

I am aware that this question is already posted lot many times. But none of them are helping me to solve my issue. Please consider reading entire question before downvote :-)

So, for I have below lines of code

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL * url = [NSURL URLWithString: @"https://www.w3schools.com/code/tryit.asp?filename=FEGU9IV4SAK8"];

    NSURLRequest * request = [NSURLRequest requestWithURL:url];

    self.webView.delegate = self;
    [self.webView loadRequest:request];
}

#pragma mark - UIWebView Delegates

- (void) webViewDidFinishLoad:(UIWebView *)webView {

    NSString * jsString = [NSString stringWithFormat:@"test()"];
    [webView stringByEvaluatingJavaScriptFromString:jsString] ;


//This code is working. Able to prompt "Hello"    
//     jsString = [NSString stringWithFormat:@"alert('Hello');"];
//    [webView stringByEvaluatingJavaScriptFromString:jsString] ;
}

- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType;
{

   // ignore legit webview requests so they load normally
    if (![request.URL.scheme isEqualToString:kSchemeName]) {
        return YES;
    }


    // look at the actionType and do whatever you want here
    if ([actionType isEqualToString:kJSActionForPay]) {

        //Call objective-c method - Success 
    }

    // make sure to return NO so that your webview doesn't try to load your made-up URL
    return NO;
}

So, Far I have tried / understood.

[1] Call JS function only after web view loaded. (used delegate)

[2] Able to prompt "Hello" from

[webView stringByEvaluatingJavaScriptFromString:@"alert('Hello')"] ;

[3] Changes done in HTML side

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
</head>
<body>
<h1 id="id1">My Heading 1</h1>
<button type="button" 
onclick="document.getElementById('id1').style.color = 'red'">
Click Me!</button>
</body>
     <script> 
      function test() {
        alert("Started");
     }
     </script>
</html>

Please help to find out what I am missing in HTML Side ? or In my native Objc?

EDIT 1: As @Andy suggested, I loaded html from local. Then I am able to call JS function "test()"

3
  • "test()" not called from native code. Commented Apr 9, 2017 at 15:34
  • does this help at all ? stackoverflow.com/questions/14334047/… Commented Apr 9, 2017 at 16:20
  • @AndyDonegan Yes, it does !!! It calls JS when html loaded from Local. Commented Apr 9, 2017 at 16:50

1 Answer 1

0

You should not use the w3schools editor to be the source html for your project. The url you tried to visit not only contains your content but also the scripts used by w3schools. Instead, you should put your html file on your own server or a .html file inside your project. Then, you can call the test() method.

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

1 Comment

Hey @chengsam, you are right ! It works when the same html loaded from loal( main bundle) Thanks

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.