stringWithContentsOfURL: usedEncoding: error:
Returns a string created by reading data from a given URL and returns by reference the encoding used to interpret the data.
+ (id)stringWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError**)error
Parameters
- url
- The URL from which to read data.
- enc
- Upon return, if url is read successfully, contains the encoding used to interpret the data.
- error
- If an error occurs, upon returns contains an
NSErrorobject that describes the problem. If you are not interested in possible errors, you may pass inNULL.
Return Value[ NSString stringWithContentsOfURL usedEncoding error example ]
A string created by reading data from url. If the URL can’t be opened or there is an encoding error, returns
nil.Discussion
This method attempts to determine the encoding at url.
[ NSString stringWithContentsOfURL usedEncoding error example ]
NSStringEncoding encoding;
NSError *error;
NSString *string = [NSString stringWithContentsOfURL:
[NSURL URLWithString:@"http://www.google.com/"]
usedEncoding:&encoding
error:&error];
NSLog(@"html = %@",string);
[ NSString stringWithContentsOfURL usedEncoding error example ]
NSStringEncoding encoding;
NSString *pageData = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:NULL];
NSLog(@"Encoding %d",encoding);(Note: NSUTF8StringEncoding = 4)
[ NSString stringWithContentsOfURL usedEncoding error example ]
...
CFErrorRef error = NULL;
CFURLRef fileURL = CFBundleCopyResourceURL(bundle,
CFSTR("file"),
CFSTR("txt"),
NULL);
// Ugly piece of objc code in my whole C source file :(
NSError *nsError = (__bridge NSError *)(error);
NSString *nsString = [NSString stringWithContentsOfURL:(__bridge NSURL *)fileURL
usedEncoding:NULL
error:&nsError];
CFStringRef fileContents = (__brigde CFStringRef)nsString;
...