2

I need to fill a html webview with variables that I have stored in objective C.

I believe that I need to use:

[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"John", firstName];

but not sure what to put there and what my javascript needs to be to implement the passed variables.

here is a little bit of my html to help get an idea:

<ul><li><span>First Name:</span> first name needs to go here</li>
  <li><span>Last Name:</span> last name needs to go here</li>

Updated code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"idcard" ofType:@"html"]isDirectory:NO]]];
}

-(void)viewDidAppear:(BOOL)animated
{
    NSString *str  = @"John";
    NSString *script = [NSString stringWithFormat:@"myFunc('%@')", str];
    [self.webView stringByEvaluatingJavaScriptFromString:script];


}

Update 2: javascript/html:

<ul><li><span>Policy Number:</span>        
<script> function myFunc(str)
        {
            document.write(str)  }
        </script>

3 Answers 3

5

I ended up using a different js approach and used, hopefully this is helpful to someone.

obj c:

NSString *str  = @"John";
NSString *script = [NSString stringWithFormat:@"document.getElementById('name').innerHTML = '%@';", str];
[self.webView stringByEvaluatingJavaScriptFromString:script];

js:

<li><span>Name:</span>   <span id = 'name'>name goes here</span>
      </li>
Sign up to request clarification or add additional context in comments.

Comments

1

Basically you need a javascript method to add the data to the HTML string. Once you have the function, passing data to it is fairly straight forward,

NSString *script = [NSString stringWithFormat:@"methodName([%@])", data]
[self.webView stringByEvaluatingJavaScriptFromString:script];

And writing Javascript to place data in HTML string is easy.

4 Comments

Is methodName the javascript function name?
I am getting a blank output in my html. Can you look at my updated code and see where I am going wrong?
I am sorry, you haven't written a method. Look at this(stackoverflow.com/questions/5400230/… uses JQuery nevertheless it should give you an idea.
I updated my javascript again, now when the view does appear the html goes away and im left with a blank screen with just the variable name I passed. (In this case "John") also changed methodName([%@]) to ('%@) not sure if that affects anything
0

Do you have access to the raw HTML string in your code? If so, can you do something like that?

UIWebView *webview = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:webview];
NSString *htmlString = [NSString stringWithFormat:@"<html>\
<ul><li><span>First Name:</span> %@</li>\
<li><span>Last Name:</span> %@</li>\
</html>", @"John", @"Smith"];
[webview loadHTMLString:htmlString baseURL:nil];

If you want to use JavaScript, here's an example: How to control UIWebView's javascript

1 Comment

Although this would and does work it would be a very time consuming to add all the appropriate quotes and such to get it in the correct format.

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.