title: ios app consuming webservice
body: <p>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 <code>http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service</code> and read a SO thread  <code>http://stackoverflow.com/questions/7364326/recommendations-for-net-web-service-format-protocol-for-android-iphone-etc-i</code> but I am still unclear as to how I should approach it.</p>
  
  <p>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))</p>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>EDIT: After reading through the php webservice, there are a few things which I do not have in mine. </p>
  
  <p>function redeem from <code>http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app</code></p>
  
  <pre><code>// Put parameters into local variables
          $rw_app_id = $_POST["rw_app_id"];
          $code = $_POST["code"];
          $device_id = $_POST["device_id"]
  </code></pre>
  
  <p>If I'm using asmx file, do I also have to declare something like this in every of my web method?</p>
  
  <p>E.G of one of my webMethod (basic CRUD)</p>
  
  <p>[WebMethod]</p>
  
  <pre><code>        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);
                  }
  
  }
  </code></pre>
  
  <p>Under consuming webservice from app side</p>
  
  <p>the example given in the tutorial was </p>
  
  <pre><code>- (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;
  }
  </code></pre>
  
  <p>But shouldn't it be a <code>IBAction</code> button to call instead of a <code>BOOL</code>?</p>
  
  <pre><code>- (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;
  }
  </code></pre>
  
  <p>Correct me if I'm wrong, but this method <code>- (void)requestFinished:(ASIHTTPRequest *)request</code> 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 <code>- (void)requestFinished:(ASIHTTPRequest *)request</code></p>
  
  <p>EDIT: any help would really be appreciated... I'm a beginner at this and hope that someone with experience could guide me through.</p>