Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
Tweeting/TweetingViewController.m
/* |
File: TweetingViewController.m |
Abstract: This view controller allows the user to send a tweet by using the built-in composition sheet or by using a custom post request. The user can additionally get the current Twitter public timeline. |
Version: 1.0 |
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple |
Inc. ("Apple") in consideration of your agreement to the following |
terms, and your use, installation, modification or redistribution of |
this Apple software constitutes acceptance of these terms. If you do |
not agree with these terms, please do not use, install, modify or |
redistribute this Apple software. |
In consideration of your agreement to abide by the following terms, and |
subject to these terms, Apple grants you a personal, non-exclusive |
license, under Apple's copyrights in this original Apple software (the |
"Apple Software"), to use, reproduce, modify and redistribute the Apple |
Software, with or without modifications, in source and/or binary forms; |
provided that if you redistribute the Apple Software in its entirety and |
without modifications, you must retain this notice and the following |
text and disclaimers in all such redistributions of the Apple Software. |
Neither the name, trademarks, service marks or logos of Apple Inc. may |
be used to endorse or promote products derived from the Apple Software |
without specific prior written permission from Apple. Except as |
expressly stated in this notice, no other rights or licenses, express or |
implied, are granted by Apple herein, including but not limited to any |
patent rights that may be infringed by your derivative works or by other |
works in which the Apple Software may be incorporated. |
The Apple Software is provided by Apple on an "AS IS" basis. APPLE |
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION |
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS |
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND |
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. |
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL |
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, |
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED |
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), |
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE |
POSSIBILITY OF SUCH DAMAGE. |
Copyright (C) 2011 Apple Inc. All Rights Reserved. |
*/ |
#import "TweetingViewController.h" |
@implementation TweetingViewController |
@synthesize outputTextView, easyTweetButton, customTweetButton; |
- (void)didReceiveMemoryWarning |
{ |
[super didReceiveMemoryWarning]; |
// Release any cached data, images, etc that aren't in use. |
} |
#pragma mark - View lifecycle |
- (void)viewDidLoad |
{ |
[super viewDidLoad]; |
// Do any additional setup after loading the view, typically from a nib. |
// This notification is posted when the accounts managed by this account store changed in the database. |
// When you receive this notification, you should refetch all account objects. |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(canTweetStatus) name:ACAccountStoreDidChangeNotification object:nil]; |
} |
- (void)viewDidUnload |
{ |
[super viewDidUnload]; |
// Release any retained subviews of the main view. |
// e.g. self.myOutlet = nil; |
self.easyTweetButton = nil; |
self.customTweetButton = nil; |
self.outputTextView = nil; |
} |
- (void)viewDidAppear:(BOOL)animated |
{ |
[super viewDidAppear:animated]; |
// Check to make sure our UI is set up appropriately, depending on if we can tweet or not. |
[self canTweetStatus]; |
} |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation |
{ |
// Return YES for supported orientations |
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); |
} |
- (IBAction)sendEasyTweet:(id)sender { |
// Set up the built-in twitter composition view controller. |
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init]; |
// Set the initial tweet text. See the framework for additional properties that can be set. |
[tweetViewController setInitialText:@"Hello. This is a tweet."]; |
// Create the completion handler block. |
[tweetViewController setCompletionHandler:^(TWTweetComposeViewControllerResult result) { |
NSString *output; |
switch (result) { |
case TWTweetComposeViewControllerResultCancelled: |
// The cancel button was tapped. |
output = @"Tweet cancelled."; |
break; |
case TWTweetComposeViewControllerResultDone: |
// The tweet was sent. |
output = @"Tweet done."; |
break; |
default: |
break; |
} |
[self performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO]; |
// Dismiss the tweet composition view controller. |
[self dismissModalViewControllerAnimated:YES]; |
}]; |
// Present the tweet composition view controller modally. |
[self presentModalViewController:tweetViewController animated:YES]; |
} |
- (IBAction)sendCustomTweet:(id)sender { |
// 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]; |
// For the sake of brevity, we'll assume there is only one Twitter account present. |
// You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present. |
if ([accountsArray count] > 0) { |
// Grab the initial Twitter account to tweet from. |
ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; |
// Create a request, which in this example, posts a tweet to the user's timeline. |
// This example uses version 1 of the Twitter API. |
// This may need to be changed to whichever version is currently appropriate. |
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:@"Hello. This is a tweet." forKey:@"status"] requestMethod:TWRequestMethodPOST]; |
// Set the account used to post the tweet. |
[postRequest setAccount:twitterAccount]; |
// Perform the request created above and create a handler block to handle the response. |
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { |
NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]]; |
[self performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO]; |
}]; |
} |
} |
}]; |
} |
- (IBAction)getPublicTimeline:(id)sender { |
// Create a request, which in this example, grabs the public timeline. |
// This example uses version 1 of the Twitter API. |
// This may need to be changed to whichever version is currently appropriate. |
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/public_timeline.json"] parameters:nil requestMethod:TWRequestMethodGET]; |
// Perform the request created above and create a handler block to handle the response. |
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { |
NSString *output; |
if ([urlResponse statusCode] == 200) { |
// Parse the responseData, which we asked to be in JSON format for this request, into an NSDictionary using NSJSONSerialization. |
NSError *jsonParsingError = nil; |
NSDictionary *publicTimeline = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError]; |
output = [NSString stringWithFormat:@"HTTP response status: %i\nPublic timeline:\n%@", [urlResponse statusCode], publicTimeline]; |
} |
else { |
output = [NSString stringWithFormat:@"HTTP response status: %i\n", [urlResponse statusCode]]; |
} |
[self performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO]; |
}]; |
} |
- (void)displayText:(NSString *)text { |
self.outputTextView.text = text; |
} |
// This method checks if we are able to tweet from the application. |
// We set the UI appropriately if Twitter is available and if a Twitter account is present. |
// This is called when the app loads initially and if the ACAccountStore changes while the app is being used. |
- (void)canTweetStatus { |
if ([TWTweetComposeViewController canSendTweet]) { |
self.easyTweetButton.enabled = YES; |
self.easyTweetButton.alpha = 1.0f; |
self.customTweetButton.enabled = YES; |
self.customTweetButton.alpha = 1.0f; |
} else { |
self.easyTweetButton.enabled = NO; |
self.easyTweetButton.alpha = 0.5f; |
self.customTweetButton.enabled = NO; |
self.customTweetButton.alpha = 0.5f; |
} |
} |
@end |
Copyright © 2011 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2011-10-12