iOS アプリで PHP サービスからファイルをダウンロードしようとしています。Android とブラウザから同じ PHP サービスを使用していますが、サーバー部分は正常に動作します。ブラウザで URL をテストすると、期待どおりにファイルをダウンロードするように求められます。また、iOS コードからファイルに直接 URL を指定すると、ファイルを正常にダウンロードできます。これらはzipファイルです。ID でファイルを取得する必要があり、アクセス トークン文字列を渡す必要があるため、このメソッドを使用する必要があります。これらは OK を検証するので、問題はありません。
接続すると、httpResponse ステータス コード 200 の didReceiveResponse が返されますが、これは問題ないと思います。次に、connectionDidFinishLoading デリゲート呼び出しを取得しますが、その間にデータやエラーを取得することはありません。
ここに私の接続コードがあります:
- (BOOL)downloadFile:(NSString *)fileName:(int)fileId
{
BOOL retVal = FALSE;
NSString *strUrl = [NSString stringWithFormat:@"%@?id=%d&token=%@", FILE_URL, fileId, accessToken];
NSURL *myURL = [NSURL URLWithString:strUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
// Starts the download
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
return TRUE;
}
// 応答コードは次のとおりです。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
int code = [httpResponse statusCode];
if(code <= 200)
{
[responseData setLength:0];
}
else
{
NSString *msg = [NSString stringWithFormat:@"Error Code: %d. Download has Failed.", code];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Download Error!"
message:msg
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
// これは孤独な受信データ メソッドです。呼び出されることはありません
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
// 失敗したデリゲート メソッドも呼び出されません
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSString *msg = [NSString stringWithFormat:@"Error: %@", [error localizedDescription]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Download Error!"
message:msg
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
私が言ったように、それは Android とブラウザから動作し、ファイルの URL をポイントするだけで正常にダウンロードされます。
私は立ち往生しており、私が得ることができる支援に感謝しています。