1
我下載一個文件,NSURLConnection的方法,但是當我嘗試將數據保存和存檔損壞保存文件已損壞
這是我的代碼任何幫助表示讚賞
NSNumber *filesize;
NSMutableData *data2;
- (void)connection: (NSURLConnection*) connection didReceiveResponse: (NSHTTPURLResponse*) response
{
[data2 setLength:0];
if ([response expectedContentLength] == NSURLResponseUnknownLength) {
// unknown content size
filesize = @0;
}
else {
filesize = [NSNumber numberWithLongLong:[response expectedContentLength]];
NSLog(@"%@",filesize);
}
}
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)recievedData {
[data2 appendData:recievedData];
if (data2==nil) {
data2 = [[NSMutableData alloc] initWithCapacity:[filesize floatValue]];
}
NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[data2 length]]; //MAGIC
float progress = [resourceLength floatValue]/[filesize floatValue];
progressBar.progress = progress;
NSLog(@"%f",progress);
}
此方法創建文件。但是,當我嘗試打開IPHONE模擬器文件夾中的存檔文件損壞 /用戶/ astramex19 /庫/應用程序支持/ iPhone模擬器/ 6.1 /應用/ 5D2E5AEB-6C27-43A9-9335-207A20A10F13 /文檔/ db.sqlite
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/db.sqlite"];
// BOOL filecreationSuccess = [fileManager createFileAtPath:fileName contents:data2 attributes:nil];
BOOL filecreationSuccess = [[NSFileManager defaultManager] createFileAtPath:fileName contents:data2 attributes:nil];
if(filecreationSuccess == NO){
NSLog(@"Failed to create the file");
}
}
是的,這是我的錯誤在我的應用程序 您的代碼工作得很好。非常感謝!!! = d – Fabio