Reputation: 13354
I a trying to view an .html file (index.html) that is stored in my Bundle (in my Supporting Files).
The .html file sits in a folder called HTML. My code is as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
_viewWeb.delegate = self;
NSString *path = [[NSBundle mainBundle]
pathForResource:@"index" ofType:@"html" inDirectory:@"HTML"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_viewWeb setScalesPageToFit:YES];
[self.viewWeb loadRequest:request];
}
My header file looks as follows:
@interface D6ViewController : UIViewController <UIWebViewDelegate>
{
IBOutlet UIWebView *viewWeb;
}
@property (weak, nonatomic) IBOutlet UIWebView *viewWeb;
@end
I synthesized property as viewWeb = _viewWeb
. The viewcontroller holding the UIWebView loads fine but shows a white screen with no webpage. I have set the outlets in the IB.
Any ideas? Thanks,
Upvotes: 1
Views: 3547
Reputation: 7
You can use this If you are not saving your HTML file in any directory.If you are saving your HTML file in any directory then specify the directory name in "inDirectory" parameter.
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"privacy-Policy" ofType:@"html" inDirectory:nil];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[_webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];
Upvotes: 0
Reputation: 1
You are wrong, here is the correct code:
var requestURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("index", ofType: "html", inDirectory: "www")!)
var request = NSURLRequest(URL:requestURL!)
myWebView.loadRequest(request)
Upvotes: 0
Reputation: 1275
In Swift:
func pathForResource(name: String?, ofType ext: String?, inDirectory subpath: String?) -> String?
- name: Name of Hmtl;
- ofType ext: extension for type of file. In this case "html";
- inDirectory subpath: the folder where are the file. In this case the file is in root folder;
let path = NSBundle.mainBundle().pathForResource("dados", ofType: "html", inDirectory: "root")
var requestURL = NSURL(string:path!);
var request = NSURLRequest(URL:requestURL);
webView.loadRequest(request)
Upvotes: 0
Reputation: 1791
try this
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
Upvotes: 1
Reputation: 12243
You are using a relative path (hence the blue color of a folder). You can actually find the answer to this problem here Load resources from relative path using local html in uiwebview or below:
Drag the resource into your xcode project, you will get two options "create groups for any added folders" and "create folders references for any added folders". Select the "create folder references.." option.
The code below should work.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"/HTML"]];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
Upvotes: 2