Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Loading a text file using URL

Loading a text file using URL
Thread Tools
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Mar 15, 2002, 07:54 PM
 
Can someone give me some pointers on loading a text file from a remote site using a URL?

The documentation on NSURL doesn't seem to be complete. All I want to do is retrieve a text file into a variable using a URL.

thanks,
kman
     
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Mar 15, 2002, 08:19 PM
 
Originally posted by kman42:
<STRONG>Can someone give me some pointers on loading a text file from a remote site using a URL?

The documentation on NSURL doesn't seem to be complete. All I want to do is retrieve a text file into a variable using a URL.

thanks,
kman</STRONG>
The terminal command curl should do fine:

curl http://www.site.com/file.txt
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Mar 16, 2002, 08:17 AM
 
I should have been more explicit. I want to do it within a cocoa program.

Thanks,
kman
     
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Mar 16, 2002, 10:22 AM
 
Originally posted by kman42:
<STRONG>I should have been more explicit. I want to do it within a cocoa program.

Thanks,
kman</STRONG>
Cocoa apps can call terminal commands... I can't tell you exactly how however.. sorry.
     
Forum Regular
Join Date: Oct 2001
Location: Sweden
Status: Offline
Reply With Quote
Mar 18, 2002, 04:31 AM
 
It's actually a trivial task in Cocoa:

NSString *myTextDocument = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://path.to.document"]];

NSURL is just a class that provides an object wrapper around an URL. It's up to other classes, like NSString, to make use of it. So I don't think that the docs for NSURL is missing anything essential about its use. Try to browse through the docs for some of the basic classes in Cocoa (NSString, NSData, NSArray,...). You'll learn a lot of the basic conventions on how the different classes relate to each other. Cocoa is a very consistent set of API, so if you learn something in one class, it's very likely that you can use very similiar semantics in other classes as well. As an example, to get the contents of an URL as a collection of bytes instead of a string, just use:

NSData *myDataObject = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://path.to.document"]];
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Mar 18, 2002, 01:39 PM
 
Originally posted by tobli:
<STRONG>It's actually a trivial task in Cocoa:

NSString *myTextDocument = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://path.to.document"]];

NSURL is just a class that provides an object wrapper around an URL. It's up to other classes, like NSString, to make use of it. So I don't think that the docs for NSURL is missing anything essential about its use. Try to browse through the docs for some of the basic classes in Cocoa (NSString, NSData, NSArray,...). You'll learn a lot of the basic conventions on how the different classes relate to each other. Cocoa is a very consistent set of API, so if you learn something in one class, it's very likely that you can use very similiar semantics in other classes as well. As an example, to get the contents of an URL as a collection of bytes instead of a string, just use:

NSData *myDataObject = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://path.to.document"]];</STRONG>

Awesome! Thanks. I never would have thought that NSString would have a method for loading in the contents of a URL. I actually found info on using NSData over at cocoadev and did it that way, but using NSString will save me a step!

kman
     
Forum Regular
Join Date: Oct 2001
Location: Sweden
Status: Offline
Reply With Quote
Mar 19, 2002, 09:39 AM
 
Originally posted by kman42:
<STRONG>


Awesome! Thanks. I never would have thought that NSString would have a method for loading in the contents of a URL. I actually found info on using NSData over at cocoadev and did it that way, but using NSString will save me a step!

kman</STRONG>
I should add that NURL are not just for internet urls. file:// urls can be used to load data/strings/whatever from local files as well. A nice, unified, way of accessing files.
     
   
Thread Tools
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -5. The time now is 09:52 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2