I am using the following code to post something on Facebook.
- (IBAction)post:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
mySocialComposer = [[SLComposeViewController alloc]init];
mySocialComposer = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeFacebook];
[mySocialComposer setInitialText:#"Hellooooooo World"];
[mySocialComposer addImage:[UIImage imageNamed:#"image.jpg"]];
[self presentViewController:mySocialComposer animated:YES completion:nil];
[mySocialComposer setCompletionHandler:^(SLComposeViewControllerResult result){
NSString *outout = [[NSString alloc] init];
switch (result) {
case SLComposeViewControllerResultCancelled:
outout = #"Post Cancled";
break;
case SLComposeViewControllerResultDone:
outout = #"Post Successfull";
default:
break;
}
UIAlertView *myalertView = [[UIAlertView alloc]initWithTitle:#"FaceBook"
message:outout delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[myalertView show];
}];
}
}
I am using the above method to post on Facebook from iOS app.If I do post including an Image and some text it successfully post ed on Facebook. But when I try to post on Facebook without the image just text for that purpose i comment out the following line.
[mySocialComposer addImage:[UIImage imageNamed:#"image.jpg"]];
And while doing that i got an error alert that This post cannot be sent because connection to Facebook lost. Is there any way that i can post on Facebook without image . Thanks in advance.
You can just set the add image to nil. You should be able to post fine.
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController* mySocialComposer = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeFacebook];
[mySocialComposer setInitialText:#"Hellooooooo World"];
[mySocialComposer addImage:nil];
[self presentViewController:mySocialComposer animated:YES completion:nil];
[mySocialComposer setCompletionHandler:^(SLComposeViewControllerResult result){
NSString *outout = [[NSString alloc] init];
switch (result) {
case SLComposeViewControllerResultCancelled:
outout = #"Post Canceled";
break;
case SLComposeViewControllerResultDone:
outout = #"Post Successfull";
default:
break;
}
As per Apple's official documentation on SLComposeViewController you will see that it says
Return Value
Returns a Boolean value indicating whether the text was successfully set.
Discussion
This method returns NO if text does not fit in the currently available character space or if the view controller has already been presented to the user (and therefore cannot be changed). Character limits are dependent on the target service and are documented by the service provider. For links to documentation for the supported services, see “Table 1Social Services Individual Documentation Sites” in SLRequest Class Reference.
So if this controller has already been shown it is not going to work. Try a different text and see again.
Can you remove
mySocialComposer = [[SLComposeViewController alloc]init];
You are not supposed to do that.
[mySocialComposer setInitialText:#"Hellooooooo World"];
[mySocialComposer addImage:[UIImage imageNamed:#"image.jpg"]];
[self presentViewController:mySocialComposer animated:YES completion:nil];
Put these lines of code after you set your completion handler.
Also remove the UIAlertView from there and put it inside the completion handler for error case.
Related
I need to login to my twitter from my iOS app. I am able to check if there are any accounts that are set up in the settings in the device. Once it is confirmed that there are no accounts configured in the device, i want to able to login to my twitter account and then add the account to my device. I had a little luck in implementing checking if there are any accounts set up in the device using ACAccountStore. But can someone please help in implementing the later part in iOS7?
SLComposeViewController *sheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
SLComposeViewControllerCompletionHandler completionBlock = ^(SLComposeViewControllerResult result){
[sheet dismissViewControllerAnimated:YES completion:Nil];
};
sheet.completionHandler = completionBlock;
sheet.view.hidden = YES;
[_mainViewController presentViewController:sheet animated:YES completion:nil];
- (IBAction)tweetTapped:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:#"Your Tweet Message! :)"];
if (self.imageString)
{
[tweetSheet addImage:[UIImage imageNamed:self.imageString]];
}
if (self.urlString)
{
[tweetSheet addURL:[NSURL URLWithString:self.urlString]];
}
[self presentViewController:tweetSheet animated:YES completion:nil];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Sorry"
message:#"You can't tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
I want to use shareKit app in my project but when i m trying to post image on facebook i am getting this error .
Error: This endpoint has been deprecated. To temporarily enable it, you may disable the "august 2012" platform migration. It will be disabled permanently on August 1, 2012.
i am using latest SDK or ShareKit.
You dont need to use ShareKit to implement Facebook Integration. Use the new iOS 6 SLComposeViewController to post to Facebook
Here is the full code to post to Facebook
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) //check if Facebook Account is linked
{
mySLComposerSheet = [[SLComposeViewController alloc] init];
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[mySLComposerSheet setInitialText:[NSString stringWithFormat:#"Test",mySLComposerSheet.serviceType]];
[mySLComposerSheet addImage:yourimage]; //an image you could post
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
output = #"Post Successfull";
break;
default:
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Facebook" message:output delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}];
You're not using the latest version. The latest version is: ShareKit 2.0.
If you use Facebook, Twitter or SinaWeibo with iOS6 only it is really more straightforward to use social.network directly.
I am very new to iOS development.I am making an application in which i required to integrate twitter and Facebook for wall posting.
I have done all the required coding for this and on simulator it is working fine but not on device.
one more question as the coding for Facebook integration i have copied it from other "demo" application.So what else we need to change in it to make it for my own application.Because when i see my update done by my app on Facebook wall, "demo" app name comes there with the post.
Please guide me !!Thanking you in Advance!!
Facebook
It seems you have jumped to coding part before reading the documentation. Before integrating facebook sdk and writing your code, you need create a new app section in facebook developer console, get an Facebook app id. You need to use that app id in your project, not the app id shipped with facebook demo application.
Documentation explains the process fully, no need to rewrite it here. Make sure you read it to the end.
Twitter
I am not sure if you are having problem in twitter also (question is unclear on that). If yes, you should tell how you are connecting to twitter. But generally, from the tone of your questions, it seems you haven't read the documentation, on creating an app section in respective developer console, and getting app id.
I don't know how much integration you need or if your willing to require iOS 6 but in iOS 6 it is SO MUCH easier to integrate Facebook and Twitter.
This is all the code:
In the header file:
#import "Social/Social.h"
In the main file:
//Twitter
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(#"Cancelled");
} else {
NSLog(#"Done!");
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler = myBlock;
[controller setInitialText:#"Status Text"];
[self presentViewController:controller animated:YES completion:Nil];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry" message:#"You can't send a tweet right now. You must be online and signed into at least one Twitter account." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
//Facebook
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(#"Cancelled");
} else {
NSLog(#"Done!");
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler = myBlock;
[controller setInitialText:#""];
[self presentViewController:controller animated:YES completion:Nil];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Facebook Error" message:#"Either you are not logged into Facebook or your Facebook username and password are incorrect." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
You Can use Share kit framework, http://getsharekit.com
As per integrating twitter in your application.Try this code
at self.fullimage write any url of image.
Call buildTweetSheet method to post to twitter.
import Twitter.framework
import
#property(nonatomic,strong) TWTweetComposeViewController *_tweetSheet;
#synthesize _tweetSheet;
-(void)buildTweetSheet
{
NSLog(#"buildTweetSheet");
_tweetSheet = [[TWTweetComposeViewController alloc] init];
UIImage *eimage=UIImage *eimage=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.fullimage]]];
[_tweetSheet setInitialText:#""];
[_tweetSheet addImage:eimage];
[_tweetSheet setInitialText:#""];
[self presentModalViewController:_tweetSheet animated:YES];
TWTweetComposeViewControllerCompletionHandler completionHandler = ^(TWTweetComposeViewControllerResult result)
{
if(result == TWTweetComposeViewControllerResultDone) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Done" message:#"Image Posted Successfully" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failed" message:#"Image Posted Failed" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
[self dismissModalViewControllerAnimated:YES];
};
[_tweetSheet setCompletionHandler:completionHandler];
}
I am using this code for post the image on the facebook but it also shows image url on the facebook. I want to hide this url:
this is my code for posting an image to facebook
#pragma mark Facebook
-(void)facebook
{
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"I'm using Shape App", #"message", #"I'm using Shape App", #"caption",
#"http://www.imageurlhost.com/images/3kft472rvl2qtnzx11_Sample.png", #"picture",
#"I'm using Shape App", #"title",nil];
[[delegate facebook] dialog:#"feed" andParams:params andDelegate:self];
}
please check the image below
What you are doing is posting a URL, so it shows up as that: A link you shared. It does not matter that this link does point to an image and not a website.
Your options to change that are,
a) post a link to an Open Graph page instead, that contains the image – then you can set a title and description that will show up for that shared link, or
b) post it as a real photo, https://developers.facebook.com/docs/reference/api/user/#photos
i have resolved by latest ios6 facebook integration:
for this you have to import Social.framework after this put this code in method where you want to call Facebook.but it will work only in iOS 6 and later
NSData *imageData=[NSData dataWithContentsOfURL:[NSURL urlWithString:#"http://www.imageurlhost.com/images/3kft472rvl2qtnzx11_Sample.png"]];
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[mySLComposerSheet setInitialText:[NSString stringWithFormat:#"I am using Shape App"]];
[mySLComposerSheet addImage:[UIImage imageWithData:imageData]];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
else
{
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:#"No Facebook Account" message:#"There are no Facebook accounts configured.You can add or create a Facebook account in Settings" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alertView show];
}
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result)
{
// NSLog(#"dfsdf");
switch (result) {
case SLComposeViewControllerResultCancelled:
break;
case SLComposeViewControllerResultDone:
break;
default:
break;
}
}];
I am implementing an application from which I have to share that applications on Facebook, Twitter as well as by mail. As my application is not a game, I just want to put an application icon, application name, iTunes link of that application and a small description of the application. I have implemented code which allow me to send a mail with attachment. Is that useful here? How can I do this?
1. For Facebook.
FBGraph is a much better way to use the Facebook API in your application.
Download the FBGraph API documents folder and then add it to in your folder. Read the instructions on the Facebook developer site.
This is the sample code and let me know if you have any query about it.
2. For EMail
Add MessageUI.framework in your project. Import the header file in your ViewController.h file:
#import <MessageUI/MFMailComposeViewController.h>
Set the delegate:
UIViewController<MFMailComposeViewControllerDelegate>
And after that, open your mail composer like this:
-(void)yourEmailbuttonClick:(id)sender
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Hello!! your subject here"];
// Set up recipients
UIImage *image = [UIImage imageNamed:#"anyImage.png"];
NSData *myData = UIImageJPEGRepresentation(image, 1.0);
[picker addAttachmentData:myData mimeType:#"image/jpg" fileName:#"image"];
[self presentModalViewController:picker animated:YES];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
// message.text = #"Result: canceled";
break;
case MFMailComposeResultSaved:
// message.text = #"Result: saved";
break;
case MFMailComposeResultSent:
// message.text = #"Result: sent";
break;
case MFMailComposeResultFailed:
// message.text = #"Result: failed";
break;
default:
// message.text = #"Result: not sent";
break;
}
[self dismissModalViewControllerAnimated:YES];
}
3. For Twitter
Add Twitter.framework in your project. Import the header file in your ViewController.h file and import:
#import <Twitter/Twitter.h>
Now call the Twitter composer view like this:
-(void)yourTwitterbuttonClick:(id)sender
{
if([TWTweetComposeViewController canSendTweet])
{
UIImage *image = [UIImage imageNamed:#"anyImage.png"];
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
// Set initial text
[tweetViewController setInitialText:#"your text here"];
if (image)
{
[tweetViewController addImage: image];
}
tweetViewController.completionHandler = ^(TWTweetComposeViewControllerResult result)
{
if(result == TWTweetComposeViewControllerResultDone)
{
// The user finished composing a tweet
alert.title=#"Status";
alert.message=#"Tweet sent";
[alert show];
}
else
if(result == TWTweetComposeViewControllerResultCancelled)
{
// The user cancelled composing a tweet
alert.title = #"Status";
alert.message = #"Tweet cancelled";
[alert show];
}
[self dismissViewControllerAnimated:YES completion:nil];
};
[self presentViewController:tweetViewController animated:YES completion:nil];
}
}
Have a look at ShareKit.
This is a list of supported services:
Supported Services:
Delicious
Email
Facebook
Google Reader
Instapaper
Pinboard
Read It Later
Tumblr
Twitter
You can also do this one by one.
Facebook http://developers.facebook.com/docs/reference/iossdk/
Twitter https://developer.apple.com/library/ios/#documentation/Twitter/Reference/TWTweetSheetViewControllerClassRef/Reference/Reference.html
Mail http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html