UIWebView除了能加載網(wǎng)頁地址外還可以加載本地html,加載的方式主要有兩種
讀取本地html內(nèi)容,加載內(nèi)容
NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL URLWithString:mainBundlePath];
NSString *htmlPath = [NSString stringWithFormat:@"%@/index.html", mainBundlePath];
NSString *htmlContent = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
[_webView loadHTMLString: htmlContent baseURL:baseURL];
通過本地html地址加載內(nèi)容
NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL URLWithString:mainBundlePath];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:(NSTimeInterval)10.0 ];
[_webView loadRequest:request];
有些情況下,我們不僅要能加載本地html,還需要給本地html傳遞參數(shù),這種情況下我們只能通過方法二來進(jìn)行參數(shù)傳遞
NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL URLWithString:mainBundlePath];
NSString *queryString = [NSString stringWithFormat:@"%@/index.html?key=value", mainBundlePath];
NSURL *queryURL = [NSURL URLWithString:queryString];
NSURLRequest *request = [NSURLRequest requestWithURL:queryURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:(NSTimeInterval)10.0 ];
[_webView loadRequest:request];