簡體   English   中英

NSURLSession方法在登錄時返回true或false

[英]NSURLSession method returning true or false on login

我有這個UIViewController,它調用一個Web服務。

為了撥打該Web服務的電話,我需要先登錄。 成功登錄后,我收到

  • 會話cookie
  • 用戶特定數據

以后對服務器的任何調用都需要此數據。

因此,一種簡單的方法是(偽代碼):

if([self Login]){
    [self getSomeJsonDataForThisUser];
}

現在的問題是,作為一個好公民,我正在遠離NSURLConnection,而NSURLSessions都是異步的。

基本問題是:如何編寫這樣的方法:

-(BOOL)Login{
  --do some async stuff here
  --return TRUE or FALSE
)

謝謝!

由於異步行為,您需要一個帶有完成處理程序的方法

- (void)loginWithCompletion: (void (^)(BOOL))completion {
    // do some async stuff here
    completion(true); // This line returns the result of the task.
}

並使用它

[self loginWithCompletion:^(BOOL result) {
    NSLog(@"result: %d", result);
    if (result) {
        [self getSomeJsonDataForThisUser];
    }
}];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM