我得到了以下情況。我正在做一個帶有tabbarcontroller和導航控制器的iPhone應用程序。使用該應用程序登錄到Web服務器(提供Web服務)以獲取會話Cookie後,我單擊Tabbarnavigation菜單,向其中一個Web服務發出請求。調用這個web服務,我得到了一個句柄狀態代碼,告訴我即使該應用程序第一次成功登錄到web服務器,也不會登錄到服務器。所以我不知道我在做什麼錯我離開這裏我的代碼片段。ASIHttprequest登錄
的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"];
}
}
這裏的問題是:當我拿到菜單,我不是正從我的web服務的任何數據,實際應用甚而不嘗試調用它,我使用BaseController從那裏調用每一個webservice,所以我得到了從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中
調用WebService的- (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(即使登錄以前是成功的),然後再次調用web服務登錄。直接在第二次登錄後,我無法獲得任何數據。
嗯,我不知道我能做什麼,謝謝你的所有建議和答案。