I've done lots of reading and researching on webservice and consuming them from a iOS app side. But many website are very brief and didn't go indepth about consuming and parsing the data. Currently I'm reading a tutorial by raywenderlich http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service
and read a SO thread http://stackoverflow.com/questions/7364326/recommendations-for-net-web-service-format-protocol-for-android-iphone-etc-i
but I am still unclear as to how I should approach it.
From what I know so far, the few methods to consume webservice are REST and SOAP protocol. (I have my app in xcode and webservice done in VB (.asmx file))
I'm the kind of guy who learn more and faster through hands on by tutorial rather than reading article and such. Is there any tutorial that work on consuming .asmx(webservice built with VS) rather than php. Personally I would prefer REST protocol which parse JSON since many people say it's easier to use and that Apple already have a lib/framework for it.
Will be great if anyone could point me in the right direction as I feel that learning how to let an iOS app consume web service is very important in future app development.
EDIT: After reading through the php webservice, there are a few things which I do not have in mine.
function redeem from http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app
// Put parameters into local variables
$rw_app_id = $_POST["rw_app_id"];
$code = $_POST["code"];
$device_id = $_POST["device_id"]
If I'm using asmx file, do I also have to declare something like this in every of my web method?
E.G of one of my webMethod (basic CRUD)
[WebMethod]
public string insertUser(string User, string Password, string Gender)
{
try
{
string connectionString = ConfigurationManager.ConnectionStrings["mysql"].ToString();
MySqlCommand dCmd = new MySqlCommand();
using (MySqlConnection mysqlCon = new MySqlConnection(connectionString))
{
if (do_check_record(User) == false)
{
mysqlCon.Open();
dCmd.CommandText = "INSERT into tbl_login (username,password,gender) values (?username,?password,?gender)";
dCmd.CommandType = CommandType.Text;
dCmd.Parameters.Add(new MySqlParameter("?username", User));
dCmd.Parameters.Add(new MySqlParameter("?password", Password));
dCmd.Parameters.Add(new MySqlParameter("?gender", Gender));
dCmd.Connection = mysqlCon;
dCmd.ExecuteNonQuery();
mysqlCon.Close();
}
else
{
return string.Format( "User exists in data_base");
}
}
return string.Format("data_base insert");
}
catch (Exception ex)
{
return string.Format(ex.Message);
}
}
Under consuming webservice from app side
the example given in the tutorial was
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"Want to redeem: %@", textField.text);
// Get device unique ID
UIDevice *device = [UIDevice currentDevice];
NSString *uniqueIdentifier = [device uniqueIdentifier];
// Start request
NSString *code = textField.text;
NSURL *url = [NSURL URLWithString:@"http://www.wildfables.com/promos/"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"1" forKey:@"rw_app_id"];
[request setPostValue:code forKey:@"code"];
[request setPostValue:uniqueIdentifier forKey:@"device_id"];
[request setDelegate:self];
[request startAsynchronous];
// Hide keyword
[textField resignFirstResponder];
// Clear text field
textView.text = @"";
return TRUE;
}
But shouldn't it be a IBAction
button to call instead of a BOOL
?
- (void)requestFinished:(ASIHTTPRequest *)request
{
if (request.responseStatusCode == 400) {
textView.text = @"Invalid code";
} else if (request.responseStatusCode == 403) {
textView.text = @"Code already used";
} else if (request.responseStatusCode == 200) {
NSString *responseString = [request responseString];
NSDictionary *responseDict = [responseString JSONValue];
NSString *unlockCode = [responseDict objectForKey:@"unlock_code"];
if ([unlockCode compare:@"com.razeware.test.unlock.cake"] == NSOrderedSame) {
textView.text = @"The cake is a lie!";
} else {
textView.text = [NSString stringWithFormat:@"Received unexpected unlock code: %@", unlockCode];
}
} else {
textView.text = @"Unexpected error";
}
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
textView.text = error.localizedDescription;
}
Correct me if I'm wrong, but this method - (void)requestFinished:(ASIHTTPRequest *)request
used in the tutorial is the one that is parsing JSON, right? If so, for every webmethod that I call, I have to make a new request and a new - (void)requestFinished:(ASIHTTPRequest *)request
EDIT: any help would really be appreciated... I'm a beginner at this and hope that someone with experience could guide me through.