0

我是 iPhone 应用程序的新手。我已按照本教程从 iPhone 登录。

我有如下的php文件。

索引.php

<?php
$user = $_POST['uname'];

if ($user == 'user') {
    echo "Welcome to my site...";
} else {
    echo "Invalid User";
}
?>

当我运行应用程序并输入用户名作为用户名和密码作为一些文本时,我得到的输出是Welcome to my site ...。

现在不是欢迎信息,我想进入我的欢迎屏幕,即我在故事板上的视图控制器。知道如何完成这项工作吗?

完整代码如下。

-(IBAction)buttonClick:(id)sender
{
    greeting.text= @"";
    NSString* username = nameInput.text;
    NSString* pass = passInput.text;
    greeting.hidden = NO;
    if([nameInput.text isEqualToString:@"" ] && [passInput.text isEqualToString:@""])
    {
        greeting.text = @"Please enter username and password.";
        [nameInput resignFirstResponder];
        [passInput resignFirstResponder];
        return;
    }

    if([nameInput.text isEqualToString:@"" ])
    {
        greeting.text = @"Please enter username.";
        [nameInput resignFirstResponder];
        [passInput resignFirstResponder];
        return;
    }

    if([passInput.text isEqualToString:@""])
    {
        greeting.text = @"Please enter password.";
        [nameInput resignFirstResponder];
        [passInput resignFirstResponder];
        return;
    }


    NSString *post = [[NSString alloc] initWithFormat:@"uname=%@&pwd=%@",username,pass];

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSURL *url = [NSURL URLWithString:@"http://localhost:8888/PhPTesting/index.php"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPBody:postData];


    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if( theConnection )
    {
        indicator.hidden = NO;
        webData = [[NSMutableData data] retain];
    }
    else
    {

    }

    [nameInput resignFirstResponder];
    [passInput resignFirstResponder];
    nameInput.text = nil;
    passInput.text = nil;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [connection release];
    [webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    greeting.text = loginStatus;
    [loginStatus release];

    [connection release];
    [webData release];
    indicator.hidden = YES;
}
4

2 回答 2

1

你可以这样做 :

if ([loginStatus isEqualToString:@"Welcome to my site..."]) {
    // Show welcome view controller
    NextViewController *nextViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"myNextView"];
    [self.navigationController pushViewController:nextViewController animated:YES];
} else {
    greeting.text = @"Login was not correct";
}

这里,@"myNextView" 是 NextView 标识符。

于 2012-12-26T07:53:26.507 回答
0

你可以检查结果

if ([loginStatus isEqualToString:@"Welcome to my site..."]) {
    // Show welcome view controller
} else {
    greeting.text = @"Login was not correct";
}

我还建议通过 PHP 返回一个更有帮助的字符串


更新

有大量的教程可以展示一个新的视图。

在这里查看:在 Cocoa Touch 中以编程方式切换视图

于 2012-12-25T14:48:37.520 回答