webview的使用方法使用

來源:互聯網
上載者:User

標籤: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的使用方法使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.