1

インターネットがある場合はネットワークからのデータを表示する必要がありますが、ネットワークがない場合はキャッシュされたデータを表示する必要がありました。サーバーからデータを取得するためにnsurl接続を使用しています。私のコードは次のとおりです

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:strUrl]
                                         cachePolicy:NSURLRequestUseProtocolCachePolicy
                                     timeoutInterval:60.0];

NSURLConnection *conn = [NSURLConnection connectionWithRequest:request
                                                      delegate:self];
[conn start];

また、呼び出されていないデリゲートも

-(NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse: 
(NSCachedURLResponse *)cachedResponse
 {
return cachedResponse;
 } 

iOSのNSURLConnectionのキャッシュに保存されたデータを取得する方法を教えてください..よろしくお願いします.:)

4

2 に答える 2

1

遅れて申し訳ありませんが、これについていくつかの調査を行う必要があります:)これが、オフラインでも取得できる画像をダウンロードすることでUIが試行されるのに役立つことを願っています. これは助けになるかもしれません:)

アプリデリゲートで作成キャッシュを設定します

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
  // Override point for customization after application launch.
  self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
  //need to add this
  NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
                                                     diskCapacity:20 * 1024 * 1024
                                                         diskPath:nil];
  [NSURLCache setSharedURLCache:URLCache];
  // set sharedcache

  self.window.rootViewController = self.viewController;
  [self.window makeKeyAndVisible];
  return YES;
}  

//ダウンロード中のView Controllerで

    #import "ViewController.h"
    @interface ViewController ()<NSURLConnectionDelegate> // confirms to this delegate
     {
         NSMutableData *imagData;
     }

     @end

     @implementation ViewController

     - (void)viewDidLoad
      {
         [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

         NSURL *url = [NSURL           URLWithString:@"http://static.adzerk.net/Advertisers/d9a919813dac42adbd0e3106bc19bc04.png"];
         NSURLRequest *Req = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15];

        NSURLConnection *conn = [NSURLConnection connectionWithRequest:Req delegate:self];
        if(conn == nil)
        {
           conn = nil;
           [conn cancel];
        }

   }


   - (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
    {

       if(imagData == nil)
       {
          imagData = [[NSMutableData alloc]init];
       }

     //    NSURLResponse *resp = [[NSURLResponse alloc]initWithURL:[NSURL        URLWithString:@"http://static.adzerk.net/Advertisers/d9a919813dac42adbd0e3106bc19bc04.png"]    MIMEType:@"" expectedContentLength:50 textEncodingName:@"image"];
    //    NSLog(@"%@",connection.description);
    //    NSLog(@"%@",resp.description);
    //    
     //    NSCachedURLResponse *chacheResp = [[NSCachedURLResponse alloc]initWithResponse:resp data:data];
    //    

     [imagData  appendData:data];
      UIImage *aimage=[[UIImage alloc]initWithData:imagData];
      self.aview.image = aimage;
    } 


  -(NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
   {
       NSMutableDictionary *userInfo = [[cachedResponse userInfo] mutableCopy];
       NSMutableData *mutData = [[cachedResponse data] mutableCopy];
       NSURLCacheStoragePolicy storagePolicy = NSURLCacheStorageAllowedInMemoryOnly;


       return [[NSCachedURLResponse alloc] initWithResponse:[cachedResponse response]
                                                data:mutData
                                            userInfo:userInfo
                                       storagePolicy:storagePolicy];

   }

   - (void)connectionDidFinishLoading:(NSURLConnection *)connection
   {
       connection = nil;
       [connection cancel];
   }

また、AdamG が提供するリンクも参照してください

于 2013-10-01T05:57:26.597 に答える