Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-21-2010, 03:23 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 42
Jackson13 is on a distinguished road
Exclamation Download Text File

I have seen a lot of threads on downloading images to the apps directory. What i want to know is how to download a text file and then find it. Is this possible?
Jackson13 is offline   Reply With Quote
Old 06-21-2010, 03:26 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

Yes.

You have many options to download the file.

Synhronously - you can use NSData's initWithContentsOfUrl, then use NSFileManager's createFileAtPath method to write to disk.

Asynchronously - you can use NSURLConnection to download the data, then use the NSFileManager the same way as above to save to disk.
smithdale87 is offline   Reply With Quote
Old 06-21-2010, 03:27 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 42
Jackson13 is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
Yes.

You have many options to download the file.

Synhronously - you can use NSData's initWithContentsOfUrl, then use NSFileManager's createFileAtPath method to write to disk.

Asynchronously - you can use NSURLConnection to download the data, then use the NSFileManager the same way as above to save to disk.
I am very new to this. Do you have any example code. That would be very appreciated.
Jackson13 is offline   Reply With Quote
Old 06-21-2010, 03:29 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

I have Google. Let me google that for you

Also - NSFileManager Class Reference
smithdale87 is offline   Reply With Quote
Old 06-21-2010, 03:31 PM   #5 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 42
Jackson13 is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
Thank you.
Jackson13 is offline   Reply With Quote
Old 06-21-2010, 03:58 PM   #6 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 42
Jackson13 is on a distinguished road
Default

I am sorry but I still cannot get it. I looked at your pretty cool google search and only found definitions. I really just want some sample code. if you have worked with this before that would be great. I am just so new at this. I read everything on google from apple and other answers. But still have not found my answer. THank you for the quick responses.
Jackson13 is offline   Reply With Quote
Old 06-21-2010, 05:12 PM   #7 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 159
bluemonkey is on a distinguished road
Default

I do something similar but grab a text file and store it in a string for processing:


Code:
NSURL *urlVersion = [NSURL URLWithString: @"http://www.mydomain.com/folder1/version.txt"];
		NSString *webVersion = [NSString stringWithContentsOfURL:urlVersion encoding:1 error:NULL];
		NSLog(@"Web Version = %@", webVersion);
__________________
Aaron
bluemonkey is offline   Reply With Quote
Old 06-21-2010, 06:08 PM   #8 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 42
Jackson13 is on a distinguished road
Default

Is there a way to store it for later use so you do not have to continuosly download it.
Jackson13 is offline   Reply With Quote
Old 06-22-2010, 10:35 AM   #9 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

Yes. Look at my 2ndpost. Use NSFileManager to save the txt file to your application documents directory.

Then always, before downloading the .txt file, check if the file exists in your documents directory first. If it does, then you dont need to downlaod the text file. Otherwise, you do.
smithdale87 is offline   Reply With Quote
Old 06-22-2010, 11:23 AM   #10 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 42
Jackson13 is on a distinguished road
Default

I've tried quite a few times but that never works. Do have a code snippet that I can compare my code to. If not when I get home I'll post the code that I tried using.
Jackson13 is offline   Reply With Quote
Old 06-22-2010, 11:30 AM   #11 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

When you get home, just post your code. I think it's better to show you what you're doing wrong in your own code than just give you the right code that works.
smithdale87 is offline   Reply With Quote
Old 06-22-2010, 11:51 AM   #12 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 42
Jackson13 is on a distinguished road
Default Here it is. I don't know if this is correct. I got it from somebody.

Code:
NSError *error = nil;
NSString *str;
	
// Search if the file exists already in the documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePath = [documentsDirectory stringByAppendingPathComponent:@"myFile.txt"];
	
// If file doesn't exists in the docDirectory or it's empty then I take it from the MainBundle
if (filePath) {
	fileContent = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
	if (!fileContent) {
		filePath = [[NSBundle mainBundle] pathForResource:@"myFile" ofType:@"txt"];
		}
}else {
	filePath = [[NSBundle mainBundle] pathForResource:@"myFile" ofType:@"txt"];
}
	
// Once the file is taken (no matter if it comes from the docDirectory or the MainBundle) it's time to start working with it:
if (filePath) {
	fileContent = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
	if (fileContent) {

** * * * * * * */* // You can use this if you want to read from the file!
		NSArray* arr = [fileContent componentsSeparatedByString:@"\n"];
		NSEnumerator* nse = [arr objectEnumerator];
			
		str = [nse nextObject];
** * * * * * * **/
			
** * * * * * * *// Prepare the path to write to the file...
		filePath = [documentsDirectory stringByAppendingString:@"/"];	
		filePath = [filePath stringByAppendingString:@"myFile.txt"];	
			
** * * * * * * *NSString *string = @"Hello, world!";

		// Write string to file
		if ([string writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error: nil]) {
			//NSLog(@"File writed correctly!");
		}else {
			//NSLog(@"ERROR: Unable to write to file!");
		}
	}
}
Jackson13 is offline   Reply With Quote
Old 06-22-2010, 11:55 AM   #13 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

Here's your problem:
Quote:
Here it is. I don't know if this is correct. I got it from somebody.
If you ever have any hopes of producing any kind of applications, dont just bolt together snippets of code from all over the place. Or at least if you do, you should know what the code actually does, and be able to tell if it is working correctly or not.
smithdale87 is offline   Reply With Quote
Old 06-22-2010, 11:56 AM   #14 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

I see exactly why it doesnt work though.
smithdale87 is offline   Reply With Quote
Old 06-22-2010, 12:14 PM   #15 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 42
Jackson13 is on a distinguished road
Default

I read the entire forum which gave me a great idea of what is happening. The problem is that there are no tutorials on this so I can't really learn anything quickly. Do you know anything that would help.
Jackson13 is offline   Reply With Quote
Old 06-22-2010, 02:28 PM   #16 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 42
Jackson13 is on a distinguished road
Default

You said you know why it doesn't work. Why doesn't it work?
Jackson13 is offline   Reply With Quote
Old 06-23-2010, 09:11 AM   #17 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 42
Jackson13 is on a distinguished road
Default

Thanks guys for all your help but I figured out how to do it from an online tutorial.
Jackson13 is offline   Reply With Quote
Old 06-23-2010, 09:12 AM   #18 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

Would you mind posting the solution for anyone else that may have this same question in the future? or perhaps just a link to the tutorial you used?
smithdale87 is offline   Reply With Quote
Old 06-29-2010, 04:06 PM   #19 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 42
Jackson13 is on a distinguished road
Default

First I use the code that blue monkey gave mr and I saved the contents of the URL to a string. Then I wrote to a text file in the documents directory from this tutorial. The link to the video is below.

YouTube - XCode Tutorial: Documents Directory
Jackson13 is offline   Reply With Quote
Old 07-08-2010, 08:25 AM   #20 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 42
Jackson13 is on a distinguished road
Default

I have one last question. When I implement this the UIFreezes and I am unable to show that the application is doing something. Is it possible to allow it to do it in the background or at least show a UIActivityIndicatorView animating?
I used the initWithContentsOfURL:blah and then saved it to the documents directory using the tutorial posted above.
Jackson13 is offline   Reply With Quote
Old 07-08-2010, 08:53 AM   #21 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 126
javaconvert is on a distinguished road
Default

Quote:
Originally Posted by Jackson13 View Post
I have one last question. When I implement this the UIFreezes and I am unable to show that the application is doing something. Is it possible to allow it to do it in the background or at least show a UIActivityIndicatorView animating?
I used the initWithContentsOfURL:blah and then saved it to the documents directory using the tutorial posted above.
I'd look into NSURLConnection.
javaconvert is offline   Reply With Quote
Old 07-08-2010, 09:24 AM   #22 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

Other than NSURLConnectioon, you can just use the performSelectorInBackground method and call your initWithContents of URL there. You can add an activity indicator as well
smithdale87 is offline   Reply With Quote
Reply

Bookmarks

Tags
download, file, nsurl, text, url

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 333
8 members and 325 guests
anothermine, Chickenrig, Domele, givensur, heshiming, michaelhansen, PixelInteractive, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:18 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0