如何实现对本机iPhone应用程序的登录功能 [英] How to implement login functionality to native iphone app

查看:130
本文介绍了如何实现对本机iPhone应用程序的登录功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个要求用户登录以访问其某些侵权的应用程序.我有一个login.php文件存储在服务器上,所有用户名和密码都存储在数据库中.

I'm working on an app that asks the user to login to access some of his infrmations. I have a login.php file that is store on the server and all the usernames and passwords on the database.

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

On my app i have 2 uitextfields one for the username and one for the password. I understand that i can use the POST method to pass on the input to the web server and check wether the username and password match to then load the rest of the data. This is where am having trouble, can anyone help me with it, how do i pass on the inputs from the uitextfield to that web service to run the long.php script?

推荐答案

Skram的答案是正确的,尽管您可能正在考虑什么是ASIHTTPRequest?

Skram's answer is correct, although you might be thinking what is ASIHTTPRequest?

您可以在此处获取框架: ASIHTTP请求主页

You can get the framework here: ASIHTTPrequest home

以下是有关如何使用它的简短简短教程: 很棒的教程

Here is a short beautiful tutorial on how to use it: Awesome Tutorial

这是我前一段时间用来登录的一些代码:

And here is some code I used to do a login some time ago:

  -(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...
    }
}}

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

Here some more code, this is what happens when the server returns a response.

- (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实现的委托方法中捕获并处理响应字符串(或失败). c1>.在示例代码中,我将响应字符串解析为JSON,然后将其放入字典中以备后用.

Basically when you start the request using request startSynchronous it executes you server side code and returns a response string (or fails), you catch and handle the response string (or failure)in a delegate method you must implement from ASIHttpRequest -(void)requestFinished:(ASIHTTPRequest *)request. In the sample code I am parsing the response string into a JSON and then putting it into a dictionary for later use.

如果您阅读了本教程,这对您很快就会有意义.希望对您有帮助,祝您好运!

If you go through the tutorial this will all make sense to you very quickly. Hope it helps, good luck!

这篇关于如何实现对本机iPhone应用程序的登录功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆