准备好了使用“关注 Twitter”的本地实现。
您可以考虑使用 CocoaPods 来管理您的依赖项,而不是直接将源文件添加到项目中。按照 CocoaPods 站点的说明安装 gem,并在 Podfile 中指定 VTFollowOnTwitter 作为依赖项
pod 'VTFollowOnTwitter', '~> 0.5'
您还可以下载 VTFollowOnTwitter 的源文件,并启用 ARC 将其添加到您的项目中。请不要忘记在目标配置中添加 Accounts
和 Social
框架。
主方法需要要关注到的 Twitter 用户名。
[VTFollowOnTwitter followUsername:@"test"
fromPreferredUsername:nil
success:^{ /* Good */ }
multipleAccounts:^(NSArray *usernames) { /* Need specific username */ }
failure:^(NSError *error) { /* Not good */ }];
您的控制器需要处理用户已经配置了多个 Twitter 帐户的情况。例如,您可以使用 UIActionSheet
来提供必要的选项。
@interface VTViewController : UIViewController
- (IBAction)followOnTwitter:(id)sender;
@end
// ---
@interface VTViewController () <UIActionSheetDelegate>
- (void)followOnTwitterFromUsername:(NSString *)fromUsername;
@end
@implementation VTViewController
- (IBAction)followOnTwitter:(id)sender {
[self followOnTwitterFromUsername:nil];
}
- (void)followOnTwitterFromUsername:(NSString *)fromUsername {
NSString *username = @"StudioAMANgA";
[VTFollowOnTwitter followUsername:username fromPreferredUsername:fromUsername success:^{
[[[UIAlertView alloc] initWithTitle:@"Thanks!" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
} multipleAccounts:^(NSArray *usernames) {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Which account do you want to follow us with?" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
for (NSString *username in usernames)
[actionSheet addButtonWithTitle:[@"@" stringByAppendingString:username]];
[actionSheet addButtonWithTitle:@"Cancel"];
[actionSheet setCancelButtonIndex:[usernames count]];
[actionSheet showInView:self.view];
} failure:^(NSError *error) {
[[[UIAlertView alloc] initWithTitle:@"Request Error" message:[NSString stringWithFormat:@"Sorry, something went wrong.\n%@", [error localizedDescription]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != actionSheet.cancelButtonIndex) {
[self followOnTwitterFromUsername:[[actionSheet buttonTitleAtIndex:buttonIndex] stringByReplacingOccurrencesOfString:@"@" withString:@""]];
}
}
@end
VTFollowOnTwitter 需要 iOS 6.0 及以上版本,包含 Accounts
和 Social
框架,Xcode 6.3 及以上版本,并使用 ARC。
VTFollowOnTwitter 由 Vincent Tourraine 创建。
VTFollowOnTwitter 在 MIT 许可协议下可用。有关更多信息,请参阅 LICENSE 文件。