美文网首页
NSURLCache

NSURLCache

作者: 我是小胡胡分胡 | 来源:发表于2017-08-16 19:28 被阅读5次

    cache
    1、afnetworking默认的NSURLSessionConfiguration

        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
        //TODO set the default HTTP headers
    
        configuration.HTTPShouldSetCookies = YES;
        configuration.HTTPShouldUsePipelining = NO;
    
        configuration.requestCachePolicy = NSURLRequestUseProtocolCachePolicy;
        configuration.allowsCellularAccess = YES;
        configuration.timeoutIntervalForRequest = 60.0;
        configuration.URLCache = [AFImageDownloader defaultURLCache]; // NSURLCache
        return configuration;
    
    
    
    + (NSURLCache *)defaultURLCache {
        return [[NSURLCache alloc] initWithMemoryCapacity:20 * 1024 * 1024
                                             diskCapacity:150 * 1024 * 1024
                                                 diskPath:@"com.alamofire.imagedownloader"];
    }
    

    2、系统默认的NSURLSessionConfiguration

        if (!configuration) {
            configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        }
    
    
    [NSURLSession sessionWithConfiguration:self.sessionConfiguration delegate:self delegateQueue:self.operationQueue]
    
    
    

    3、UIWebView
    UIWebView+AFNetworking// 替换UIWebView加载网页的默认的方法- (void)loadRequest:(NSURLRequest *)request;

    用NSURLSession下载数据,然后用UIWebview下面的方法加载URLSession加载成的数据

    
    - (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL; 
    
    
    
    
        NSURLSessionDataTask *dataTask;
        dataTask = [self.sessionManager
                GET:request.URL.absoluteString
                parameters:nil
                progress:nil
                success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
                    __strong __typeof(weakSelf) strongSelf = weakSelf;
                    if (success) {
                        success((NSHTTPURLResponse *)task.response, responseObject);
                    }
                    [strongSelf loadData:responseObject MIMEType:MIMEType textEncodingName:textEncodingName baseURL:[task.currentRequest URL]];
    
                    if ([strongSelf.delegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
                        [strongSelf.delegate webViewDidFinishLoad:strongSelf];
                    }
                }
                failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
                    if (failure) {
                        failure(error);
                    }
                }];
        self.af_URLSessionTask = dataTask;
    
    
    

    相关文章

      网友评论

          本文标题:NSURLCache

          本文链接:https://www.haomeiwen.com/subject/lrkwrxtx.html