1

I have an .aspx file with some webmethods inside. I want to consume those methods from inside an iOS (iPhone) Application. The webmethod code is as follows:

[WebMethod]
public static string Login(string username, string password) 
{
    return username + ":" + password;
}

To test this webmethod I used:

$.ajax({
    type: "POST",
    url: "DataProvider.aspx/Login",
    data: JSON.stringify({ username: "myUserName", password: "myPassword" }),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result) { alert(result.d); },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    }
});

So everything checks out and I get "myUserName:myPassword" back, now I want to do the same from inside xcode 4.5.2, so I created a new iPhone app and place a label (lbResult) and a button (btDoLogin) inside the ViewController and assigned the outlet and action.

Note that I'm not interested in async or delegates, I just want the data back and be able to parse it (JSON).

Sorry for being so specific about the details, but I saw a lot of similar questions and none of the answers worked for me. For what I read it's my understanding that this can be solved using NSURLConnection. For example, this question Passing parameters to a JSON web service in Objective C is very similar to what I need, but what I get back is the entire page! (meaning it's downloading the entire page instead of calling the web method) Also, I need to pass 2 parameters and the question uses only 1 (and I don't understand how to separate them in the concatenated string).

Now, what exactly do I need to out inside the IBAction to connect to this particular webmethod and get the return value?

- (IBAction)btDoLogin:(id)sender {
    // magic code goes here!
}

Thanks.-

Community
  • 1
  • 1
Nick
  • 727
  • 10
  • 20

1 Answers1

4

It turns out that the problem was how to pass the parameters in a JSON format, not as a normal concatenated string. Here's the complete code:

- (IBAction)btSend:(id)sender {
    NSError *errorReturned = nil;
    NSString *urlString = @"http://192.168.1.180:8080/DataProvider.aspx/DoLogin";
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod: @"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"myUsername" forKey:@"username"];
    [dict setObject:@"myPassword" forKey:@"password"];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions     error:&errorReturned];
    [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody: jsonData];

    NSURLResponse *theResponse =[[NSURLResponse alloc]init];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
    if (errorReturned)
    {
        //...handle the error
    }
    else
    {
        NSString *retVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@", retVal);
        //...do something with the returned value        
    }
}

Hope this helps someone else

Nick
  • 727
  • 10
  • 20