2013-02-01 47 views
0

我有一個Web服務,我有一個網址的工作,如POST用戶名和密碼JSON

//myurl/index.jsp?user_name=bob&user_pwd=new 

,你可以看到用戶名已經被設置爲「鮑勃」和密碼「新」。進入時,該網站給這個JSON文件,

[{"success":"1"}] 

你如何實現它在Xcode中,當用戶輸入"bob"如用戶名和"new"作爲密碼應該導致下一個控制器的位置。我該如何做到這一點?

我跟着這tutorial雖然它不完全相同,你怎麼做到這一點。謝謝。

+0

你正面臨什麼問題?你有什麼嘗試? – CRDave

+0

任何工作@iPatel –

回答

2

使用導航控制器並將視圖控制器設置爲rootViewController。然後,在用戶輸入憑據後,將新視圖控制器推入導航堆棧。

+0

我已經實現,已經是一個替代品,但我需要的應用程序發佈用戶名和密碼到web服務url並檢查它匹配,然後如果是這樣,使用導航控制器推到下一個控制器 –

+0

你的問題是什麼。您可以檢查返回的JSON,並在答案成功時推送下一個視圖控制器。 – dasdom

0

從json響應中獲得成功的價值。如果它等於1,那麼推到下一個控制器,否則什麼都不做。

+0

爲什麼有人不高興呢? – Rox

0

在下面的代碼,你可以通過GETPOST方法(正如你希望)傳遞數據...使用任何一個

-(void) sendRequest 
{ 
    //////////////////////////// GET METHOD ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

    NSString *strURL=[NSString stringWithFormat:@"http://myurl/index.jsp?user_name=bob&user_pwd=new"]; 
    self.request=[NSURLRequest requestWithURL:[NSURL URLWithString:strURL]]; 
    self.nsCon=[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

//////////////////////////// POST METHOD ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

    NSString *postString = [NSString stringWithFormat:@"&user_name=bob&user_pwd=new"]; 
    NSString *url = [NSString stringWithFormat:@"http://myurl/index.jsp/"]; 

    self.request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]]; 
    [self.request setHTTPMethod:@"POST"]; 

    [self.request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; 

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

    if(self.nsCon) 
    { 
     self.receivedData=[[NSMutableData alloc] init]; 
    } 
    else 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error",@"") message:NSLocalizedString(@"Not Connected !!",@"") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
    } 
} 

#pragma mark - 
#pragma mark - Connection Delegate Methods 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [self.responseData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [self.responseData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    [GeneralClass stopHUD]; 
    NSLog(@"Connection failed."); 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error",@"") message:NSLocalizedString(@"Connection failed!",@"") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil]; 
    [alert show]; alert = nil; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
    { 
     NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding]; 
     NSMutableDictionary *receivedData = [responseString JSONValue]; 

     if([[receivedData objectForKey:@"success"] isEqualToString:@"1"]) 
     { 
      MainDetailViewController *mdController = [[MainDetailViewController alloc] init]; 
      [self.navigationController pushViewController:mdController animated:YES]; 
     } 
     else 
     { 
      NSLog(@"%@",receivedData); 
     } 

    } 
+0

真棒謝謝你,但我如何使登錄按鈕的方法,我很困惑 –

+0

創建按鈕與方法...並調用**按鈕方法中的sendRequest **方法 – iPatel

+0

你只需要調用** sendRequest **方法:)或者在viewDidload或Button操作方法中......謝謝:) – iPatel

0

登錄後數據

NSString *soapMsg = [NSString stringWithFormat:@"&data={\"LoginUser\":[{\"UserName\":\"%@\",\"Password\":\"%@\"}]}",firstname.text, password.text]; 

    NSHTTPURLResponse *response; 
    NSData *myRequestData = [ NSData dataWithBytes: [ soapMsg UTF8String ] length: [ soapMsg length ] ]; 
    NSString *postLength = [NSString stringWithFormat:@"%d", [myRequestData length]]; 
    NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString:@"http://myurl/index.jsp"]]; 

    [request setHTTPMethod: @"POST" ]; 
    [request setHTTPBody: myRequestData ]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody: myRequestData]; 

    NSURLConnection *myConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

接收和vaild json數據

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
     NSString *responseString = [[NSString alloc] initWithData:WebXmlData encoding:NSUTF8StringEncoding]; 
     NSDictionary *results = [responseString JSONValue]; 
     BOOL success = [[results objectForKey:@"success"] boolValue]; 

     if (success) { 
      ViewController *viewController =[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil]; 
      [self.navigationController pushViewController:viewController animated:YES];  
     } 


    } 
+0

實例不應以大寫字母開頭。 – dasdom

+0

我有點困惑,你把第一組代碼放在viewdidload方法中,第二組代碼放在我的登錄按鈕方法中? –

0

不是你正在尋找的答案,但這是更多的建議你應該使用什麼。 我使用github link進行網絡相關活動。 他們有很好的文件,並非常容易使用(使用的塊)

NSDictionary *paramsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"user",@"client_id",@"password",@"new", nil]; 

    [[YourAFHttpClientExtenstion sharedInstance] postPath:LOGIN_PATH parameters:paramsDictionary success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     //handle success 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     // handle error. 
    }]; 

YourAFHttpClientExtenstion是延長AFHttpClient,並增加了共享實例的方法召集並實現initWithBaseUr: