ASIHttprequest登录

时间:2011-11-27 18:39:08

标签: iphone objective-c uitabbarcontroller asihttprequest

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

的AppDelegate

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

- (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"];
    }
}

问题出现了:当我收到菜单时,我没有从我的webservices获取任何数据,实际上应用程序甚至没有尝试调用它,我正在使用BaseController从那里调用每个web服务所以我得到了我的从BaseController扩展的HomeViewController,所以我得到了一个泛型方法来调用我的BaseController上的webservice,如下所示:

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

调用web服务
- (void) viewDidLoad
{
    [super viewDidLoad];
    [self callRequest];

}

HomeViewController

- (id) init
{
    if ((self = [super init]))
    {
        self.title = @"My Home";
        self.url = @"http://localhost/services/gethome"
    }

    return self;
}

总而言之,问题是:我无法从tabbarnavigation获取任何部分的数据。我第一次点击1项时,它会加载loginviewcontroller(即使先前登录成功),然后再次调用webservice登录。并且在第二次登录后,我无法获得任何数据。

嗯,我不知道我能做什么,感谢你的所有建议和答案。

1 个答案:

答案 0 :(得分:0)

您的问题可能是您未在登录请求中接受Cookie。在登录电话后测试您的cookie的价值。