开发者

stringWithContentsOfFile works, but requestWithURL doesn't

开发者 https://www.devze.com 2023-01-18 13:23 出处:网络
For some reason, the first method doesn\'t display anything, while the second does: //Common NSString *path=[[NSBundle mainBundle] pathForResource:@\"about\" ofType:@\"html\"];

For some reason, the first method doesn't display anything, while the second does:

//Common
NSString *path=[[NSBundle mainBundle] pathForResource:@"about" ofType:@"html"];
//Method1
[webView loadRequest:[NSURLRequest requestWithURL: [NSURL fileURLWithPath: path isDirectory: NO]]];
//Method 2
NSString *HTML=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
[webView loadHTMLString:HTML baseURL:[NSURL URLWithString:[[NSBundle mainBundle] bundlePath]]];
//Common        
UIViewController* con开发者_运维百科troller=[webView controllerWithTitle:@"Help"];
[delegateRef.navController pushViewController:controller animated:YES];

On the other hand, I also tried loading it using a URL as well. Similarly, the first method didn't display anything while the second did.

//Common
NSURL *url=[[NSBundle mainBundle] URLForResource:@"about" withExtension:@"html"];
//Method 1
[webView loadRequest:[NSURLRequest requestWithURL:url]];
//Method 2
NSString *HTML=[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:NULL];
[webView loadHTMLString:HTML baseURL:[NSURL URLWithString:[[NSBundle mainBundle] bundlePath]]];

Strangely, if I change the URL as follows, then both methods work (of course loading the string doesn't show css):

NSURL *url=[NSURL URLWithString:@"http://www.google.com"];

controllerWithTitle is defined in a category as follows:

-(UIViewController*) controllerWithTitle:(NSString*)title{
    UIViewController *vc=[[[UIViewController alloc] init] autorelease];
    vc.view=self;
    vc.title=title;
    return vc;
}


It turns out that the CSS was causing the entire page to not display!

0

精彩评论

暂无评论...
验证码 换一张
取 消