iOS open Facebook and Twitter app share page - ios

I want to open the Facebook and twitter app from my application. At the same time I have a text which has to be auto populated in the Facebook share box. How do I achieve this? I am able to open the app now. But how do I open the share page and pass the text to be populated.
The same with Twitter.

You can easily post to facebook with image and text. For this you don't have to exit your app. Try this:
- (void) postFacebook {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *fbSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[fbSheet setInitialText:#"Your Text here"];
[fbSheet addImage:[UIImage imageNamed:#"Icon-144.png"]];
[fbSheet addURL:[NSURL URLWithString:#"http://asdf.com"]];
[[CCDirector sharedDirector] presentViewController:fbSheet animated:YES completion:nil];
}
else {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Sorry!!!"
message:#"You can't share on facebook right now, make sure your device has an internet connection and you have at least one facebook account setup in your device."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
For tweeter do this:
- (void) callTwitter {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:#"Your text here"];
[tweetSheet addImage:[UIImage imageNamed:#"Icon-144.png"]];
[tweetSheet addURL:[NSURL URLWithString:#"http://asdf.com"]];
[[CCDirector sharedDirector] presentViewController:tweetSheet animated:YES completion:nil];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Sorry!!!"
message:#"You can't send a tweet right now, make sure your device has an internet connection and you have at least one twitter account setup in your device."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
Don't forget to add the SOCIAL.FRAMEWORK by going to
BuildPhases -> LinkBinaryWithLibraries
Also in the ViewController don't forget to import at the top #import <Social/Social.h>
EDIT:
Well I found this in SO:
NSString *myMessage = [NSString stringWithFormat:#"%#",
[self.txtAdreca.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *post = [NSString stringWithFormat:#"fb://publish/profile/me?text=%#",urlString];
BOOL canOpenURL = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:post]];
if (canOpenURL) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:post]];
}
For tweeter:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"twitter://status?id=12345"]];
Hope this helps.. :)

Related

Default string is not visible in Facebook dialog box [duplicate]

This question already has answers here:
iOS: How to share text and image on social networks?
(3 answers)
Closed 7 years ago.
I want to share photo and some text from the app on Facebook. I used SLComposeViewController class for sharing.
My problem is that when I tap on Facebook button a dialog box appears with image which I want to post but default text is not appearing in the device while in simulator it works perfectly fine. This code works perfectly for Twitter both in simulator and device. For more clarity I added code and an image
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result)
{
if (result == SLComposeViewControllerResultCancelled)
{
NSLog(#"Cancelled");
} else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Congratulations!" message:#"Photo is posted to facebook Wall." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler =myBlock;
str=[NSString stringWithFormat:#"Text to share"];
[controller setInitialText:str];
[controller addImage:savedImage];
[self presentViewController:controller animated:YES completion:Nil];
}
str=#"Text to share";
[controller setInitialText:str];
OR
[controller setInitialText:#"Text to share"];
change your code like this and try...
- (IBAction)facebookPost:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[mySLComposerSheet setInitialText:#"Social Framework test!"];
[mySLComposerSheet addImage:[UIImage imageNamed:#"myImage.png"]];
[mySLComposerSheet addURL:[NSURL URLWithString:#"http://stackoverflow.com"]];
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
switch (result) {
case SLComposeViewControllerResultCancelled:
NSLog(#"Post Canceled");
break;
case SLComposeViewControllerResultDone:
NSLog(#"Post Sucessful");
break;
default:
break;
}
}];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
}
Try this........This what I did for my project

how do i publish text and picture to Facebook news feed

I am pretty new at incorporating Facebook into iOS apps. I have read 3 books, studied the Facebook examples, and studies the Facebook tutorials, and I don't understand Facebook integration. I understand how to register the app with Facebook, but what I don't understand is the code to post text and a picture to a user's news feed. I have tried to understand Facebook's tutorials, but they are all based on Xcode and iOS versions which are not current. Facebook's tutorials are also not consistent (e.g., the login tutorial does not match variables with the posting tutorial, etc.). I do understand how to add text to "initialText", but it's against Facebook policy to provide default text in this variable. Can anyone explain how to publish text and a picture to a user's news feed in Xcode?
Thanx!
Have you tried this one?
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(#"ResultCancelled");
} else
{
NSLog(#"ResultSuccess");
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler =myBlock;
[controller setInitialText:#"Learn iOS6 Social Framework integration"];
[controller addURL:[NSURL URLWithString:#"http://google.com"]];
[controller addImage:[UIImage imageNamed:#"myimage.jpeg"]];
[self presentViewController:controller animated:YES completion:Nil];
}
else{
//NSLog(#"UnAvailable");
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Facebook"
message:[NSStringstringWithFormat:#"The application cannot post message at the moment. Please login under setting section with your Facebook account."]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
import social framework in your project.
- (void)ShareOnFB
{
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(#"Cancelled");
} else
{
NSLog(#"Done");
//Adding the Text to the facebook post value from iOS
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler =myBlock;
NSString *fb=[NSString stringWithFormat:#"you can add your text hereand change it as and when needed"];
[controller setInitialText:fb];
//Adding the Image to the facebook post value from iOS
[controller addImage:yourImageView.image];
[self presentViewController:controller animated:YES completion:Nil];
}

SLCOmposeviewcontroller twitter sharing alert issue

I have been using this code for twitter posting .
SLComposeViewController *fbController=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){
[fbController dismissViewControllerAnimated:YES completion:nil];
switch(result){
case SLComposeViewControllerResultCancelled:
default:
{
NSLog(#"Cancelled.....");
}
break;
case SLComposeViewControllerResultDone:
{
NSLog(#"Posted....");
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Success"
message:#"Posted Successfully"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
break;
}};
[fbController addImage:[UIImage imageNamed:#"1.jpg"]];
[fbController setInitialText:#"Check out this article."];
//[fbController addURL:[NSURL URLWithString:#"http://soulwithmobiletechnology.blogspot.com/"]];
[fbController setCompletionHandler:completionHandler];
[self presentViewController:fbController animated:YES completion:nil];
}
If the user doesn't setup the twitter account it is displaying the alert which is having the setting and cancel buttons . But it is not displaying the alert in the Device? Could any one help me please . Thanks in advance .
The same thing happens with Facebook sharing. Remove the test:
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
If the user's Twitter account isn't already set up in iOS 6, the above test will evaluate to false and none of the sharing code will execute.
Instead, if you skip the check and try to present the SLComposeViewController anyway, iOS will prompt the user to set up their Twitter account.
BOOL canCompose= [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter];
if (canCompose) {
SLComposeViewController *fbCompose=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[fbCompose setInitialText:addressLabel.text];
[self presentViewController:fbCompose animated:YES completion:nil];
}
once try this code instead of using completionHandler
Follow this tutorial. here open the alert.
http://www.appcoda.com/ios-programming-101-integrate-twitter-and-facebook-sharing-in-ios-6/
- (IBAction)postToTwitter:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:#"Great fun to learn iOS programming at appcoda.com!"];
[self presentViewController:tweetSheet animated:YES completion:nil];
}}

Multi touch is not working after facebook share

In my project i am having 5 levels. In First and second level i can able to move and shoot at a time. When completing 2nd level i want to share it with Facebook. After facebook share i cant able to move and shoot at a time. Only one process is working(Either shoot or move).
What i want to do for solve this problem.
My coding is here:
{
UIViewController*v=[[UIViewController alloc]init];
[[[CCDirector sharedDirector]openGLView]addSubview:v.view];
SLComposeViewController*mySLComposerSheet;
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
NSString *strg=[NSString stringWithFormat:#" "];
NSString *bodyStr=[NSString stringWithFormat:#"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"!\n;
mySLComposerSheet = [[SLComposeViewController alloc] init];
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; //Tell him with what social plattform to use it, e.g. facebook or twitter
[mySLComposerSheet setInitialText:bodyStr]; //the message you want to post
[mySLComposerSheet addImage:[UIImage imageNamed:#"my.jpg"]]; //an image you could post
NSURL *url = [NSURL URLWithString:#"https:example.com"];
[mySLComposerSheet addURL:url];
[v presentViewController:mySLComposerSheet animated:YES completion:nil];
}
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output,*head;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"FREE levels NOT unlocked";
head=#"Facebook Share Unsuccessfull";
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:head message:output delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert1 show];
[self reload];
break;
case SLComposeViewControllerResultDone:
output = #"Extra FREE levels successfully unlocked";
head=#"Successfull";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:head message:output delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
alert.tag=22;
break;
default:
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Facebook" message:output delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}];
}
You are presenting the ViewController mySLComposerSheet
[v presentViewController:mySLComposerSheet animated:YES completion:nil];
but there is no dismiss... statement in your completionHandler
[self dismissViewControllerAnimated:YES completion:nil];
see the post https://stackoverflow.com/a/12651085/2787026

IOS twitter app to add image

I'm creating an application that has integration with Twitter .. and wanted to know what have to do to create a button to add an image of the device in the tweet.and also wanted to know which framework to use it and also where the code
You can just use the twitter framework, it's included since iOS 5
And the code i use:
- (IBAction)twitter:(id)sender {
if([TWTweetComposeViewController canSendTweet])
{
NSLog(#"Ready to Tweet.");
TWTweetComposeViewController *tweetComposer = [[TWTweetComposeViewController alloc] init];
[tweetComposer setInitialText:[NSString stringWithFormat:#"My message"]];
[tweetComposer addImage:[UIImage imageNamed:#"114x114"]];
[tweetComposer addURL:[NSURL URLWithString:#"http://myPage"]];
tweetComposer.completionHandler = ^(TWTweetComposeViewControllerResult result){
if(result == TWTweetComposeViewControllerResultDone){
NSLog(#"Tweeted.");
} else if(result == TWTweetComposeViewControllerResultCancelled) {
NSLog(#"Cancelled.");
}
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
};
[self presentModalViewController:tweetComposer animated:YES];
}else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Oops"
message:#"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}

Resources