iOS 5 Twitter Framework: Tweeting Without User Input and Confirmation (Modal View Controller)

iOS 5 Twitter Framework: Tweeting without user input and confirmation (modal view controller)

It's definitely possible to tweet without it, the following is in production iOS 5 apps. It even takes the user to the requisite section of preferences if they haven't registered an account.

- (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:[self stringToPost] 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) {
Alert(0, nil, @"Tweet Successful", @"Ok", nil);
}else {

Alert(0, nil, @"Tweet failed", @"Ok", nil);
}
});
}];
});

}
else
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=TWITTER"]];
}
}
}];
}

Sending Tweet without TWTweetComposeViewController

You can use TWRequest for programatically posting tweets from my app, if you are using iOS 5.0 and up. Posting tweets is quite straightforward and there is no need for an external framework or anything.

You need the #import . You retrieve the twitter account from the iPhone account store (you could check if there are multiple accounts) and then you use the account to post the request.

Here is an example of a method for posting a tweet with an image.

- (void)shareTwitterImage:(UIImage *)image
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
{
if(granted)
{
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];

if ([accountsArray count] > 0)
{
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];

TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"] parameters:[NSDictionary dictionaryWithObject:self.textViewOutlet.text forKey:@"status"] requestMethod:TWRequestMethodPOST];

[postRequest addMultiPartData:UIImagePNGRepresentation(image) withName:@"media" type:@"multipart/png"];
[postRequest setAccount:twitterAccount];

[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
//show status after done
NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
NSLog(@"Twiter post status : %@", output);
}];
}
}
}];
}

Automatically tweet from iOS application using Twitter Framework - Without using TWTweetComposeViewController

Use below code to do post image and text without showing ViewContoller . This is called silent Post.

 - (void) shareOnTwitterWithMessage:(NSString *)message {

ACAccountStore *twitterAccountStore = [[ACAccountStore alloc]init];
ACAccountType *TWaccountType= [twitterAccountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

[twitterAccountStore requestAccessToAccountsWithType:TWaccountType options:nil completion:

^(BOOL granted, NSError *e) {

if (granted) {

NSArray *accounts = [twitterAccountStore accountsWithAccountType:TWaccountType];

twitterAccounts = [accounts lastObject];

NSDictionary *dataDict = @{@"status": message};

[self performSelectorInBackground:@selector(postToTwitter:) withObject:dataDict];

}
else {

return ;
}

}];
}


- (void)postToTwitter:(NSDictionary *)dataDict{

NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update_with_media.json"];

SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:requestURL parameters:dataDict];

NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"icon@2x.png"]);

[request addMultipartData:imageData
withName:@"media[]"
type:@"image/jpeg"
filename:@"image.jpg"];

request.account = twitterAccounts;

[request performRequestWithHandler:^(NSData *data, NSHTTPURLResponse *response, NSError *error) {

if(!error){

NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

if(![list objectForKey:@"errors"]){

if([list objectForKey:@"error"]!=nil){

//Delegate For Fail
return ;
}

}
}

}];

}

How to post on twitter by hiding SLComposeViewController *tweetSheettweet instance

 - (IBAction)doneButtonClicked:(id)sender
{
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSString *message = _textView.text;
//hear before posting u can allow user to select the account
NSArray *arrayOfAccons = [account accountsWithAccountType:accountType];
for(ACAccount *acc in arrayOfAccons)
{
NSLog(@"%@",acc.username); //in this u can get all accounts user names provide some UI for user to select,such as UITableview
}
in below


// Request access from the user to access their Twitter account
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
// Populate array with all available Twitter accounts
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0)
{
//use the first account available
ACAccount *acct = [arrayOfAccounts objectAtIndex:0]; //hear this line replace with selected account. than post it :)

//Build a twitter request
TWRequest *postRequest = [[TWRequest alloc] initWithURL:
[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"]
parameters:[NSDictionary dictionaryWithObject:message forKey:@"status"] requestMethod:TWRequestMethodPOST];//for iOS 7


//for iOS 6 use "https://api.twitter.com/1/statuses/update.json"
//Post the request
//u should get the response code 200 for successful post
[postRequest setAccount:acct];

//manage the response
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if(error)
{
//if there is an error while posting the tweet
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"Error in posting" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
// on successful posting the tweet
NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"Successfully posted" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];

}
}];
[postRequest release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"You have no twitter account" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
else
{
//suppose user not set any of the accounts
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"Permission not granted" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
} ];

[account release]; //for non-ARC
}


iOS 5: Twitter composer view appears slowly

Yes, there is. You can preload the class by initializing it in the background sometime before you'll ever need it. Move tweetViewController into an instance or static variable, initialize and set all of its properties. Then just show it in the tweet method.

How to post on twitter by hiding SLComposeViewController *tweetSheettweet instance

 - (IBAction)doneButtonClicked:(id)sender
{
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSString *message = _textView.text;
//hear before posting u can allow user to select the account
NSArray *arrayOfAccons = [account accountsWithAccountType:accountType];
for(ACAccount *acc in arrayOfAccons)
{
NSLog(@"%@",acc.username); //in this u can get all accounts user names provide some UI for user to select,such as UITableview
}
in below


// Request access from the user to access their Twitter account
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
// Populate array with all available Twitter accounts
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0)
{
//use the first account available
ACAccount *acct = [arrayOfAccounts objectAtIndex:0]; //hear this line replace with selected account. than post it :)

//Build a twitter request
TWRequest *postRequest = [[TWRequest alloc] initWithURL:
[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"]
parameters:[NSDictionary dictionaryWithObject:message forKey:@"status"] requestMethod:TWRequestMethodPOST];//for iOS 7


//for iOS 6 use "https://api.twitter.com/1/statuses/update.json"
//Post the request
//u should get the response code 200 for successful post
[postRequest setAccount:acct];

//manage the response
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if(error)
{
//if there is an error while posting the tweet
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"Error in posting" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
// on successful posting the tweet
NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"Successfully posted" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];

}
}];
[postRequest release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"You have no twitter account" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
else
{
//suppose user not set any of the accounts
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"Permission not granted" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
} ];

[account release]; //for non-ARC
}


I want to tweet without pop dialog by TWTweetComposeViewController

i have done this by following code...

-(void)postToTwittert:(NSString *)stringtoPost
{
Class TWTweetComposeViewControllerClass = NSClassFromString(@"TWTweetComposeViewController");

if (TWTweetComposeViewControllerClass != nil) {
if([TWTweetComposeViewControllerClass respondsToSelector:@selector(canSendTweet)]) {
TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];

[twitter setInitialText:stringtoPost];

[twitter addImage:[UIImage imageNamed:@"apple.jpg"]];
[twitter addURL:[NSURL URLWithString:@"http://www.erwinzwart.com"]];
[self findSubViewofTwitter:twitter.view];
[self presentViewController:twitter animated:YES completion:nil];

twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {

if(res == TWTweetComposeViewControllerResultDone)
{
[self completeGameStep:@"glueper2012"];

}
else if(res == TWTweetComposeViewControllerResultCancelled)
{

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Canceled" message:@"Your Tweet was not posted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

[alertView show];

}

[self dismissModalViewControllerAnimated:YES];

};
}
}
}

by finding button and firing event

    - (void) findSubViewofTwitter:(UIView*)theView
{
for (UIView * subview in theView.subviews)
{
//NSLog(@">>>>>>>>>>>>>>>>>> :%@", subview);
if ([subview isKindOfClass:[UIButton class]])
{
UIButton *btn = (UIButton *)subview;
//NSLog(@">>>>>>>>>>>>>> is kind of class :%@", btn.titleLabel.text);
if([btn.titleLabel.text isEqualToString:@"Send"])
{
[btn sendActionsForControlEvents:UIControlEventTouchUpInside];
}
}
[self findSubViewofTwitter:subview];
}
}

Set location programmatically when Tweeting using TWTweetComposeViewController

Impossible. There is no API to set the location for a tweet, the system will always use the user's location.



Related Topics



Leave a reply



Submit