user3189586 user3189586 - 5 months ago 25
Objective-C Question

Passing Username and Password to Web Service

I want to call the following web service, but would like to know how to pass a Username and Password and get a response.

Answer
@interface ViewController ()
{ 
NSURLConnection *clearSession;
NSMutableData *receivedData;
}


- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[self clearsession];
}



-(void)clearsession

{



NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"https://mybusiness.expressoft.eu/webserviceintegration/v1/myshop/MyShopDataService.svc/"]];




 NSString *requestString = [NSString stringWithFormat:@"Username=karthik&Password=abcdesg",nil];


NSLog(@" RequestString for get card details: %@",requestString);

NSMutableData *requestData =[NSMutableData dataWithBytes:[requestString UTF8String] length: [requestString length]];

[request setHTTPMethod: @"POST"];

[request setHTTPBody: requestData];
clearSession = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if (clearSession) {
    NSLog(@"data sent ");
} else
{
    NSLog(@"Not sent");
}

[clearSession start];


}





-(void)connection:(NSConnection*)conn didReceiveResponse:(NSURLResponse *)response
{
if (receivedData == NULL)
{
    receivedData = [[NSMutableData alloc] init];
}
[receivedData setLength:0];



}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

[receivedData appendData:data];



 }

 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
 {
  NSLog(@"Connection failed! Error - %@ %@",
      [error localizedDescription],
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);

UIAlertView *customAlert = [[UIAlertView alloc]initWithTitle:@"No NetWork" message:@"Interet Connection is Lost" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[customAlert show];


}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

if (connection==clearSession)
{

    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:receivedData options: kNilOptions error:nil];
    NSString *tmp=[[NSString alloc]initWithData:receivedData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", tmp);
    NSLog(@"  parsing JSON of add checkout: %@", jsonDict);


}

}