标签:
这样,再看看介绍里面的例子。应该就能看懂普通用法了。然后畅快的使用MKNetworkKit库了。
上次写的ASIHTTPRequest续传其实已经可以用了。但是为什么需要重新写呢。就是重用!!!
一直都很少自己设计接口什么的。其实只是做了写体力的劳动,把一堆逻辑换成代码给堆了起来。觉得没有人会去重用自己的代码。很少去花心思想怎么样才能写好。
这次改写就只有一个目标,为了更好更多人能使用断点续传来 功能
每次进入 暂停恢复 可以随时暂停下载
这次改写,这些功能都实现了,啊哈哈哈。
最开始使用MKNetworkKit库做
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSUInteger size = [self.response expectedContentLength] < 0 ? 0 : [self.response expectedContentLength];
self.response = (NSHTTPURLResponse*) response;
// dont‘ save data if the operation was created to download directly to a stream.
if([self.downloadStreams count] == 0)
self.mutableData = [NSMutableData dataWithCapacity:size];
else
self.mutableData = nil;
for(NSOutputStream *stream in self.downloadStreams)
[stream open];
NSDictionary *httpHeaders = [self.response allHeaderFields];
// if you attach a stream to the operation, MKNetworkKit will not cache the response.
// Streams are usually "big data chunks" that doesn‘t need caching anyways.
if([self.request.HTTPMethod isEqualToString:@"GET"] && [self.downloadStreams count] == 0) {
// We have all this complicated cache handling since NSURLRequestReloadRevalidatingCacheData is not implemented
// do cache processing only if the request is a "GET" method
NSString *lastModified = [httpHeaders objectForKey:@"Last-Modified"];
NSString *eTag = [httpHeaders objectForKey:@"ETag"];
NSString *expiresOn = [httpHeaders objectForKey:@"Expires"];
NSString *contentType = [httpHeaders objectForKey:@"Content-Type"];
// if contentType is image,
NSDate *expiresOnDate = nil;
if([contentType rangeOfString:@"image"].location != NSNotFound) {
// For images let‘s assume a expiry date of 7 days if there is no eTag or Last Modified.
if(!eTag && !lastModified)
expiresOnDate = [[NSDate date] dateByAddingTimeInterval:kMKNetworkKitDefaultImageCacheDuration];
else
expiresOnDate = [[NSDate date] dateByAddingTimeInterval:kMKNetworkKitDefaultImageHeadRequestDuration];
}
NSString *cacheControl = [httpHeaders objectForKey:@"Cache-Control"]; // max-age, must-revalidate, no-cache
NSArray *cacheControlEntities = [cacheControl componentsSeparatedByString:@","];
for(NSString *substring in cacheControlEntities) {
if([substring rangeOfString:@"max-age"].location != NSNotFound) {
// do some processing to calculate expiresOn
NSString *