 |
 |
Loading a text file using URL
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status:
Offline
|
|
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
|
|
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
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status:
Offline
|
|
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
|
|
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
|
|
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"]];
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status:
Offline
|
|
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
|
|
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.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|