views:

137

answers:

0

Hi everyone,

I'm trying to download a song/movie file from server that host it, and after its download to the app --> save the file to the iphone.

Here is my code:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
[self beginDownloading];

[window addSubview:viewController.view];
[window makeKeyAndVisible];
}

- (void) beginDownloading {
    NSString *string = @"https://....."; 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:string]];
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

    if(connection) {
        receivedData = [[NSMutableData data]retain];
    }
    else {
        NSLog(@"connection Faild");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data];

    /*NSFileHandle *handle = [NSFileHandle      fileHandleForWritingAtPath:@"...."];
    [handle seekToEndOfFile];
    [handle writeData:data];
    [handle closeFile];
    [str release];*/
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [connection release];
    [receivedData release];
    NSLog(@"connection Faild");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];
    //printf((const char*)[receivedData bytes]);
    NSLog(@"connection Finished");
}

What to add? if i want to get the song/movie from the URL and then save it to the iphone.

And, is it possible to save it to the music playlist / movie library in the iphone? and how to purchasing it?

Thanks.