2

Spent a whole evening googling this and still no dice. I'm just trying to pass a string to an embedded html/javascript test file. I'm not getting any errors but I'm also not getting the function called either :[

Heres what i got code wise:

DetailedController.h

#import <UIKit/UIKit.h>

@interface DetailController : UIViewController<UIWebViewDelegate>{
    UIWebView *webView;
}


@property (nonatomic, strong) NSArray *Detailinfo;
@property (nonatomic, strong) IBOutlet UILabel *DetailName;
@property (nonatomic, strong) IBOutlet UILabel *dob;
@property (nonatomic, retain) IBOutlet UIWebView *webView;


@end

DetailedController.m

#import "DetailController.h"

@interface DetailController ()


@end

@implementation DetailController
@synthesize webView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];




    // Do any additional setup after loading the view.
    self.navigationItem.hidesBackButton = YES;
    UINavigationItem *topBar;
    topBar.title = self.Detailinfo[0];
    self.DetailName.text = self.Detailinfo[0];
    self.dob.text = self.Detailinfo[1];


    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"javascriptTest" ofType:@"html"]];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:requestObj];
    self.webView.delegate = self;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

    [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"loadText(%@)", self.Detailinfo[1]]];

}





@end

and the HTML file javascriptTest.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test JavaScript</title>
<script>
function loadText(txtString)
{
    var divd = document.createElement('div');
    divd.innerHTML = "<p>" + txtString + "</p>";
    document.body.appendChild(divd);
    document.getElementById('info').value = "";
}
</script>
</head>
<body>
    loaded
<!--
<input type="text" id="info" />
<button onclick="loadText(document.getElementById('info').value)"> Load Text </button> 
-->
</body>
</html>

any help is much appreciated :]

UPDATE As @ACB said, just needed to add '' inside the javascript function @"loadText('%@')"

3
  • Shouldn't it be @"loadText('%@')" Commented Feb 6, 2013 at 7:54
  • @ACB is right. And you may need to escape the string just in case it has any " character inside. There's no short of questions to help you do that actually ;) Commented Feb 6, 2013 at 8:05
  • @ACB the '' where what did it! Commented Feb 6, 2013 at 16:29

1 Answer 1

2

You need to change,

@"loadText(%@)"

to

@"loadText('%@')"

Since you are passing a string on to this javascript function, it should be inside quotes.

As Fabio said, you might have to escape the string if it has any " inside it.

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

Comments

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.