0

I am trying to login to a Jira instance from an ios app and i have encountered some problems with the basic 64 encode authorization header with usernames that have chars as 'ñ', 'ó' etc.

I solved this problem using NSISOLatin1StringEncoding when getting the data out of the string. But this does not work with other users, such as russian users, etc.

So, how can i get a correct base 64 encoded string from user:password regardless the language the username is?

This is the code i'm using

-(NSString *) baseEncode{

NSString *username = @"привет";
NSString *password = @"123привет";

NSString *basic = [NSString stringWithFormat:@"%@:%@", username, password];
NSString *string = @"";

//this way works, but not with russian users
NSData *data = [basic dataUsingEncoding:NSISOLatin1StringEncoding];

//if i use this line instead it won't work with latin users with 'ñ', 'ó', etc on their username
//NSData *data = [basic dataUsingEncoding:NSUTF8StringEncoding];


if (data != nil) {
    if ([data respondsToSelector:@selector(base64EncodedStringWithOptions:)]) {
        string = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];  // iOS 7+
    } else {
        if ([data respondsToSelector:@selector(base64Encoding)]) {
            string = [data performSelector:@selector(base64Encoding)];  // pre iOS7
        }
    }
}

NSLog(@"encode result : %@", string);
return string;

}

-(void) doRequest{

NSURL *url = [NSURL URLWithString:[@"http://url_to_jira_rest_api" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSString *login = [self baseEncode];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

[request setValue:[NSString stringWithFormat:@"Basic %@", login] forHTTPHeaderField:@"Authorization"];

[request setHTTPMethod:@"GET"];

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[queue setSuspended:NO];

[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData * data, NSError *error) {
    NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
    long statusCode = 0;

    statusCode = [httpResponse statusCode];

    NSString* newStr = [[NSString alloc] initWithData:data
                                             encoding:NSUTF8StringEncoding];

    NSLog(@"%@",newStr);

    NSLog(@"%@",[error description]);

}];
}

this is the server response

Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)"
Lucho
  • 1
  • 2
  • See http://stackoverflow.com/a/9056877/413337. Have fun with the implementation. – Codo May 05 '14 at 15:59
  • You need find out what encoding the Jira website expects. UTF-8 is the best option to support all characters but the website needs to expect UTF-8. It it expects a different encoding, such as ISO Latin-1, then you can only support characters supported by the encoding. – rmaddy May 05 '14 at 16:02
  • @rmaddy i already encoded on latin and utf8 and it worked fine. I think that is not a server problem. I will find out and post it here. – Lucho May 05 '14 at 16:47

0 Answers0