2

I'm downloading a webpage using NSMutableURLRequest but having problem putting that very webpage into a NSString.

NSString *username = @"my_username";

NSString *password = @"my_password";

NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.mypage.com/login.php?username=%@&password=%@", username, password]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSString *returnPage = [NSString stringWithFormat:@"%.*s", [returnData length], [returnData bytes]];

This works fine except for special chars like åäö etc. Is there any better way?

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
user872661
  • 251
  • 2
  • 13

1 Answers1

5

Yes, use the following:

NSString *returnPage = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];

That uses UTF8 instead of ASCII.

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • Hmm, Xcode (4.2) tells me "ARC forbids explicit message of 'autorelease'", should I just leave to autorelease out and cross my fingers? :) I'm not so used to ARC, ObjC nor Xcode... – user872661 Dec 09 '11 at 20:03
  • Tried NSString *returnPage = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; didn't work. returnPage is after execution =/ – user872661 Dec 09 '11 at 20:11
  • That means you are using the wrong encoding... what encoding is returned from your api? – Richard J. Ross III Dec 10 '11 at 15:34
  • Well, the api is not mine it is just a plain webpage. How do I see which encoding it is using? – user872661 Dec 14 '11 at 22:08