Я новичок в iPhone,
В настоящее время я разрабатываю приложение для iPhone и хотел бы реализовать возможность загрузки файла с URL-адреса. Я создал UIWebView
, когда я нажимаю ссылку download
в веб-просмотре, начинается загрузка, и я сохраняю этот файл в указанной папке в каталоге документов. но я не вижу загруженный файл.
Вот мой фрагмент кода,
//CAPTURE USER LINK-CLICK in UIwebView.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:DUrl]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
receivedData = [[NSMutableData data] retain];
} else {
NSLog(@"Inform the user that the connection failed.");
}
return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data1
{
[receivedData appendData:data1];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
DirPath=[self MyApplicationDocumentDirectory];
[receivedData writeToFile:DirPath atomically:YES];
UIAlertView* Alert = [[UIAlertView alloc] initWithTitle:@"Download Complete !"
message:nil delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[Alert show];
[Alert release];
// release the connection, and the data object
[connection release];
[receivedData release];
}
Любая помощь будет принята.
РЕДАКТИРОВАТЬ:
BOOL success =[[NSFileManager defaultManager] fileExistsAtPath:MyDirPath];
if (success)
{
UIAlertView* innerAlert = [[UIAlertView alloc] initWithTitle:@"Already downloaded."
message:@"Do you want to Downlaod again ?" delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Yes",@"No",nil];
[innerAlert show];
[innerAlert release];
}
где написать это условие?
if([receivedData writeToFile:DirPath atomically:YES]) { //alert success } else { //handle failure }
- person Killian   schedule 20.08.2012