开发者

Linking to a local HTML file with links to JS and CSS

开发者 https://www.devze.com 2023-02-20 05:45 出处:网络
I have a simple two page website that I would like to create an app for. I currently have a UIWebView set up to receive a local HTML file:

I have a simple two page website that I would like to create an app for.

I currently have a UIWebView set up to receive a local HTML file:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *file = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSURL *filePath = [NSURL fileURLWithPath:file];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:filePath];
    [webView loadRequest:requestObj];
}

This works fine in loading the file; however the website has JQuery plugins and CSS stylesheets attached to it that are being ignored by the iPhone. I've no idea if I've imported the files correctly so this may be part of the problem (I'm still not 100% on how X-Code links to files).

If a group in X-Code (scripts) has a file in it called slider.js could I use the html to link to scripts/slider.js or is the g开发者_如何学Pythonroups part of the environment for display only? If so, how do I link to a file that's in a folder structure like this?

Cheers for your help.


Load your request with

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL

The important part is the base URL: It tells the webview where your docroot is. Relative to the baseURL you can link your files within the html. Assuming that your jQuery and CSS files are in your bundle as well.

Put a real folder(drag a folder to your projects folder tree and select "Create Folder Reference for any added folders" it will turn blue) in your project root like htdocs. Put your html stuff in it and set the baseURL to that htdocs folder then.

0

精彩评论

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