iOS之NSURLCache、ASI設定緩衝及使用步驟

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   ar   使用   

  1 一NSURLCache  2 緩衝的使用步驟  3 // 獲得全域的緩衝對象  4 NSURLCache *cache = [NSURLCache sharedURLCache];  5   6 // 設定緩衝容量  7 cache.memoryCapacity = 1024 * 1024;  8 cache.diskCapacity = 20 * 1024 * 1024;  9  10 // 佈建要求的緩衝策略 11 request.cachePolicy = NSURLRequestReturnCacheDataElseLoad; 12  13 二、ASI 14 1.緩衝的使用步驟 15 1> 緩衝單個請求 16 // 1.獲得全域的緩衝對象(決定緩衝的儲存路徑, 儲存到什麼地方) 17 ASIDownloadCache *cache = [ASIDownloadCache sharedCache]; 18 // 設定預設的緩衝載入策略 19 cache.defaultCachePolicy = ASIDoNotReadFromCacheCachePolicy; 20  21 // 2.佈建要求對象的緩衝對象(使用哪個緩衝對象) 22 request.downloadCache = cache; 23  24 // 3.佈建要求對象的緩衝載入策略 25 request.cachePolicy = ASIOnlyLoadIfNotCachedCachePolicy; 26 // 如果沒有緩衝, 才發送請求 27  28 // 4.佈建要求對象的緩衝儲存策略(儲存的時間長度) 29 request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy; 30 // 永久儲存 31  32 注意, 緩衝載入策略的優先順序 : request.cachePolicy > cache.defaultCachePolicy 33  34 2> 緩衝所有請求 35 // 1.獲得全域的緩衝對象(決定緩衝的儲存路徑, 儲存到什麼地方) 36 ASIDownloadCache *cache = [ASIDownloadCache sharedCache]; 37 // 設定預設的緩衝載入策略 38 cache.defaultCachePolicy = ASIOnlyLoadIfNotCachedCachePolicy; 39  40 // 2.設定全域緩衝對象 41 [ASIHTTPRequest setDefaultCache:cache]; 42  43 2.發送請求 44 1> 同步請求 45 [request startSynchronous]; 46  47 2> 非同步請求 48 [request startAsynchronous]; 49  50 3.GET\POST 51 1> GET請求 52 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 53  54 2> POST請求 55 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 56 // 添加普通參數(非檔案參數) 57 [request setPostValue:@"zhangsan" forKey:@"username"]; 58 [request setPostValue:@"123" forKey:@"pwd"]; 59  60 4.檔案下載 61 // 檔案的儲存路徑(檔案下載到什麼地方) 62 request.downloadDestinationPath = filepath; 63 // 設定下載代理(監聽下載進度) 64 request.downloadProgressDelegate = self.circleView; 65 // 支援斷點下載 66 request.allowResumeForFileDownloads = YES; 67  68 5.檔案上傳 69 // 添加檔案參數(file : 需要上傳檔案的路徑) 70 [request setFile:file forKey:@"file"]; 71 [request setFile:file withFileName:@"123.txt" andContentType:@"text/plain" forKey:@"file"]; 72 [request setData:data withFileName:@"minion.png" andContentType:@"image/png" forKey:@"file"]; 73  74 // 設定上傳代理(監聽上傳進度) 75 request.uploadProgressDelegate = self.circleView; 76  77 6.監聽請求的過程 78 1> 代理方法 79 // 設定代理 80 request.delegate = self; 81 // 遵守協議 82 ASIHTTPRequestDelegate 83 // 實現協議中的代理方法 84 - (void)requestStarted:(ASIHTTPRequest *)request; 85 - (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data 86 - (void)requestFinished:(ASIHTTPRequest *)request; 87 - (void)requestFailed:(ASIHTTPRequest *)request; 88  89 2> SEL 90 // 設定代理 91 request.delegate = self; 92 // 設定方法名 93 [request setDidStartSelector:@selector(start)]; // 開始發送請求, 就會調用代理的start方法 94 // .... 95  96 3> block 97 [request setStartedBlock:^{ 98     NSLog(@"setStartedBlock ----"); 99 }];100 101 [request setDataReceivedBlock:^(NSData *data) {102     NSLog(@"setDataReceivedBlock ----");103 }];104 105 [request setCompletionBlock:^{106     NSLog(@"setCompletionBlock ----");107 }];108 109 [self setFailedBlock:^{110     NSLog(@"setFailedBlock ----");111 }];112 113 7.通過request對象獲得伺服器的響應114 1> 獲得回應標頭資訊115 @property (atomic, retain) NSDictionary *responseHeaders;116 117 2> 獲得響應體(實體內容)118 - (NSData *)responseData; // 直接返回伺服器的位元據119 - (NSString *)responseString; // 將位元據轉成字串(方便調試)

 

iOS之NSURLCache、ASI設定緩衝及使用步驟

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.