2010-12-19 35 views

回答

2

在深入研究代碼並玩弄東西之後,我找到了一種方法來做到這一點,無論您在何處獲得框架,都可能記錄在案。

查看iCodeOauthViewController.m,在viewDidAppear:之內,您可以在引擎上調用isAuthorized,它會告訴您您是否已通過身份驗證。如果返回yes,則可以調用引擎對象上的clearAccessToken方法來清除該身份驗證。當下一次調用controllerToEnterCredentialsWithTwitterEngine: delegate:時,它將返回視圖控制器以重新輸入用戶名和密碼。

編輯: 在iCodeOauthViewController.m內FO viewDidAppear:(46行),你會看到這樣一行:

UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _engine delegate: self]; 

這個調用返回的登錄屏幕,你看到,如果用戶還沒有登錄。如果用戶已登錄,則返回nil。如果控制器爲零,則直接跳轉到列表。

爲「註銷」,用戶可以使用這個方法:

- (void)switchUser 
{ 
    // log off the existing user if one is validated 
    if ([_engine isAuthorized]) 
     [_engine clearAccessToken]; 

    // display the login prompt 
    UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _engine delegate: self];  
    if (controller) 
     [self presentModalViewController: controller animated: YES]; 
} 

編輯2: 看起來你的問題是你的tweet方法裏面。鳴叫嘗試後發送,並導致如果用戶沒有登錄崩潰您已經添加了報警代碼這裏是你的代碼:

-(IBAction)tweet:(id)sender { 

    [textfield resignFirstResponder]; 
    [_engine sendUpdate:[textfield text]]; 
    [self updateStream:nil]; 


    if([_engine isAuthorized]==NO){UIAlertView *alert = [[UIAlertView alloc] 
                 initWithTitle: @"Please, Sign in" 
                 message: @"You'll have to sign in for this app to work!" 
                 delegate: nil 
                 cancelButtonTitle:@"Ok" 
                 otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
     } 
} 

改變它看起來像這樣:

-(IBAction)tweet:(id)sender { 


if([_engine isAuthorized]==NO){ 
    UIAlertView *alert = [[UIAlertView alloc] 
                initWithTitle: @"Please, Sign in" 
                message: @"You'll have to sign in for this app to work!" 
                delegate: nil 
                cancelButtonTitle:@"Ok" 
                otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    } 
else { 
    [textfield resignFirstResponder]; 
    [_engine sendUpdate:[textfield text]]; 
    [self updateStream:nil]; 

} 

}

請注意,我們現在檢查一下,如果我們要發送的鳴叫之前要驗證,如果我們沒有授權,則彈出一個警告來代替。我的道歉,我可能誤導了你釋放警報的事情,我誤解了你的意思。

我會建議嘗試瞭解更多關於objective-c如何工作並獲得familiar with the debugger。如果您運行調試器並且您的應用程序崩潰,則調試器將停止在崩潰的代碼中,您可以查看堆棧中的函數調用以確定代碼做錯了什麼。請參閱this stack overflow question(特別是答案)以獲取有關如何更好地開始使用objective-c的更多資源。我會推薦一些像CocoaDevCentral's教程的在線網站。 Remember this。基於一個例子,你嘗試做出自己的東西是一個很好的開始。如果在主項目中沒有馬上解決問題,不要害怕做一個副項目,即使它找到了另一種方法來做2 + 2這樣簡單的事情。希望有所幫助。

+0

對不起,但我仍然在學習objective-c。你有可能向我提供代碼嗎? – Tapy 2010-12-20 17:17:02

+0

我想通過使用調試器發現此屏幕出現的位置以及授權的發生位置。你可能想嘗試一下,看看你是否可以弄清楚,即使是上面的代碼。首先在viewDidAppear:方法的頂部放置一個斷點。 – slycrel 2010-12-20 19:50:35

+0

謝謝!我也設法給一個按鈕的動作去頁面。但有一件事我在調試器中弄不清楚。你會發現如果你沒有輸入用戶名和密碼就點擊取消,並嘗試發佈應用程序崩潰的內容。是否可以顯示警報或告訴用戶他/她沒有登錄? – Tapy 2010-12-20 21:30:03