mad*_*erz 165 iphone cocoa-touch objective-c uiwebview ios
我正在尝试将html文件加载到我的UIWebView中,但它不起作用.这是舞台:我的项目中有一个名为html_files的文件夹.然后我在界面构建器中创建了一个webView,并在viewController中为它分配了一个插座.这是我用来附加html文件的代码:
-(void)viewDidLoad
{
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"html" inDirectory:@"html_files"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];
[super viewDidLoad];
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,UIWebView是空白的.我很感激一些帮助.
use*_*681 269
可能最好使用NSString并加载html文件,如下所示:
Objective-C的
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL: [[NSBundle mainBundle] bundleURL]];
Run Code Online (Sandbox Code Playgroud)
迅速
let htmlFile = NSBundle.mainBundle().pathForResource("fileName", ofType: "html")
let html = try? String(contentsOfFile: htmlFile!, encoding: NSUTF8StringEncoding)
webView.loadHTMLString(html!, baseURL: nil)
Run Code Online (Sandbox Code Playgroud)
Swift 3几乎没有变化:
let htmlFile = Bundle.main.path(forResource: "intro", ofType: "html")
let html = try? String(contentsOfFile: htmlFile!, encoding: String.Encoding.utf8)
webView.loadHTMLString(html!, baseURL: nil)
Run Code Online (Sandbox Code Playgroud)
你试过了吗?
还要检查是否通过pathForResource:ofType:inDirectory调用找到了资源.
Nea*_*rdt 90
编辑2016-05-27 - loadRequest暴露"一个通用的跨站点脚本漏洞." 确保拥有您加载的每个资产.如果加载坏脚本,它可以加载它想要的任何东西.
如果您需要相关链接在本地工作,请使用以下命令:
NSURL *url = [[NSBundle mainBundle] URLForResource:@"my" withExtension:@"html"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
Run Code Online (Sandbox Code Playgroud)
捆绑包将搜索项目的所有子目录以进行查找my.html.(目录结构在构建时变平)
如果my.html有标签<img src="some.png">,webView some.png将从您的项目加载.
AJP*_*tel 40
通过这个,您可以加载项目Assets(bundle)中的html文件到webView.
UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"test" ofType:@"html"]isDirectory:NO]]];
Run Code Online (Sandbox Code Playgroud)
这可能对你有用.
我想你需要allocate并初始化你的webview第一个::
- (void)viewDidLoad
{
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"html" inDirectory:@"html_files"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
webView = [[UIWebView alloc] init];
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];
[super viewDidLoad];
}
Run Code Online (Sandbox Code Playgroud)
一个简单的复制粘贴代码片段:
-(void)LoadLocalHtmlFile:(NSString *)fileName onWebVu:(UIWebView*)webVu
{
[webVu loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:fileName ofType:@"html"]isDirectory:NO]]];
}
Run Code Online (Sandbox Code Playgroud)
注意:
确保检查html文件的Target成员资格,否则将抛出异常: -

由于未捕获的异常而终止应用程序
'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:isDirectory:]: nil string parameter'
对于Swift 3和Swift 4:
let htmlFile = Bundle.main.path(forResource: "name_resource", ofType: "html")
let html = try! String(contentsOfFile: htmlFile!, encoding: String.Encoding.utf8)
self.webView.loadHTMLString(html, baseURL: nil)
Run Code Online (Sandbox Code Playgroud)
小智 5
UIWebView *web=[[UIWebView alloc]initWithFrame:self.view.frame];
//[self.view addSubview:web];
NSString *filePath=[[NSBundle mainBundle]pathForResource:@"browser_demo" ofType:@"html" inDirectory:nil];
[web loadRequest:[NSURLRequest requestWhttp://stackoverflow.com/review/first-postsithURL:[NSURL fileURLWithPath:filePath]]];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
190879 次 |
| 最近记录: |