I have looked at various similar questions and answers and still cannot get this to work, so I\'m adding my own question:
I\'m playing with UIWebView. I can create a
change the baseURL of the UIWebView to the url of your mainbundle.
NSURL *mainBundleURL = [[NSBundle mainBundle] bundleURL];
[self.webView loadHTMLString:htmlString baseURL:mainBundleURL];
Swift 3:
webView.loadHTMLString(contentString, baseURL: Bundle.main.bundleURL)
I thought I should post the entire code block to access an external CSS file. Hopefully, it will be useful to others:
- (void)viewDidLoad
{
NSURL *mainBundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *path = [[NSBundle mainBundle] pathForResource:@"mypagename" ofType:@"html"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData:
[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];
[self.webView loadHTMLString:htmlString baseURL:mainBundleURL];
[htmlString release];
}
NSURL *BundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"name your style" ofType:@"css"]];
webView.opaque = NO;
webView.backgroundColor=[UIColor clearColor];
[webView loadHTMLString:htmlString baseURL:BundleURL];
I think i have figured it out. the htmlString is simply an NSString we can replace text in it. this code worked for me
NSString *info = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"your html file name" ofType:@"html"] encoding: NSUTF8StringEncoding error: &error];
info=[info stringByReplacingOccurrencesOfString:@"styles.css" withString:@"stylesIPad.css"];