Wednesday, May 1, 2013

NSURLResponse textEncodingName example ios


textEncodingName

Returns the name of the receiver’s text encoding provided by the response’s originating source.
- (NSString *)textEncodingName
Return Value of [NSURLResponse textEncodingName]
The name of the receiver’s text encoding provided by the response’s originating source, ornil if no text encoding was provided by the protocol
Discussion of [NSURLResponse textEncodingName]
Clients can convert this string to an NSStringEncoding or a CFStringEncoding using the methods and functions available in the appropriate framework.
Example of [NSURLResponse textEncodingName]
    // Encode:

 [archiver encodeObject:[response URL] forKey:@"URL"];
 [archiver encodeObject:[response MIMEType] forKey:@"MIMEType"];
 [archiver encodeObject:[NSNumber numberWithLongLong:[response expectedContentLength]] forKey:@"expectedContentLength"];
 [archiver encodeObject:[response textEncodingName] forKey:@"textEncodingName"];

    // Decode:

 NSURL *url = [unarchiver decodeObjectForKey:@"URL"];
 NSString *mimeType = [unarchiver decodeObjectForIKey:@"MIMEType"];
 NSNumber *expectedContentLength = [unarchiver decodeObjectForKey:@"expectedContentLength"];
 NSString *textEncodingName = [unarchiver decodeObjectForKey:@"textEncodingName"];

 NSURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:url MIMEType:mimeType expectedContentLength:[expectedContentLength longLongValue] textEncodingName:textEncodingName];
Example of [NSURLResponse textEncodingName]
CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef) [response textEncodingName]);
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);

NSString* string = [[NSString alloc] initWithData:data encoding:encoding];

// Do stuff here..

[string release];