2

I am using SLComposeViewController to share the images on twitter. If i am not logged-in via iPhone settings than i want to show alert view. Please any body will suggest what to do.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Gaurav Gilani
  • 1,584
  • 14
  • 18

3 Answers3

2

Use this code:

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];
Khawar Ali
  • 3,462
  • 4
  • 27
  • 55
2

Using ACAccountStore and ACAccount you can Check weather you have login or not.

Refer the sample code here: How to post on twitter by hiding SLComposeViewController *tweetSheettweet instance

Community
  • 1
  • 1
Velmurugan S
  • 619
  • 3
  • 21
2

Declare in .h file

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

Add in .m file

- (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];
    }
}
Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81
Kittu
  • 1,577
  • 1
  • 9
  • 12