简体   繁体   中英

Calling javascript function from in objective c using webview methods

Am trying to call javascript function through objective C

Steps I have followed:

In Html Page:

<!DOCTYPE html>
<html>
<head>
<script>
function init()
{
  alert("Hello Contraceptive_Quiz!");
  setTimeout(function(){destroy()},1000);
}
function destroy()
{
 alert("calling destroy Contraceptive_Quiz!");
}
</script>
</head>
<body>
</body>
</html>

In ObjectiveC.m file i have used,

In my app bundle wwwfilePath folder exist:

wwwfilePath structure contains : www/IYG_G8_L05/IYG_G8_L05_Directory/IYGindex1.html

Here, In this structure contains www/IYG_G8_L05/ Many sub folders

www/IYG_G8_L05/IYG_G8_L05_Directory1

www/IYG_G8_L05/IYG_G8_L05_Directory2

www/IYG_G8_L05/IYG_G8_L05_Directory3

So,I need to load multiple contents in html page one by one

first time am loading IYG_G8_L05_Directory1 and then next like ...

- (void)viewDidLoad{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"IYGindex1" ofType:@"html" inDirectory:wwwfilePath];
NSURL *htmlurl =    [[NSURL alloc]initFileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:htmlurl];
[_contentWebView loadRequest:request];
}

In webview delegate methods:

** calling javascript functions in objective c**

-(void) webViewDidFinishLoad:(UIWebView *)webView {
NSString *jsString = [[NSString alloc] initWithFormat: @"init()"];
[_contentWebView stringByEvaluatingJavaScriptFromString:jsString];
}

Here, init( ) method will execute and also executes destroy( ) method because of calling setTimeout( ) function in html page.

My issue is: Once the destroy ( ) method executes, how can i load the next wwwfilepath ie, IYG_G8_L05_Directory2 it shoud be call in dynamical way.

Let us know how to resolve?

Solution 1: For loading content of HTML file dynamically, you could try using AJAX call using XMLHttpRequest().

In below code a url is sent to the loadHTMLURL() function, in which the loaded content is returned.

function destroy()
{
   alert("calling destroy Contraceptive_Quiz!");
   document.createElement("body").innerHTML = loadHTMLURL("IYG_G8_L05_Directory1/NEW_HTML.html");
}
function loadHTMLURL(href)
{
   var xmlHTTPObj = new XMLHttpRequest();
   xmlHTTPObj.open("GET", href, false);
   xmlHTTPObj.send();
   return xmlHTTPObj.responseText;
}

I haven't run this code. So carefully check html file url and try into your code.

================================

Solution 2:

Objective-c Code:

NSTime *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(_timerFired:) userInfo:nil repeats:YES];
- (void)_timerFired:(NSTimer *)timer {
    NSString *function = [[NSString alloc] initWithFormat: @"destroy(%@)", @"IYG_G8_L05/IYG_G8_L05_Directory1"];
    NSString *result = [webView stringByEvaluatingJavaScriptFromString:function];
}

Javascript Code:

function destroy(fileNameWithURL)
    {
       alert("calling destroy Contraceptive_Quiz!");
       document.createElement("body").innerHTML = loadHTMLURL(fileNameWithURL);
    }

This above code may helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM