我是 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;
}