標籤:objective-c ios webview
- (IBAction)testLoadHTMLSting:(id)sender { // 設定首頁檔案的基本路徑 // 檔案名稱為“index.html” // [NSBundle mainBundle]是為了擷取當前項目地址 NSString *htmlPath = [[NSBundle mainBundle]pathForResource: @"index" ofType:@"html"]; // 擷取html資源路徑 NSURL *bundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; NSError *error = nil; // 先指定編碼的字元集,然後將htmlPath載入進入進行編碼 // 裝載WebView的時候必須指定字元集! // 將html的內容存入NSString裡 NSString *html = [[NSString alloc] initWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:&error]; if (error == nil) { // loadHTMLString用來擷取html的路徑 [self.webView loadHTMLString:html baseURL:bundleURL]; } }- (IBAction)testLoadData:(id)sender { // 設定首頁檔案!的基本路徑,通過一個HTML字串載入首頁資料 NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; // 擷取首頁檔案的資源路徑 NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; NSError *error = nil; // 這裡是NSData NSData *htmlData = [[NSData alloc] initWithContentsOfFile:htmlPath]; if (error == nil) { // 由於是NSData因此需要指定字元集UTF-8 [self.webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:bundleUrl]; }}- (IBAction)testLoadRequest:(id)sender { // string 存成 NSURL NSURL *url = [NSURL URLWithString:@"http://www.51work6.com"]; // 發起非同步請求 NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.webView loadRequest:request]; self.webView.delegate = self;}
webview的使用方法使用