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.-