<HTTP>ASI实现的登陆方法

服务端文档:

1.登录 /my/login.php

输入参数: username password

例如: username=test&password=123456

输出响应:
<result>
<code>login_success</code>
<uid>登录成功</uid>
<message>登录成功</message> <m_auth>f06eYFOrTIA/QmDa4mNEr1QJYeMeXi+Nfy5JF2441xNA4m5Cd192+V OkFT3Te1pYomXeU0SuMvWpMn5d8pla</m_auth>
</result> 

登陆方法代码

- (void) loginToSns:(id)sender {
    NSString *username = usernameField.text;
    NSString *password = passwordField.text;
    //拼接登录地址
    NSString *s = [NSString stringWithFormat:@"%@/%@", QFSNS_HOST, @"/my/login.php"];
    NSURL *url = [NSURL URLWithString:s];
    //使用post方式发送登录信息
    ASIFormDataRequest *r = [ASIFormDataRequest requestWithURL:url];
    [r setPostValue:username forKey:@"username"];
    [r setPostValue:password forKey:@"password"];
    r.delegate = self;
    r.tag = 101;
    [r startAsynchronous];
}

ASI的requestFinish方法代码:

- (void) requestFinished:(ASIHTTPRequest *)request {
    //查看打印当前请求的地址
    NSLog(@"request is %@", request.responseString);
    //当前请求的类型为登录
    if (request.tag == 101) {
        NSDictionary *dict = [request.responseString JSONValue];
        NSString *token = [dict objectForKey:@"m_auth"];
        NSLog(@"token is %@", token);
        //将token保存到文件,保存时间由服务器决定
        if (token) {
            NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
            [ud setValue:token forKey:@"TOKEN"];
            [ud synchronize];
        }
    }
}

 

 

posted @ 2012-11-14 17:26  白条围巾  阅读(474)  评论(0编辑  收藏  举报