I have a list of mp3 files on server.
Now I want that when the user clicks the 开发者_如何学运维button the selected mp3 file should get downloaded into his/her iPhone.
How can it be done?
Any references to articles or tutorials related to this would be really appreciated.
One of the Techniques is using:
NSMutableURLRequest
You can initiailize a URL Request as the following:
NSURL *URL = [NSURL URLWithString:@"your server url"];
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:15.0];
NSURLConnection *URLConneSync = [NSURLConnection connectionWithRequest:request delegate:self];
And the delegates of the NSURLConnection:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
Update
On RecieveData Delegate you can do as follows:
NSFileManager *fileManager;
[fileManager createFileAtPath:@"Your Path" contents:nil attributes:nil];
NSFileHandle *FileHandle = [NSFileHandle fileHandleForWritingAtPath:@"Your Path Again"];
Note: you can get the iphone/iPad SandBox Path by using:
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *sDocumentsDir = [documentPaths objectAtIndex:0];
check the Simple URL Connections code sample... it explains how to get, put and post data to server....
精彩评论