如何在objective-c中维护一个会话? [英] How to maintain a session in objective-c?

查看:128
本文介绍了如何在objective-c中维护一个会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我得到以下问题:



我有一个登录viewcontroller和窗体viewcontroller。在登录viewcontroller,我发送一个POST请求到PHP脚本,验证用户是否有访问权限。该脚本只返回1或0.所以我可以选择关闭或维护viewcontroller。当凭据正确传递时,用户将看到表单控制器。



获取表单的按钮向另一个PHP脚本发出POST请求,该脚本将返回一个带有值的XML文档的用户。我需要一种方法来记住用户通过的用户名和密码。所以我可以在另一个(视图)控制器中使用它们。



有没有人知道一种方法来实现这个?

解决方案

如果你使用NSURLConnection,并且会话是基于cookie的,这将自动完成。所以你需要写的将类似于这个

  NSMutableURLRequest * request = nil; 
request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@http://server.com/login.php]];

NSString * post = [NSString stringWithFormat:@username =%@& password =%@,@< username>,@< password&
NSData * postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setValue:[NSString stringWithFormat:@%d,[postData length]] forHTTPHeaderField:@Content-Length];
[request setTimeoutInterval:15];
[request setHTTPMethod:@POST];
[request setHTTPBody:postData];

_urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[_urlConnection start];

您还必须实现NSURLConnectionDelegate方法

   - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// Oops!句柄失败
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if(_statusCode> = 200& _ amp; _statusCode< 400){
//事情看起来很好
NSString * responseString = [[[NSString alloc] initWithData:_responseData] autorelease];
//发送到xml库并解析
}

[_responseData release];
_responseData = nil;
[connection autorelease];
}

如果您在标题中有其他信息, (void)连接:(NSURLConnection *)连接didReceiveResponse(NSURLConnection *)连接:(NSURLConnection *)连接didReceiveResponse :(NSURLResponse *)response {
if([response isKindOfClass:[NSHTTPURLResponse class]]){
NSDictionary * headerFields = [(NSHTTPURLResponse *)response allHeaderFields]; //这将给你所有的头字段;
}
}

并设置下一个请求的标题字段

  [request setValue:[NSString stringWithFormat:@%d,[postData length]] forHTTPHeaderField:@Content-Length ]; 

保存信息,无论是用户名和/您可以使用NSUserDefaults

  //保存
NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];

if(standardUserDefaults){
[standardUserDefaults setObject:@< username> forKey:@username];
[standardUserDefaults setObject:@< pass> forKey:@password];
[standardUserDefaults synchronize];
}

//检索
NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString * val = nil;

if(standardUserDefaults)
val = [standardUserDefaults objectForKey:@username];

最后,最好构建一个模型来将xml API映射到[Eg:a User类用户名和密码属性]。



Google for Apple在MVC上的文档。



希望这有助于!


So i got the following problem:

I got a login viewcontroller and a form viewcontroller. On the login viewcontroller, i am sending a POST request to a PHP script which validates if the user has access. The script just returns an 1 or 0. So i can then choose to dismiss or maintain the viewcontroller. When the credentials are passed right, the user will see the form controller. This controller got a button to fetch a form.

The button to fetch the form, makes a POST request to another PHP script, which will return an XML document with the values of the user. I need a way to remember the username and password the user has passed through. So i can use them in another (view) controller.

Does anybody know a way to achieve this?

解决方案

If you're using NSURLConnection, and the session is cookie based, this would automatically be done. So all you'd need to write would be similar to this

NSMutableURLRequest *request = nil;
request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://server.com/login.php"]];

NSString *post = [NSString stringWithFormat:@"username=%@&password=%@", @"<username>", @"<password>"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
[request setTimeoutInterval: 15];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];

_urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[_urlConnection start];

And you would have to implement the NSURLConnectionDelegate methods as well

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [_responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    //Oops! handle failure here
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    if (_statusCode >= 200 && _statusCode < 400) {
            //Things look ok
        NSString *responseString = [[[NSString alloc] initWithData:_responseData] autorelease];
        //Send this to an xml lib and parse
        }

    [_responseData release];
    _responseData = nil;
    [connection autorelease];
}

If you have some other information thats in the headers and which you need to send back with consequent requests, you can read it from the response like this

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
        NSDictionary *headerFields = [(NSHTTPURLResponse*)response allHeaderFields]; //This would give you all the header fields;
    }
}

And set the header fields for the next request like this

    [request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];

To save information, be it username and/or password or session information you can use NSUserDefaults

    //To save
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

if (standardUserDefaults) {
    [standardUserDefaults setObject:@"<username>" forKey:@"username"];
    [standardUserDefaults setObject:@"<pass>" forKey:@"password"];
    [standardUserDefaults synchronize];
}

//To retrieve
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *val = nil;

if (standardUserDefaults) 
    val = [standardUserDefaults objectForKey:@"username"];

Lastly, it would be advisable to build a model to map the xml API with [Eg: a User class with username and password properties].

Google for Apple's docs on MVC.

Hope this helps!

这篇关于如何在objective-c中维护一个会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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