我正在将twitter集成到我的应用程序中以共享文本。我的twitter集成体系结构是我有两个Button _btnTwitter
和_btntwitteLogout
,当用户成功登录twitter时,他的名字将显示在_btntwitterLogout
作为标题。
当用户点击_btntwitterLogout
它注销时,Twitter和注销按钮隐藏,登录按钮就会出现。一切顺利。但是当用户点击登录按钮(_btnTwitter
)时,Twitter登录用于登录目的的popup cmes,这是面临的问题,当弹出窗口用户点击该弹出窗口的取消按钮弹出窗口消失时,这里用户的先前用户名在注销按钮中没有改变。我已经设置了用户默认值。
-(IBAction)_clickbtnTwitter:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Twitter_logged"];
[_btntwitterLogeout setTitle:nil forState:UIControlStateNormal];
_btntwitterLogeout.hidden = NO;
_btnTwitter.hidden=YES;
if (_engine) return;
_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate: self];
_engine.consumerKey = kOAuthConsumerKey;
_engine.consumerSecret = kOAuthConsumerSecret;
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _engine delegate: self];
if (controller)
[self presentModalViewController: controller animated: YES];
else {
[_engine sendUpdate: [NSString stringWithFormat: @"Already Updated. %@", [NSDate date]]];
}
}
}
然后注销代码
-(IBAction)_clickbtntwitterlogeout:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"Twitter_logged"];
crosstwitterimage.hidden = YES;
[_btntwitterLogeout setTitle:nil forState:UIControlStateNormal];
_btntwitterLogeout.hidden = YES;
_btnTwitter.hidden=NO;
_btnTwittermain.enabled = NO;
[_engine clearAccessToken];
[_engine clearsCookies];
[_engine setClearsCookies:YES];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"authData"];
[[NSUserDefaults standardUserDefaults]removeObjectForKey:@"authName"];
NSLog(@"%@",[[NSUserDefaults standardUserDefaults]valueForKey:@"authName"]);
NSLog(@"%@",[[NSUserDefaults standardUserDefaults]valueForKey:@"authData"]);
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"Twitter_logged"];
[_engine release];
_engine=nil;
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
NSString* domainName = [cookie domain];
NSRange domainRange = [domainName rangeOfString:@"twitter"];
if(domainRange.length > 0)
{
[storage deleteCookie:cookie];
}
}
}
在viewwillappear中
BOOL logged = [[NSUserDefaults standardUserDefaults] boolForKey:@"Twitter_logged"];
;
if (logged == YES) {
_btnTwitter. hidden = YES;
_btntwitterLogeout.hidden = NO;
crosstwitterimage.hidden = NO;
_btnTwittermain.enabled =YES;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//Checks if there is a saved User Name
if([defaults objectForKey:@"kTwitterUserName"])
{
NSString *username = [defaults objectForKey:@"kTwitterUserName"];
[_btntwitterLogeout setTitle:username forState:UIControlStateNormal];
crosstwitterimage.hidden = NO;
}
}
else
{
_btnTwitter. hidden = NO;
crosstwitterimage.hidden = YES;
[_btntwitterLogeout setTitle:nil forState:UIControlStateNormal];
_btnTwittermain.enabled =NO;
crosstwitterimage.hidden = YES;
}
但是当我点击twitter的登录窗口中的取消按钮时,如果用户已经从Twitter发送,则返回带有注销按钮的用户名。 在我的代码中有任何问题。 提前谢谢。
修改
#pragma mark SA_OAuthTwitterEngineDelegate
- (void) storeCachedTwitterOAuthData: (NSString *) data forUsername: (NSString *) username {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: data forKey: @"authData"];
[defaults synchronize];
}
- (NSString *) cachedTwitterOAuthDataForUsername: (NSString *) username {
return [[NSUserDefaults standardUserDefaults] objectForKey: @"authData"];
}
#pragma mark SA_OAuthTwitterControllerDelegate
- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username {
NSLog(@"Authenicated for %@", username);
[_btntwitterLogeout setTitle:username forState:UIControlStateNormal];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:username forKey:@"kTwitterUserName"];
[defaults synchronize];
}
- (void) OAuthTwitterControllerFailed: (SA_OAuthTwitterController *) controller {
NSLog(@"Authentication Failed!");
}
- (void) OAuthTwitterControllerCanceled: (SA_OAuthTwitterController *) controller {
NSLog(@"Authentication Canceled.");
}
#pragma mark TwitterEngineDelegate
- (void) requestSucceeded: (NSString *) requestIdentifier {
NSLog(@"Request %@ succeeded", requestIdentifier);
}
- (void) requestFailed: (NSString *) requestIdentifier withError: (NSError *) error {
NSLog(@"Request %@ failed with error: %@", requestIdentifier, error);
}
答案 0 :(得分:0)
是的,有问题。每当您点击 _btnTwitter 时,您都将 Twitter_logged 设置为是。那不对。只有在成功登录后才能将其设置为yes。那是用户在Twitter登录窗口中点击取消 NOT 的时候。
换句话说,您需要在用户实际登录后将 Twitter_logged 设置为YES,而不是在用户点击按钮 _btnTwitter
时似乎 SA_OAuthTwitterController 有一个指向self的委托属性。您应该处理代码以在该委托方法中设置Twitter_logged YES。
希望有所帮助。
编辑:
鉴于此处的最新代码,您应该添加NsUserDefaults数据
- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username {
NSLog(@"Authenicated for %@", username);
[_btntwitterLogeout setTitle:username forState:UIControlStateNormal];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:username forKey:@"kTwitterUserName"];
[defaults setBool:YES forKey:@"Twitter_logged"]; // Right here - mbh
[defaults synchronize];
}