Shoaib Sarwar Cheema Shoaib Sarwar Cheema - 7 months ago 21
iOS Question

Restful API call using IOS with authentication

I am working on an application that uses restful API call using prestashop API. I am new at IOS I coded the same method in android as:

InputStream is = null;
try {

DefaultHttpClient client = new DefaultHttpClient();

/* adding credentials as it is RESTful call */
String username = "xyz";
String password = "";
client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),new UsernamePasswordCredentials(username, password));
// HTTP get request
HttpGet get = new HttpGet("");
HttpResponse responseGet;
responseGet = client.execute(get);
is = responseGet.getEntity().getContent();
} catch (ClientProtocolException e) {
Log.e("HTTP Request","Client Protocol exception" );
} catch (IOException e) {
Log.e("HTTP Request","IO exception" );

It is working perfectly for Android. For IOS I used this coding but I am not getting data from the server.

NSString *userName = @"XYZ";
NSString *password = @"";
//setting the string of the url taking from appliance IP.

NSString *urlString = @"";

NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];

[request setURL:[NSURL URLWithString:urlString]];

[request setHTTPMethod:@"GET"];

NSString *str1 = [NSString stringWithFormat:@"%@:%@",userName,password];

NSLog(@" str1 %@", str1);

[request addValue:[NSString stringWithFormat:@"Basic %@",str1] forHTTPHeaderField:@"Authorization"];

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

NSString *str = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"str: %@", str);

please tell me what I am doing wrong and provide any solution.



You can build the URL string this way and it should work :-

NSString *str1 = [NSString stringWithFormat:@"",userName,password];

No need to use the HTTP header fields I believe