Twitter上有Firebase iOS简单登录示例吗?
我很难从简单的登录iOS中找到以下问题的解决方案: 由于一台设备可能连接多个Twitter帐户,因此您需要提供一个块,该块可用于确定登录哪个帐户。将下面的“[yourApp selectUserName:UserName]”替换为您自己的代码,以便从用户名列表中进行选择 这是提供的代码:Twitter上有Firebase iOS简单登录示例吗?,ios,objective-c,firebase,firebasesimplelogin,Ios,Objective C,Firebase,Firebasesimplelogin,我很难从简单的登录iOS中找到以下问题的解决方案: 由于一台设备可能连接多个Twitter帐户,因此您需要提供一个块,该块可用于确定登录哪个帐户。将下面的“[yourApp selectUserName:UserName]”替换为您自己的代码,以便从用户名列表中进行选择 这是提供的代码: [authClient loginToTwitterAppWithId:@"YOUR_CONSUMER_KEY" multipleAccountsHandler:^int(NSArray *usernames)
[authClient loginToTwitterAppWithId:@"YOUR_CONSUMER_KEY"
multipleAccountsHandler:^int(NSArray *usernames) {
// If you do not wish to authenticate with any of these usernames, return NSNotFound.
return [yourApp selectUserName:usernames];
} withCompletionBlock:^(NSError *error, FAUser *user) {
if (error != nil) {
// There was an error authenticating
} else {
// We have an authenticated Twitter user
}
}];
允许用户选择使用哪个帐户的UIActionSheet是最好的吗?那怎么办
#import <Twitter/Twitter.h>
#import <Accounts/Accounts.h>
它将打开一个操作表,显示设备拥有的帐户列表
投寄:
您的iPhone设备上必须至少添加一个有效的twitter帐户
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
NSString *initialText=@"Text to be posted";
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
switch(result) {
// This means the user cancelled without sending the Tweet
case SLComposeViewControllerResultCancelled:{
}
break;
case SLComposeViewControllerResultDone:{
}
break;
}
};
[tweetSheet setInitialText:initialText];
if (initialText) {
[self presentViewController:tweetSheet animated:YES completion:nil];
}
}
我发现了一个很好的例子,说明了如何使用的代码实现这一点。它使用UIActionSheet,就像我尝试的那样
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (IBAction)actionTwitter:(id)sender
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[ProgressHUD show:@"In progress..." Interaction:NO];
//---------------------------------------------------------------------------------------------------------------------------------------------
selected = 0;
//---------------------------------------------------------------------------------------------------------------------------------------------
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
//---------------------------------------------------------------------------------------------------------------------------------------------
[account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
{
if (granted)
{
accounts = [account accountsWithAccountType:accountType];
//-------------------------------------------------------------------------------------------------------------------------------------
if ([accounts count] == 0)
[self performSelectorOnMainThread:@selector(showError:) withObject:@"No Twitter account was found" waitUntilDone:NO];
//-------------------------------------------------------------------------------------------------------------------------------------
if ([accounts count] == 1) [self performSelectorOnMainThread:@selector(loginTwitter) withObject:nil waitUntilDone:NO];
if ([accounts count] >= 2) [self performSelectorOnMainThread:@selector(selectTwitter) withObject:nil waitUntilDone:NO];
}
else [self performSelectorOnMainThread:@selector(showError:) withObject:@"Access to Twitter account was not granted" waitUntilDone:NO];
}];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)selectTwitter
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"Choose Twitter account" delegate:self cancelButtonTitle:nil
destructiveButtonTitle:nil otherButtonTitles:nil];
//---------------------------------------------------------------------------------------------------------------------------------------------
for (NSInteger i=0; i<[accounts count]; i++)
{
ACAccount *account = [accounts objectAtIndex:i];
[action addButtonWithTitle:account.username];
}
//---------------------------------------------------------------------------------------------------------------------------------------------
[action addButtonWithTitle:@"Cancel"];
action.cancelButtonIndex = accounts.count;
[action showInView:self.view];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
if (buttonIndex != actionSheet.cancelButtonIndex)
{
selected = buttonIndex;
[self loginTwitter];
}
else [ProgressHUD dismiss];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)loginTwitter
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
Firebase *ref = [[Firebase alloc] initWithUrl:FIREBASE];
FirebaseSimpleLogin *authClient = [[FirebaseSimpleLogin alloc] initWithRef:ref];
//---------------------------------------------------------------------------------------------------------------------------------------------
[authClient loginToTwitterAppWithId:TWITTER_KEY multipleAccountsHandler:^int(NSArray *usernames)
{
return (int) selected;
}
withCompletionBlock:^(NSError *error, FAUser *user)
{
if (error == nil)
{
if (user != nil) [delegate didFinishLogin:ParseUserData(user.thirdPartyUserData)];
[self dismissViewControllerAnimated:YES completion:^{ [ProgressHUD dismiss]; }];
}
else
{
NSString *message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
[self performSelectorOnMainThread:@selector(showError:) withObject:message waitUntilDone:NO];
}
}];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
-(iAction)actionTwitter:(id)发件人
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[ProgressHUD show:@“正在进行…”交互:否];
//---------------------------------------------------------------------------------------------------------------------------------------------
所选=0;
//---------------------------------------------------------------------------------------------------------------------------------------------
ACAccountStore*account=[[ACAccountStore alloc]init];
ACAccountType*accountType=[帐户类型WithAccountTypeIdentifier:ACAccountTypeIdentifierWitter];
//---------------------------------------------------------------------------------------------------------------------------------------------
[account requestAccessToAccountsWithType:accountType选项:无完成:^(已授予BOOL,N错误*错误)
{
如果(授予)
{
accounts=[accounts accountsWithAccountType:accountType];
//-------------------------------------------------------------------------------------------------------------------------------------
如果([帐户计数]==0)
[self-performSelectorOnMainThread:@selector(淋浴错误:)with object:@“未找到Twitter帐户”waitUntilDone:No];
//-------------------------------------------------------------------------------------------------------------------------------------
如果([accounts count]==1)[自执行SelectorOnMainThread:@selector(LoginWitter),对象为nil waitUntilDone:NO];
如果([accounts count]>=2)[自执行SelectorOnMainThread:@selector(selectTwitter)with object:nil waitUntilDone:NO];
}
else[self-performSelectorOnMainThread:@selector(淋浴错误:)with object:@“未授予对Twitter帐户的访问权”waitUntilDone:NO];
}];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
-(无效)选择Twitter
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
UIActionSheet*action=[[UIActionSheet alloc]initWithTitle:@“选择Twitter帐户”委托:自我取消按钮:nil
破坏性按钮:无其他按钮:无];
//---------------------------------------------------------------------------------------------------------------------------------------------
对于(NSInteger i=0;i),最新的iOS 8 beta版似乎无法实现此功能。苹果公司已弃用UIActionSheet,转而支持UIAlertViewController。
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (IBAction)actionTwitter:(id)sender
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[ProgressHUD show:@"In progress..." Interaction:NO];
//---------------------------------------------------------------------------------------------------------------------------------------------
selected = 0;
//---------------------------------------------------------------------------------------------------------------------------------------------
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
//---------------------------------------------------------------------------------------------------------------------------------------------
[account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
{
if (granted)
{
accounts = [account accountsWithAccountType:accountType];
//-------------------------------------------------------------------------------------------------------------------------------------
if ([accounts count] == 0)
[self performSelectorOnMainThread:@selector(showError:) withObject:@"No Twitter account was found" waitUntilDone:NO];
//-------------------------------------------------------------------------------------------------------------------------------------
if ([accounts count] == 1) [self performSelectorOnMainThread:@selector(loginTwitter) withObject:nil waitUntilDone:NO];
if ([accounts count] >= 2) [self performSelectorOnMainThread:@selector(selectTwitter) withObject:nil waitUntilDone:NO];
}
else [self performSelectorOnMainThread:@selector(showError:) withObject:@"Access to Twitter account was not granted" waitUntilDone:NO];
}];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)selectTwitter
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"Choose Twitter account" delegate:self cancelButtonTitle:nil
destructiveButtonTitle:nil otherButtonTitles:nil];
//---------------------------------------------------------------------------------------------------------------------------------------------
for (NSInteger i=0; i<[accounts count]; i++)
{
ACAccount *account = [accounts objectAtIndex:i];
[action addButtonWithTitle:account.username];
}
//---------------------------------------------------------------------------------------------------------------------------------------------
[action addButtonWithTitle:@"Cancel"];
action.cancelButtonIndex = accounts.count;
[action showInView:self.view];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
if (buttonIndex != actionSheet.cancelButtonIndex)
{
selected = buttonIndex;
[self loginTwitter];
}
else [ProgressHUD dismiss];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)loginTwitter
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
Firebase *ref = [[Firebase alloc] initWithUrl:FIREBASE];
FirebaseSimpleLogin *authClient = [[FirebaseSimpleLogin alloc] initWithRef:ref];
//---------------------------------------------------------------------------------------------------------------------------------------------
[authClient loginToTwitterAppWithId:TWITTER_KEY multipleAccountsHandler:^int(NSArray *usernames)
{
return (int) selected;
}
withCompletionBlock:^(NSError *error, FAUser *user)
{
if (error == nil)
{
if (user != nil) [delegate didFinishLogin:ParseUserData(user.thirdPartyUserData)];
[self dismissViewControllerAnimated:YES completion:^{ [ProgressHUD dismiss]; }];
}
else
{
NSString *message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
[self performSelectorOnMainThread:@selector(showError:) withObject:message waitUntilDone:NO];
}
}];
}