如何在 WebView 中为类型 html 加载 url?
How can load url for type html in WebView?
我想在 WebView 中加载 html 类型的 URL 字符串:“http://www........html”。
我尝试了以下代码,但它不起作用。谁能帮帮我吗?
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.link]];
[self.webView loadRequest:request];
试试这个,
将 link 另存为“.html”并添加到您的项目中。
然后这样做。
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];
编辑
NSURL *url = [NSURL URLWithString:@"Your Link.html"];
NSString*content = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
[webView loadHTMLString:htmlString baseURL:nil];
NSString *url = self.link;
[self.webView loadHTMLString:[NSString stringWithFormat:
@"<script>window.location=%@;</script>",
[[NSString alloc]
initWithData:[NSJSONSerialization
dataWithJSONObject:url
options:NSJSONReadingAllowFragments
error:NULL]
encoding:NSUTF8StringEncoding]
]
baseURL:nil];
我想在 WebView 中加载 html 类型的 URL 字符串:“http://www........html”。 我尝试了以下代码,但它不起作用。谁能帮帮我吗?
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.link]];
[self.webView loadRequest:request];
试试这个,
将 link 另存为“.html”并添加到您的项目中。 然后这样做。
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];
编辑
NSURL *url = [NSURL URLWithString:@"Your Link.html"];
NSString*content = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
[webView loadHTMLString:htmlString baseURL:nil];
NSString *url = self.link;
[self.webView loadHTMLString:[NSString stringWithFormat:
@"<script>window.location=%@;</script>",
[[NSString alloc]
initWithData:[NSJSONSerialization
dataWithJSONObject:url
options:NSJSONReadingAllowFragments
error:NULL]
encoding:NSUTF8StringEncoding]
]
baseURL:nil];