首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASIHttprequest登录

ASIHttprequest登录
EN

Stack Overflow用户
提问于 2011-11-28 02:39:08
回答 1查看 830关注 0票数 0

我遇到了以下情况。我正在做一个带有and栏控制器和导航控制器的iPhone应用程序。在使用该应用程序登录到web服务器(提供web服务)以获取会话cookie后,我单击了to栏导航菜单,该菜单向其中一个web服务发出请求。调用这个web服务时,我得到了一个处理状态代码,告诉我im没有登录到服务器,即使应用程序第一次成功地登录到了web服务器。所以我不确定我做错了什么,我在这里留下了我的代码片段。

AppDelegate

代码语言:javascript
复制
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [super application:application didFinishLaunchingWithOptions:launchOptions];
    [self initTabBarMenu];

    return YES;
}    
// call when the app is not login
- (void) showLogin 
{
    loginViewController = [[LoginViewController alloc] init];
    [self.tabBarController presentModalViewController:loginViewController animated:YES];
}

LoginViewController

代码语言:javascript
复制
- (void) viewDidLoad
{
    [super viewDidLoad];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *username = [defaults objectForKey:@"usernameApp"];
    NSString *password = [defaults objectForKey:@"passwordAp"];

    if (![StrUtil isEmpty:username] && ![StrUtil isEmpty:password])
    {
        userField.text = username;
        passwordField.text = password;
        [self loginCheck];
    }

}

- (void) loginCheck
{
    NSString *url = @"http://localhost/service/login";

     ASIFormDataRequest *theRequest = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
    [theRequest setUseKeychainPersistence:YES];  
    [theRequest setDelegate:self];
    [theRequest addPostValue:userField.text forKey:@"username"];
    [theRequest addPostValue:passwordField.text forKey:@"password"];
    [theRequest setDidFinishSelector:@selector(loginDone:)];
    [theRequest setDidFailSelector:@selector(loginFailed:)];
    [theRequest startAsynchronous];
    [userField resignFirstResponder];
    [passwordField resignFirstResponder];
}

- (void) loginResponse:(ASIFormDataRequest *)theRequest
{
    nsData = [[NSDictionary alloc] initWithDictionary:[theRequest.responseString JSONValue]];
    [self hideProgress];

    if ([[nsData objectForKey:@"status"] integerValue] == 1)
    {

        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:userField.text forKey:@"usernameApp"];
        [defaults setObject:passwordField.text forKey:@"passwordApp"]; 
        [defaults synchronize];
        // Remove current view controller
        [self.parentViewController dismissModalViewControllerAnimated:YES];
    }
    else
    {
        [self showAlert:@"Problem"];
    }
}

问题是:当我得到菜单时,我没有从我的get服务中获得任何数据,实际上应用程序甚至不会尝试调用它,我使用BaseController从那里调用每个get服务,所以我得到了从BaseController扩展的HomeViewController,所以我得到了一个通用方法来调用我的BaseController上的webservice,如下所示:

代码语言:javascript
复制
- (void) callRequest
{
    //url is a NSString and you can set on HomeviewController
    if ([StrUtils isEmpty:url]) return;
    request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
    //[request setDownloadCache:[ASIDownloadCache sharedCache]];
    [request setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
    [request setSecondsToCache:60*60*24*7]; // Cache for 7 days
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];

    [request setDelegate:self];
    [request setResponseEncoding:NSUTF8StringEncoding];
    [request setDefaultResponseEncoding:NSUTF8StringEncoding];
    [request setDidFinishSelector:@selector(requestFinished:)];
    [request setDidFailSelector:@selector(finishedWithError:)];
    [request setTimeOutSeconds:15];
    [request startAsynchronous];
}

- (void) requestFinished:(ASIHTTPRequest*)theRequest
{
    NSString *responseString = [theRequest responseString];
    nsData = [[NSDictionary alloc] initWithDictionary:[responseString JSONValue]];

    if ([[receivedData objectForKey:@"status"] integerValue] == 1)
    {
        return [(AppDelegate *)[[UIApplication sharedApplication] delegate] showLogin];
    }   
}

然后我从viewDidLoad调用the服务

代码语言:javascript
复制
- (void) viewDidLoad
{
    [super viewDidLoad];
    [self callRequest];

}

HomeViewController

代码语言:javascript
复制
- (id) init
{
    if ((self = [super init]))
    {
        self.title = @"My Home";
        self.url = @"http://localhost/services/gethome"
    }

    return self;
}

所以总而言之,问题是:我不能从我的标签栏导航中获取任何部分的任何数据。当我第一次点击1个项目时,它会加载loginviewcontroller (尽管之前登录成功了),然后调用webservice再次登录。直接在第二次登录后,我无法获取任何数据。

嗯,我不知道我能做些什么,谢谢你的建议和回答。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-28 09:15:20

您的问题可能是您不接受登录请求中的cookies。测试以查看在登录调用之后cookie的值是什么。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8288017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档