Hi folks,
In this section we are going to explore new Apple framework "Twitter.framework" which is available from iOS 5. There are lots of things you can track with this framework like tweeting from your app, extracting profile and pictures of others from within your app etc. But, for this blog we are only going to send out tweets from our iOS apps. Along with the tweet text, i will even show you how to add image/pictures and links with the tweet. Lets get started then:
1. Add Twitter.framework and Accounts.framework to your project. One thing to be noted is if you are going to support your app for below iOS 5 too then you have to weak link the Twitter framework since it is not available for older versions. For this, simply click on the Twitter.Framework on the Build Phases of your target settings and change the "Required" drop down to "Optional".
2. Include #import <Twitter/Twitter.h> in your .h file.
3. Now, lets write a code for tweeting in a custom method :
}
Note : If your app is still in development phase or it is not submitted/accepted by App Store then all the tweets you make in simulator or testing devices will appear as Tweet was sent "via iOS" instead of your app name. But it will show your tweet "via yourAppName" once it is submitted and accepted by App Store.
In this section we are going to explore new Apple framework "Twitter.framework" which is available from iOS 5. There are lots of things you can track with this framework like tweeting from your app, extracting profile and pictures of others from within your app etc. But, for this blog we are only going to send out tweets from our iOS apps. Along with the tweet text, i will even show you how to add image/pictures and links with the tweet. Lets get started then:
1. Add Twitter.framework and Accounts.framework to your project. One thing to be noted is if you are going to support your app for below iOS 5 too then you have to weak link the Twitter framework since it is not available for older versions. For this, simply click on the Twitter.Framework on the Build Phases of your target settings and change the "Required" drop down to "Optional".
2. Include #import <Twitter/Twitter.h> in your .h file.
3. Now, lets write a code for tweeting in a custom method :
-(IBAction)sendTweetsFromYourApp : (id) sender {
if(NSClassFromString(@"TWTweetComposeViewController")!= nil){ // this if call is required if you are going to support your app for older than iOS 5, this simply checks whether this Tweet class is available or not in running device (we do have to weak link Tweeter framework as described above for older iOS,But for app targeting iOS 5 and later this check is not required.)
// Now checking whether we can send Tweet or not , canSendTweet: method will return NO if there is no network or if the user hasn't set up his twitter account on device.
if ([TWTweetComposeViewController canSendTweet]) {
if ([TWTweetComposeViewController canSendTweet]) {
// yes, it can send tweets, now display tweet composer
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
// you can write initial text to be appear on composer sheet always like default text
[tweetSheet setInitialText: @"Tweeting from yourAppName"];
[tweetSheet setInitialText: @"Tweeting from yourAppName"];
// adding Url (hyperlink) with your tweets (it may be the website where your app is or app info page)
[tweetSheet addURL:[NSURL URLWithString:@"your url string to show"]];
[tweetSheet addURL:[NSURL URLWithString:@"your url string to show"]];
// adding image with the tweets
[tweetSheet addImage:[UIImage imageNamed:@"your image to show"]];
[tweetSheet addImage:[UIImage imageNamed:@"your image to show"]];
// Now handling the result of composer sheet
tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result) {
[self dismissModalViewControllerAnimated:YES];
switch (result) {
case TWTweetComposeViewControllerResultCancelled:
NSLog(@"Tweet is Cancelled.");
break;
case TWTweetComposeViewControllerResultDone:
NSLog(@"Tweet is sent");
break;
default:
break;
}
};
[self presentModalViewController:tweetSheet animated:YES];
//[self presentViewController:tweetSheet animated:YES completion:nil];
}
else
{
tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result) {
[self dismissModalViewControllerAnimated:YES];
switch (result) {
case TWTweetComposeViewControllerResultCancelled:
NSLog(@"Tweet is Cancelled.");
break;
case TWTweetComposeViewControllerResultDone:
NSLog(@"Tweet is sent");
break;
default:
break;
}
};
[self presentModalViewController:tweetSheet animated:YES];
//[self presentViewController:tweetSheet animated:YES completion:nil];
}
else
{
// No, it can't send tweets, display error alert
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Sorry!"
message:@"You can't send a tweet right now, make sure you have at least one Twitter account setup.\n Kindly configure your Twitter account in Device Settings." delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Sorry!"
message:@"You can't send a tweet right now, make sure you have at least one Twitter account setup.\n Kindly configure your Twitter account in Device Settings." delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
else {
// show alert, device does not support this framework
}}
Note : If your app is still in development phase or it is not submitted/accepted by App Store then all the tweets you make in simulator or testing devices will appear as Tweet was sent "via iOS" instead of your app name. But it will show your tweet "via yourAppName" once it is submitted and accepted by App Store.
hi ameer,This is Hareesh.today i am join in your blog,if possible please provide the source code of the tasks
ReplyDeleteHi......
ReplyDeleteToday i read your blog was good & standard.
thanks.