Reputation: 3878
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 the webview and then download them to a specified folder in the documents directory.
Any advice/links to tutorials etc would be much appreciated.
Cheers
Upvotes: 2
Views: 1765
Reputation: 6949
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.
Upvotes: 3
Reputation: 19999
To capture links in a UIWebView, implement the webView:shouldStartLoadWithRequest:navigationType:
delegate method, and return NO
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.
Upvotes: 1
Reputation: 3878
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;
}
Will post my final code once I'm done.
Upvotes: 2