IOS uiwebview中加載服務器上的html圖片不顯示

uiwebview顯示服務器上的html,圖片顯示成問號,在電腦上用瀏覽器打開,圖片顯示403。但是將html保存到本地後打開就可以顯示。

所以我在IOS中也是將服務器返回的html內容保存到了本地,然後加載到uiwebview中,可以顯示。

- (void)viewDidLoad {
    [super viewDidLoad];
    
    webViews.delegate=self;
    //自適應寬度
    webViews.scalesPageToFit=YES;
    
    webViews.backgroundColor=[UIColor whiteColor];
    webViews.scrollView.bounces=YES;
    
    //iOS7 NavigationBar覆蓋內容
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = NO;
    [self clearBlackBottom];
    
    //清除緩存
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage=[NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies]) {
        [storage deleteCookie:cookie];
    }
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    
    //因爲一些網頁上的圖片在線顯示不出來,我就先將html內容保存到本地 然後加載本地文件
    //獲取文件路徑
    //沙盒中文件
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath=[path stringByAppendingPathComponent:@"detail.html"];
    //本地文件
//    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"detail" ofType:@"html"];

    //待寫入的數據
    //從服務器獲取數據
    NSString *string = [NSString stringWithFormat:@"%@docdetail/detail?doc_id=%@", BaseURLString,self.data];
    NSString *htmlResponseStr=[NSString stringWithContentsOfURL:[NSURL URLWithString:string] encoding:NSUTF8StringEncoding error:nil];
    //創建數據緩衝
    NSMutableData *writer = [[NSMutableData alloc] init];
    //將字符串添加到緩衝中
    [writer appendData:[htmlResponseStr dataUsingEncoding:NSUTF8StringEncoding]];
    //將緩衝的數據寫入到文件中
    [writer writeToFile:filePath atomically:YES];
    
    //加載本地文件
    NSURL *localUrl = [NSURL fileURLWithPath:filePath];
    NSURLRequest *localRequest = [NSURLRequest requestWithURL:localUrl];
    //打開網頁
    [webViews loadRequest:localRequest];
    
}



發佈了119 篇原創文章 · 獲贊 6 · 訪問量 51萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章