Showing posts with label iOS6. Show all posts
Showing posts with label iOS6. Show all posts

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.

Monday, November 26, 2012

iOS6, Rotation.

Hi, it's been a while since I posted some thing.

Ok, This post is to fix the auto-rotation problem that has been around since the release of iOS6.

To be clear, the problem is that iOS6 doesn't support - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; any more. And hence, our apps/code won't behave as expected any more.

In this post, I'll be using three methods/callbacks were introduced in iOS6 which are (BOOL)shouldAutorotate- (NSUInteger)supportedInterfaceOrientations and (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window.

One important thing to know, (BOOL)shouldAutorotate and (NSUInteger)supportedInterfaceOrientations are added to the topmost parent of the view controller that you want to control its rotation. if, of-course, the view controller is a root view controller, then these methods will be added to it directly.

I hate to repeat myself, but for the sake of clarification, if your root-view-controller is a navigation-controller and you want to control the rotation of a view-controller inside the navigation-controller then  (BOOL)shouldAutorotate and (NSUInteger)supportedInterfaceOrientations will be added to the navigation-controller NOT the view-controller itself.

If you have a tab-bar-controller that contains a navigation-controller inside which lies the view-controller that you want to control its rotation, then (BOOL)shouldAutorotate and (NSUInteger)supportedInterfaceOrientations will be added to the topmost parent which is - in this case - the tab-bar-controller.

How to add those methods to a tab-bar-controller or a navigation-controller?
  1. By subclassing them and overriding the methods inside them.
  2. By adding them to a category of your tab-bar-controller's or navigation-controller's type. I like this method because, it's simpler specially if the code is already written and i'm doing some slight modifications.
Now, lets take the case of a tab-bar-controller that contains a navigation-controller inside which lies the view-controller that you want to control its rotation as an example. you'll do as follows:
  1. add (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window to your appdelegate as follows:
     - (NSUInteger)application:(UIApplication *)application 
    supportedInterfaceOrientationsForWindow:(UIWindow *)window {  
       return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |  
           UIInterfaceOrientationMaskLandscapeRight);  
     }  
    

  2. create a category inside your appdelegate as follows:
     @implementation tab-view-controller (fixingRotation)  
       
     - (BOOL)shouldAutorotate {  
        
       if ([((UINavigationController *)[[((AppDelegate *)[[UIApplication sharedApplication]
     delegate]).tabBarController viewControllers] objectAtIndex:self.selectedIndex])
    .visibleViewController isKindOfClass:[myViewController class]]) {  
           
         return YES;  
       }  
       else{  
           
         return NO;  
       }  
     }  
     -(NSUInteger) supportedInterfaceOrientations {  
       return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |  
           UIInterfaceOrientationMaskLandscapeRight);  
     }  
       
     @end  
And your done.

Don't remove -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation from your code to keep supporting iOS5.

It's recommended to see the documentation for better understanding of the callbacks.

That's it, don't hesitate to comment, to share your knowledge and to correct me.