我想将Facebook和Twitter帐户与我的iOS应用程序连接起来。我正在寻找检查帐户是否在设置中的代码。如果它在那里需要使用相同的帐户登录,如果不是,则要求用户在设置中添加帐户。
我尝试了几件事,但实际上没有用。如果有人需要我的代码,请告诉我,我会将其发布在pastebin上。
答案 0 :(得分:3)
在项目中添加Twitter和社交框架,并导入类文件,如
#import <Social/Social.h>
#import <Twitter/Twitter.h>
For Facebook
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]){
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
NSData *aImgdata=[[NSUserDefaults standardUserDefaults]objectForKey:@"EditImage"];
if (aImgdata) {
UIImage *aImage=[UIImage imageWithData:aImgdata];
[controller addImage:aImage];
}
controller.completionHandler = ^(SLComposeViewControllerResult result) {
if(result == SLComposeViewControllerResultDone) {
}
[self dismissViewControllerAnimated:YES completion:nil];
};
[self presentViewController:controller animated:YES completion:Nil];
}
else
{
[self ShowAlertView: @"You can't share on facebook right now, make sure your device has an internet connection and you have at least one Facebook account setup"];
}
For Twitter
if([TWTweetComposeViewController canSendTweet]){
{
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
NSData *aImgdata=[[NSUserDefaults standardUserDefaults]objectForKey:@"EditImage"];
if (aImgdata) {
UIImage *aImage=[UIImage imageWithData:aImgdata];
[tweetViewController addImage:aImage];
}
tweetViewController.completionHandler = ^(TWTweetComposeViewControllerResult result) {
if(result == TWTweetComposeViewControllerResultDone) {
[self ShowAlertView: @"Tweet Has been Shared"];
}
[[AppDelegate sharedInstance]showActivity:NO];
[self dismissViewControllerAnimated:YES completion:nil];
};
[self presentViewController:tweetViewController animated:YES completion:nil];
}
} else {
[self ShowAlertView: @"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup"];
}
答案 1 :(得分:1)
为您的项目添加社交框架并在您的课程中导入
#import <Social/Social.h>
对于Facebook
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller addImage:UIImage];
controller.completionHandler = ^(SLComposeViewControllerResult result) {
if(result == SLComposeViewControllerResultDone) {
}
[self dismissViewControllerAnimated:YES completion:nil];
};
[self presentViewController:controller animated:YES completion:Nil];
}
else
{
//Show Alert: No Facebook Account in Settings Page
}
对于Twitter
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[controller addUrl:NSURL];
controller.completionHandler = ^(SLComposeViewControllerResult result) {
if(result == SLComposeViewControllerResultDone) {
}
[self dismissViewControllerAnimated:YES completion:nil];
};
[self presentViewController:controller animated:YES completion:Nil];
}
else
{
//Show Alert: No Twitter Account in Settings Page
}
答案 2 :(得分:0)
要检查您是否可以与Facebook或Twitter分享,请使用:
[SLComposeViewController isAvailableForServiceType:serviceType]
其中serviceType可以是任何SLServiceTypes,例如SLServiceTypeFacebook
和SLServiceTypeTwitter
这基本上将检查他们是否在其设置中本机连接了这些服务。我认为您不能要求用户在其设置中添加它。大多数情况下,如果某个特定的共享选项在UIActionSheet
上不可用,则不会显示。
我的猜测是您可以使用SLComposeViewController
检查已连接的帐户,如果没有连接,请使用相应的SDK将其帐户与您的iOS应用相关联。