Download Files within iPhone app
I am currently developing an iPhone app and would like to implement the ability to download files (specifically pdf, mp3, doc and ppt files) from the internet. I have created the UIWebView, but want to know the best way of capturing the files when they are linked to in t开发者_高级运维he webview and then download them to a specified folder in the documents directory.
Any advice/links to tutorials etc would be much appreciated.
Cheers
Edited
UIWebView can not be used to download pdf, mp3, doc, ppt, etc... It is used to display only webpage.
You need to use Quartz 2D to draw pdf document. You need Media Player framework to to play songs, audio books.
To download a file from the server, you can try to use [NSData initWithContentsOfURL] method.
An example:
NSString *strImagePathURL = [NSString stringWithFormat:@"http://foo.com/%d.png", item];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:strImagePathURL]];
You can later decide what to do with NSData, you can save it to file on the Documents folder using the [NSData writeToFile] method.
Have found the method for capturing links. It's a delegate method, so make sure that all of the hooks are in place:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//CAPTURE USER LINK-CLICK.
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
NSLog(@"url is: %s ", URL);
}
return YES;
}
From here
Will post my final code once I'm done.
To capture links in a UIWebView, implement the
webView:shouldStartLoadWithRequest:navigationType:
delegate method, and returnNO
when you see a link you want to handle.For example code on how to download using HTTP on the iPhone, see the SimpleURLConnections sample project.
精彩评论