簡體   English   中英

在Webview中加載Javascript

[英]Loading Javascript in webview

我真的陷入了需要在Webview中加載HTML和Javascript代碼的情況。 如果使用html文件加載它,則可以正常工作,但是當我嘗試將其作為字符串加載時卻無法正常工作。我需要將其作為字符串加載,因為我想動態更改HTML中的url。我認為問題出在HTML字符串,有人可以幫我嗎。

  NSString *embedHTML = [NSString stringWithFormat:@"<HTML><HEAD><BODY>"
                           "<div id=\"fb-root\">sfd</div><script>(function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = \"http://connect.facebook.net/en_US/all.js#xfbml=1&appId=251520118193948\"; fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));</script>""<fb:comments-count href=http://abcd.com></fb:comments-count>"
                           "<BR></BODY></HTML>"];


    [webView loadHTMLString:embedHTML baseURL:nil];

非常簡單:請使用HEADALEX,BODYALEX插入html中的u boundle index.html,並替換為u字符串

NSURL *indexURL = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
NSError *error = nil;

NSString *index = [NSString stringWithContentsOfURL:indexURL encoding:NSUTF8StringEncoding error:&error];

index = [index stringByReplacingOccurrencesOfString:@"HEADALEX" withString:htmlCodeHead];
index = [index stringByReplacingOccurrencesOfString:@"BODYALEX" withString:htmlCode];
[index writeToURL:indexURL atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (error) NSLog(@"PHONE CONFIG: write to file html error:%@",error);

[adsView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"] isDirectory:NO]]];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM