Saving song/movie file to iphone from Server (with didReceiveData?)

Discussion in 'iOS Programming' started by PizzaTray, Jan 19, 2010.

  1. PizzaTray macrumors newbie

    Joined:
    Oct 31, 2009
    #1
    Hi everyone,

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

    Here is my code:
    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?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Except connection:didReceiveData: usually gets called more than once. It is an incremental thing.
     
  3. PizzaTray thread starter macrumors newbie

    Joined:
    Oct 31, 2009
    #3
    I didn't get it. Is it possible download&save the files? how to?
    any code help or something?
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Yes, it's possible. Here's some pseudo-code:
    1. Retrieve file from server into an NSData object.
    2. Write NSData object to a file.

    The problem with your above code is you're writing the data too early in the process.
     
  5. PizzaTray thread starter macrumors newbie

    Joined:
    Oct 31, 2009
    #5
    ok so, when i'm suppose to write the data in the code?
    and how i'm writing a music file (like mp3 or somthing) to a file?

    there is an option to insert the song file after that to the music library in the iphone? and how i'm purchasing it?
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    After it's finished loading.

    Same as any other file. As long as it's in an NSData object you should be able to write it to a file.

    No, I don't believe there is.

    Purchasing it? I'm not sure what you're talking about.
     
  7. PizzaTray thread starter macrumors newbie

    Joined:
    Oct 31, 2009
    #7
    I mean, to get paid by in app purchsing it in the application.
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    Apple will not put any app on the store that competes with the iTunes Music Store. I'm pretty sure it's covered in the SDK agreement you read, agreed to and understood.
     
  9. PizzaTray thread starter macrumors newbie

    Joined:
    Oct 31, 2009
    #9
    ok,
    but if i want that the user will download from my app ringtons (in payment) to its iphone. is it possible? legal?
     
  10. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    I'm 99% sure that will be rejected.
     
  11. PizzaTray thread starter macrumors newbie

    Joined:
    Oct 31, 2009
    #12
    So, there is absolutly no way to sale music or ringtons ,in any some strange way, from application?? :(
     
  12. drf1229 macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #13
    Not unless you sell it on the jailbroken store. If you owned a billion dollar company that sold music, you'd stop any possible threat when you got the chance, wouldn't you?
     

Share This Page