0

我有一个网络服务,我正在使用一个网址,例如

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

如您所见,用户名已设置为“bob”,密码设置为“new”。该站点在输入时会提供此 json 文件,

[{"success":"1"}]

你如何在 xcode 上实现它,当用户输入"bob"用户名和"new"密码时,它应该引导到下一个控制器。我怎样才能做到这一点?

我按照本教程进行了操作,尽管它并不完全相同,您是如何做到的。谢谢。

4

5 回答 5

2

使用导航控制器并将视图控制器设置为 rootViewController。然后在用户输入凭据后,将新的视图控制器推送到导航堆栈上。

于 2013-02-01T10:20:55.337 回答
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];

接收并有效的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];    
        }


    }
于 2013-02-01T10:35:32.137 回答
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);
        }

    }
于 2013-02-01T10:30:45.907 回答
0

从 json 响应中获取成功的值。如果它等于 1,则推送到下一个控制器,否则什么也不做。

于 2013-02-01T10:28:35.510 回答
0

不完全是您正在寻找的答案,但这更像是您应该使用什么的建议。我使用github 链接进行网络相关活动。他们有很好的文档并且非常易于使用(使用块)

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:

于 2013-02-01T11:25:26.030 回答