Thursday, January 24, 2013

Twitter for iOS6 without the modal view.

Hi,
I guess I got this one from some Stackoverflow question.
Link against Social.framework and accounts.framework.
 #import <Accounts/Accounts.h>  
 #import <Social/Social.h>  
 =========================================
 ACAccountStore *account = [[ACAccountStore alloc] init];  
 ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:    
           ACAccountTypeIdentifierTwitter];  
       
 [account requestAccessToAccountsWithType:accountType options:nil   
         completion:^(BOOL granted, NSError *error)  
 {  
    if (granted == YES)  
    {  
      NSArray *arrayOfAccounts = [account   
          accountsWithAccountType:accountType];  
   
      if ([arrayOfAccounts count] > 0)  
      {  
       ACAccount *twitterAccount = [arrayOfAccounts lastObject];  
   
       NSDictionary *message = @{@"status": @”My First Twitter post from iOS6”};  
         
       NSURL *requestURL = [NSURL   
        URLWithString:@"http://api.twitter.com/1/statuses/update.json"];  
   
       SLRequest *postRequest = [SLRequest   
         requestForServiceType:SLServiceTypeTwitter  
             requestMethod:SLRequestMethodPOST  
             URL:requestURL parameters:message];  
   
       postRequest.account = twitterAccount;  
   
       [postRequest performRequestWithHandler:^(NSData *responseData,   
             NSHTTPURLResponse *urlResponse, NSError *error)  
       {  
          NSLog(@"Twitter HTTP response: %i", [urlResponse   
             statusCode]);  
       }];  
      }  
    }  
 }];  
That's it, don't hesitate to comment, to share your knowledge and to correct me. 
Hi,
I found this here.
Link the project against Twitter.framework and accounts.framework.
 #import <Twitter/Twitter.h>  
 #import <Accounts/Accounts.h>  
 =============================================  
 - (void)postToTwitter   
  {   
   // Create an account store object.   
   ACAccountStore *accountStore = [[ACAccountStore alloc] init];   
     
   // Create an account type that ensures Twitter accounts are retrieved.   
   ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:  
   ACAccountTypeIdentifierTwitter];   
     
   // Request access from the user to use their Twitter accounts.   
   [accountStore requestAccessToAccountsWithType:accountType   
   withCompletionHandler:^(BOOL granted, NSError *error) {   
    if(granted) {   
     // Get the list of Twitter accounts.   
     NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];   
     
     if ([accountsArray count] > 0) {   
      // Grab the initial Twitter account to tweet from.   
      ACAccount *twitterAccount = [accountsArray objectAtIndex:0];   
      TWRequest *postRequest = nil;   
     
      postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:  
             @"http://api.twitter.com/1/statuses/update.json"]   
             parameters:[NSDictionary dictionaryWithObject:  
             @"The message to post" forKey:@"status"]   
             requestMethod:TWRequestMethodPOST];   
     
      // Set the account used to post the tweet.   
      [postRequest setAccount:twitterAccount];   
     
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {   
       [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {   
        dispatch_async(dispatch_get_main_queue(), ^(void) {   
         if ([urlResponse statusCode] == 200) {   
          NSLog(@"Successful");   
         }else {   
     
         NSLog(@"Failed");   
         }   
        });   
       }];   
      });   
     
     }   
     else   
     {   
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=TWITTER"]];   
     }   
    }   
   }];   
  }
That's it, don't hesitate to comment, to share your knowledge and to correct me.