From 4a93904566f6bcded910773bd13590b7be26d275 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Thu, 13 Oct 2011 12:57:39 -0500 Subject: [PATCH] Set text encoding for -responseString to UTF-8 by default, in case an encoding is not set in the response --- AFNetworking/AFURLConnectionOperation.h | 2 +- AFNetworking/AFURLConnectionOperation.m | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/AFNetworking/AFURLConnectionOperation.h b/AFNetworking/AFURLConnectionOperation.h index 210ac43..1491f20 100644 --- a/AFNetworking/AFURLConnectionOperation.h +++ b/AFNetworking/AFURLConnectionOperation.h @@ -128,7 +128,7 @@ extern NSString * const AFNetworkingOperationDidFinishNotification; /** The string representation of the response data. - @discussion This method uses the string encoding of the response to construct a string from the response data. + @discussion This method uses the string encoding of the response, or if UTF-8 if not specified, to construct a string from the response data. */ @property (readonly, nonatomic, copy) NSString *responseString; diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index 97bf745..3869bb9 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -233,7 +233,11 @@ static inline NSString * AFKeyPathFromOperationState(AFOperationState state) { - (NSString *)responseString { if (!_responseString && self.response && self.responseData) { - NSStringEncoding textEncoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef)self.response.textEncodingName)); + NSStringEncoding textEncoding = NSUTF8StringEncoding; + if (self.response.textEncodingName) { + textEncoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef)self.response.textEncodingName)); + } + self.responseString = [[[NSString alloc] initWithData:self.responseData encoding:textEncoding] autorelease]; }