NSURLConnection について質問があります: イメージをダウンロードしたい:
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
(正しく)呼び出された最初のデリゲートは
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
これが一度呼び出された後:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
そしてすぐに:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
ここまではすべて正しいですが、connectionDidFinishLoading が同じ接続の didReceiveData デリゲートをもう一度起動した後、次の方法で接続を識別します。
NSString * connectionKey = [[[connection originalRequest] URL] absoluteString];
これは可能ですか?
更新、詳細情報:
私のアプリは多くの同時接続を開始し、デリゲートが呼び出されたときに、すべての接続の情報を辞書に保存します。次のキーを使用して接続情報を取得します。接続の 99% ではすべて問題ありませんが、1 つの接続 (毎回同じです!) ではこの動作をします
ここに完全な実装があります:
- (void)downloadFileFromUrl:(NSURL *)url
inPath:(NSString *)completeFilePath
dataReceivedBlock:(void (^)(long long byteDownloaded ,long long totalByte))dataReceivedBlock
endBlock:(void (^)(NSString * downloadPath, NSDictionary * responseHeaders))endBlock
failBlock:(void (^)(NSString * downloadPath, NSDictionary * responseHeaders, NSError * error))failBlock
{
//save the connection infos
NSMutableDictionary * requestData = [[NSMutableDictionary alloc] init];
if(dataReceivedBlock)
[requestData setObject:[dataReceivedBlock copy] forKey:@"dataReceivedBlock"];
if(endBlock)
[requestData setObject:[endBlock copy] forKey:@"endBlock"];
if(failBlock)
[requestData setObject:[failBlock copy] forKey:@"failBlock"];
[requestData setObject:[NSNumber numberWithBool:YES] forKey:@"usingBlock"];
[requestData setObject: completeFilePath forKey:@"downloadDestinationPath"];
//delete the file if already on fs
if([[NSFileManager defaultManager] fileExistsAtPath: completeFilePath])
[[NSFileManager defaultManager] removeItemAtPath:completeFilePath error:nil];
// Create the request.
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:TIME_OUT];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
if (!theConnection)
{
// Inform the user that the connection failed.
failBlock(completeFilePath,nil,[NSError errorWithDomain:@"Connection fail" code:0 userInfo:nil]);
}
//add connection infos to the requests dictionary
NSString * connectionKey = [[[theConnection originalRequest] URL] absoluteString];
[self.requests setObject:requestData forKey:connectionKey];
}
ここにデリゲートの例:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString * connectionKey = [[[connection originalRequest] URL] absoluteString];
NSMutableDictionary * requestData = [self.requests objectForKey:connectionKey];
NSFileHandle *file = [requestData objectForKey:@"fileHandle"];
[file closeFile];
//se la richiesta usa o no block
BOOL usingBlock = [[requestData objectForKey:@"usingBlock"] boolValue];
if(usingBlock)
{
__block void (^endBlock)(NSString * , NSDictionary *) = [requestData objectForKey:@"endBlock"];
if(endBlock)
endBlock([requestData objectForKey:@"downloadDestinationPath"],[requestData objectForKey:@"responseHeaders"]);
}
else
[self.delegate downloadEnded:[requestData objectForKey:@"responseHeaders"]];
//elimino dati richiesta
[self.requests removeObjectForKey:[NSString stringWithFormat:@"%@",connectionKey]];
//Inibisco standby
[UIApplication sharedApplication].idleTimerDisabled = NO;
}