缓存策略不能与php webservice IOS SDK一起使用

时间:2015-09-03 10:45:28

标签: php ios web-services caching nsurlconnection

我正在使用第三方api进行json响应,我也使用cachePolicy:NSURLRequestReturnCacheDataElseLoad来快速响应,但是现在我们已经开发了自己的api但现在cachePolicy:NSURLRequestReturnCacheDataElseLoad无法正常工作,我的意思是它不是缓存数据

NSString *urlAsString = @"api url";

NSString* webStringURL = [urlAsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];

NSURLRequest *urlRequest =
[NSURLRequest
 requestWithURL:url
 cachePolicy:NSURLRequestReturnCacheDataElseLoad
 timeoutInterval:30.0f];

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection
 sendAsynchronousRequest:urlRequest
 queue:queue
 completionHandler:^(NSURLResponse *response,
                     NSData *data,
                     NSError *error) {
     if ([data length] >0 && error == nil){          

         NSMutableArray *arrayResponse=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

        [self prepareData:arrayResponse];



     }
     else if ([data length] == 0 && error == nil){
         dispatch_async(dispatch_get_main_queue(), ^{
             UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"The server is not responding \n please check your internet connection" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
             [alert show];
             alert=nil;
         });            
         NSLog(@"Nothing was downloaded.");
     }
     else if (error != nil){

         dispatch_async(dispatch_get_main_queue(), ^{
             UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"The server is not responding \n please check your internet connection" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
             [alert show];
             alert=nil;
         });
         NSLog(@"Error happened = %@", error);
     }
 }];

0 个答案:

没有答案