I am working on an application that uses a calm API call using the preashop API. I am new to iOS. I encoded the same method in android, like:
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("http://www.example.com/api/"); 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 works great for Android. For IOS, I used this encoding, but I do not receive data from the server.
NSString *userName = @"XYZ"; NSString *password = @""; //setting the string of the url taking from appliance IP. NSString *urlString = @"http://www.example.com/api/"; 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.
Thanks!
source share