Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

PizzaTray

macrumors newbie
Original poster
Oct 31, 2009
25
0
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?
 
Except connection:didReceiveData: usually gets called more than once. It is an incremental thing.

I didn't get it. Is it possible download&save the files? how to?
any code help or something?
 
I didn't get it. Is it possible download&save the files? how to?
any code help or something?
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.
 
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.

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?
 
ok so, when i'm suppose to write the data in the code?
After it's finished loading.

and how i'm writing a music file (like mp3 or somthing) to a file?
Same as any other file. As long as it's in an NSData object you should be able to write it to a file.

there is an option to insert the song file after that to the music library in the iphone?
No, I don't believe there is.

and how i'm purchasing it?
Purchasing it? I'm not sure what you're talking about.
 
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.

ok,
but if i want that the user will download from my app ringtons (in payment) to its iphone. is it possible? legal?
 
So, there is absolutly no way to sale music or ringtons ,in any some strange way, from application?? :(

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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.