通过iPhone中的SLComposeViewController进行Twitter集成

时间:2014-05-10 07:39:06

标签: ios twitter

我正在使用SLComposeViewController在twitter上分享图片。如果我没有通过iPhone设置登录,那么我想显示警报视图。请任何机构建议做什么。

3 个答案:

答案 0 :(得分:2)

使用此代码:

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
//show compose view
else
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Not available"
                                                  message:@"Twitter is not available."
                                                 delegate:nil
                                        cancelButtonTitle:@"OK"
                                        otherButtonTitles:nil];

[message show];

答案 1 :(得分:2)

使用ACAccountStore和ACcount,您可以检查您是否已登录。

请参阅此处的示例代码:How to post on twitter by hiding SLComposeViewController *tweetSheettweet instance

答案 2 :(得分:2)

在.h文件中声明

@property (strong, nonatomic) ACAccount *fbAccount;
@property (strong, nonatomic) NSString *slService;
@property (strong, nonatomic) ACAccountStore *accountStore;

添加.m文件

- (IBAction)TwittButtonPressed:(id)sender
{
if(!accountStore)
    accountStore = [[ACAccountStore alloc] init];

ACAccountType *twitterAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

// Request access to the Twitter account with the access info

[self.accountStore requestAccessToAccountsWithType:twitterAccountType options:nil completion:^(BOOL granted, NSError *error) {
    if (granted) {
        // If access granted, then get the Twitter account info
        NSArray *accounts = [self.accountStore accountsWithAccountType:twitterAccountType];
        if (accounts.count>0) {
            self.fbAccount=[accounts lastObject];
        //NSLog(@"Twitter account Details =%@",self.fbAccount);
        self.fbAccount=[accounts objectAtIndex:0];
        NSString *username1 = self.fbAccount.username;
                   NSUserDefaults   *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setValue:username1 forKey:@"twitUserName"];
        [defaults synchronize];
        //NSLog(@"USer ID %@and Username%@",[defaults valueForKey:@"twitUserID"],[defaults valueForKey:@"twitUserName"]);

        self.slService = SLServiceTypeTwitter;
           // NSLog(@"Show indicatoer############");
        [self performSelectorOnMainThread:@selector(sharingPostData:) withObject:_slService waitUntilDone:YES];


        }
        else{
           // NSLog(@"Access not granted");
            [self performSelectorOnMainThread:@selector(throwAlertWithTitle:) withObject:@"Account not found. Please setup your account in settings" waitUntilDone:NO];
        }
        //  [self sharingPostData:self.slService];

    } else {
       // NSLog(@"Access not granted");
        [self performSelectorOnMainThread:@selector(throwAlertWithTitle:) withObject:@"Account not found. Please setup your account in settings" waitUntilDone:NO];
    }
}];      
}

-(void)throwAlertWithTitle:(NSString *)message{
//remove DSActivity if any
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Notification" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
errorAlert=nil;
}

-(void)sharingPostData:(NSString *)serviceType{
    if ([SLComposeViewController isAvailableForServiceType:serviceType])
    {

        SLComposeViewController *fvc = [SLComposeViewController composeViewControllerForServiceType:serviceType];

            [fvc setInitialText:@"Share text here"];
            [fvc addImage:backGroundImage.image];
            [fvc addURL:[NSURL URLWithString:kAppituneslink]];

        [self presentViewController:fvc animated:YES completion:nil];
    }
}