如何实现本机iphone应用程序的登录功能

时间:2012-06-06 23:46:05

标签: iphone web-services login web

我正在开发一个应用程序,要求用户登录以访问他的一些信息。我有一个存储在服务器上的login.php文件以及数据库上的所有用户名和密码。

在我的应用程序上,我有2个uitextfields用于用户名,1个用于密码。我知道我可以使用POST方法将输入传递给Web服务器,并检查用户名和密码是否匹配,然后加载其余数据。这是我遇到麻烦的地方,任何人都可以帮助我,我如何将uitextfield的输入传递给该Web服务以运行long.php脚本?

2 个答案:

答案 0 :(得分:2)

Skram的回答是正确的,尽管你可能在想什么是ASIHTTPRequest?

您可以在此处获取框架: ASIHTTPrequest home

这是一个关于如何使用它的简短漂亮的教程: Awesome Tutorial

以下是我之前用于登录的一些代码:

  -(IBAction)doLogin{

//make sure you have text in the username field, this is optional
if(![[username text] isEqualToString:@""]){
    //URL of your web service 
    NSURL *url = [NSURL URLWithString:@"http://localhost:8888/myservice.php"];
    //instantiate request object with URL
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    //Set post values before you send it off (forcing password to lowercase)
    [request setPostValue:[[self.password text] lowercaseString] forKey:@"password"];
    [request setPostValue:[username text] forKey:@"email"];
    //this is optional, I have switch controlling what methods are called in the web service 
    [request setPostValue:@"2" forKey:@"method"];
    //set the delegate to self for ASIHTTPRequest delegates (things you'll see in the tutorial)
    [request setDelegate:self];
    //send out request
    [request startSynchronous];


    //Now this code handles what happens after the web service call, notice I use a dictionary called user info to hold the response and I check the string to verify pass or fail and act accordingly.

    NSString *verify = [NSString stringWithFormat:@"%@",[userinfo objectForKey:@"verify"]];
    if([verify isEqualToString:@"pass"]){
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
                            @"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
        UITabBarController *mainMenu = 
        [storyboard instantiateViewControllerWithIdentifier:@"mainMenu"];

        [self.navigationController pushViewController:mainMenu animated:YES];
    }
    else{
        //show login failed Dialog or something...
    }
}}

这里有更多代码,这是服务器返回响应时发生的情况。

- (void)requestFinished:(ASIHTTPRequest *)request{       
if (request.responseStatusCode == 400) {
    NSLog(@"Something is wrong");        
} else if (request.responseStatusCode == 403) {
    NSLog(@"Something is wrong");
} else if (request.responseStatusCode == 200) {
    //the response code is good so proceed.
    NSString *responseString = [request responseString];
    userinfo = [responseString JSONValue];
        NSLog(@"%@", [userinfo objectForKey:@"id"]);
        NSLog(@"%@", [userinfo objectForKey:@"user"]);
        NSLog(@"%@", [userinfo objectForKey:@"verify"]);   
} else {
    //print mystery code.
    NSLog(@"%d",request.responseStatusCode);

}

}

基本上,当您使用request startSynchronous启动请求时,它会执行服务器端代码并​​返回响应字符串(或失败),您必须在必须实现的委托方法中捕获并处理响应字符串(或失败) ASIHttpRequest -(void)requestFinished:(ASIHTTPRequest *)request。在示例代码中,我将响应字符串解析为JSON,然后将其放入字典中供以后使用。

如果你完成本教程,这对你很有意义。希望它有所帮助,祝你好运!

答案 1 :(得分:1)

您需要在服务器上使用对{。1}}和username值的login.php脚本发出的POST请求。

您可以使用passwordASIHTTPRequest NSURLConnection等框架来实现这一目标。

在服务器上,您应该执行ASIFormDataRequest$_POST['username']之类的操作,以检索发送到脚本的值,以便对数据库进行处理。

编辑:来自iPhone端和ASIHTTPRequest的基本示例。

$_POST['password']

实现以下方法以捕获响应:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
[request setPostValue:@"some_user" forKey:@"username"];
[request setPostValue:@"some_password" forKey:@"password"];
[request  setDelegate:self];
[request startAsynchronous];