Unable to post image into facebook using SLComposeViewController? - iphone

I would like to post image into facebook and twitter. I am fine with twitter but not with facebook using SLComposeViewController class. With out add image i am able to post text and url into facebook. The problem is when i use add image i was unable to post this image and also text, url. SLComposeViewController shows image, text and url when i send. I have correct appId and i did not get any errors. But the problem is still there. I don't where the problem is. Please help me.
- (void)performFBRequestUploadForImage{
[self showListOfFaceBookAccountsFromStore];
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"ACtionCancelled";
break;
case SLComposeViewControllerResultDone:
output = #"Post Successfull";
[self dismissViewControllerAnimated:YES completion:nil];
break;
default:
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Face Book Message" message:output delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
};
[mySLComposerSheet addImage:[UIImage imageNamed:#"images4.jpg"]];
[mySLComposerSheet setInitialText:#"I am developer."];
[mySLComposerSheet addURL:[NSURL URLWithString:#"http://stackoverflow.com/"]];
[mySLComposerSheet setCompletionHandler:completionHandler];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
}
- (void)showListOfFaceBookAccountsFromStore
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
if( [SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook] )
{
NSDictionary *options = #{
#"ACFacebookAppIdKey" : myAppId,
#"ACFacebookPermissionsKey" : #[#"publish_stream"],
#"ACFacebookAudienceKey" : ACFacebookAudienceFriends};
[accountStore requestAccessToAccountsWithType:accountType options:options completion:^(BOOL granted, NSError *error){
if(granted) {
ACAccount * account = [[accountStore accountsWithAccountType:accountType] lastObject];
NSLog(#"Facebook user: %#",[account username]);
if([account username]==NULL){
[self facebookAlert];
} else {
}
}
else{
NSLog(#"Read permission error: %#", [error localizedDescription]);
}
}];
} else {
[self facebookAlert];
}
}

I used below code in my projects to post the image it works fine.
if([SLComposeViewController instanceMethodForSelector:#selector(isAvailableForServiceType)] != nil)
{
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:#"Check out my Christmas Gift!"];
[controller addImage:#"gift.jpg"];
[self presentViewController:controller animated:YES completion:Nil];
}
You just try follow below tutorials
Tutorial 1
2.Tutorial 2

I checked the internet and noticed a few discussion in the recent 1 or 2 days related to this issue and they seem to related to Facebook bug. So if your code post messages and images successfully to Twitter, then don't worry! you are doing correctly.
https://stackoverflow.com/questions/14868518/sharekit-not-sharing-link-to-facebook/
https://discussions.apple.com/thread/4805777

I have resolved through open present-view controller in navigation-bar.it may help you.
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:shareFrom];
[controller addImage:self.photoImageView.image];
[controller addURL:[NSURL URLWithString:dayCareWebsite]];
dispatch_async(dispatch_get_main_queue(), ^ {
[self.navigationController presentViewController:controller animated:YES completion:nil];
});

Related

Posting an image from a file instead to Facebook?

How would I go about posting an image from a file instead to Facebook? This works fine:
SLComposeViewController *controllerSLC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controllerSLC setInitialText:#" iPhone app"];
[controllerSLC addURL:[NSURL URLWithString:#"http://www.example.com"]];
[controllerSLC addImage:[UIImage imageNamed:#"icon.png"]];
However I'm trying to send an image from:
self.imageView.image = [self.photo objectForKey:photoPictureKey];
Try this mate (iOS6),
send your image to this method,
Make Sure you add Social.framework and #import <Social/Social.h>
-(void) uploadToFaceBook:(UIImage *) image{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]){
SLComposeViewController *fbController =[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler __block completionHandler=
^(SLComposeViewControllerResult result){
[fbController dismissViewControllerAnimated:YES completion:nil];
switch(result){
case SLComposeViewControllerResultCancelled:
default:
{
NSLog(#"Cancelled.....");
}
break;
case SLComposeViewControllerResultDone:
{
NSLog(#"Posted....");
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Sent" message:nil delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles: nil];
[alert show];
}
break;
}};
[fbController addImage:image];
[fbController setInitialText:#" iPhone App"];
[fbController addURL:[NSURL URLWithString:#"http://www.example.com"]];
[fbController setCompletionHandler:completionHandler];
[self presentViewController:fbController animated:YES completion:nil];
}
else{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry"
message:#"You can't post on Facebook right now, make sure your device has an internet connection and you have at least one Facebook account setup"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
if this [self.photo objectForKey:photoPictureKey] code returns the path of the image you can use UIImage class method that is imageWithContentsOfFile:
UIImage *fbImage = [UIImage imageWithContentsOfFile:[self.photo objectForKey:photoPictureKey]];
Then check fbImage is it is not nil before adding it to the instance of the class SLComposeViewController
if(fbImage!=nil){
[controllerSLC addImage: image];
}else{
NSLog('image is nil');
}
if this "[self.photo objectForKey:photoPictureKey]" code returns the a UIImage instance then do this:
UIImage *fbImage = [self.photo objectForKey:photoPictureKey];
if(fbImage!=nil){
[controllerSLC addImage: fbImage];
}else{
NSLog('image is nil');
}
you can also check if fbImage is null by:
if(fbImage!=nil && !([fbImage isKindOfClass:[NSNull class]])){
[controllerSLC addImage: fbImage];
}

use FacebookComposeViewController ,but photo can't show

I learned this Sample code, and try to implement in my app, but the photo can't show.
enter link description here
- (IBAction)facebookBtn_down:(id)sender {
if (osVer >= 6.0) {
SLComposeViewController *fbVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[fbVC setInitialText:apple_F_MSG];
[fbVC addURL:[NSURL URLWithString:APP_URL]];
[fbVC setCompletionHandler:^(SLComposeViewControllerResult res) {
if (res == SLComposeViewControllerResultDone) [self AlertToukou];
}];
[self presentModalViewController:fbVC animated:YES];
} else {
DEFacebookComposeViewController *fbVC = [[[DEFacebookComposeViewController alloc] init] autorelease];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[fbVC setInitialText:apple_F_MSG];
[fbVC addURL:[NSURL URLWithString:APP_URL]];
[fbVC setCompletionHandler:^(DEFacebookComposeViewControllerResult res) {
if (res == DEFacebookComposeViewControllerResultDone) [self AlertToukou];
[self dismissModalViewControllerAnimated:YES];
self.modalPresentationStyle = UIModalPresentationFullScreen;
}];
[self presentModalViewController:fbVC animated:YES];
}
}
please give me some advice,thank you!
From the documentation page of that it says,
DEFacebookComposeViewController *facebookViewComposer = [[DEFacebookComposeViewController alloc] init];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[facebookViewComposer setInitialText:#"Look on this"];
[facebookViewComposer addImage:[UIImage imageNamed:#"1.jpg"]];//<------Check This
facebookViewComposer.completionHandler = completionHandler;
[self presentViewController:facebookViewComposer animated:YES completion:^{ }];
You should use addImage function and give proper UIImage object than it should be working.
I found the problem:
I did not set the Key "forKey:#"source",than can't post on the wall
errorError Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)"
if ([self.images count] > 0) {
[d setObject:UIImagePNGRepresentation([self.images lastObject]) forKey:#""];
graphPath = #"me/photos";
}

How to solve login issue while sharing post on Facebook

i want to share a post on facebook it works fine when i use iOS 6 but in iOS 5 and iOS 5.1 it gives an issue of login, when i login and goes to already authorised page and press okay button it pulls me back to login like this and repeats again and again on these two pages
when i press okay it gives back
case 1: // facebook
{
if(NSClassFromString(#"SLComposeViewController"))
{
SLComposeViewController *fbController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){
[fbController dismissViewControllerAnimated:YES completion:nil];
switch(result){
case SLComposeViewControllerResultCancelled:
default:
{
NSLog(#"Cancelled.....");
}
break;
case SLComposeViewControllerResultDone:
{
ALERT_VIEW(#"Successfully posted to facebook.");
}
break;
}};
[fbController setInitialText:quotesss];
[fbController setCompletionHandler:completionHandler];
[self presentViewController:fbController animated:YES completion:nil];
}
else
{
DEFacebookComposeViewControllerCompletionHandler completionHandler = ^(DEFacebookComposeViewControllerResult result) {
switch (result) {
case DEFacebookComposeViewControllerResultCancelled:
NSLog(#"Facebook Result: Cancelled");
break;
case DEFacebookComposeViewControllerResultDone:
ALERT_VIEW(#"Successfully posted to facebook.");
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
};
DEFacebookComposeViewController *facebookViewComposer = [[DEFacebookComposeViewController alloc] init];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[facebookViewComposer setInitialText:quotesss];
facebookViewComposer.completionHandler = completionHandler;
[self presentViewController:facebookViewComposer animated:YES completion:nil];
[facebookViewComposer release];
}
break;
}
case 2: // twitter
{
NSString *text = [NSString stringWithFormat:#"%#", quotesss ];
if([text length] > 135)
{
text = [NSString stringWithFormat:#"%#%#", quotesss ,#"..."];
text = [text substringToIndex:135];
}
if(NSClassFromString(#"SLComposeViewController"))
{
SLComposeViewController *twitterController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){
[twitterController dismissViewControllerAnimated:YES completion:nil];
switch(result){
case SLComposeViewControllerResultCancelled:
default:
{
NSLog(#"Cancelled.....");
}
break;
case SLComposeViewControllerResultDone:
{
ALERT_VIEW(#"Successfully posted to twitter.");
}
break;
}};
[twitterController setInitialText:text];
[twitterController setCompletionHandler:completionHandler];
[self presentViewController:twitterController animated:YES completion:nil];
}
else
{
DETweetComposeViewControllerCompletionHandler completionHandler = ^(DETweetComposeViewControllerResult result) {
switch (result) {
case DETweetComposeViewControllerResultCancelled:
NSLog(#"Twitter Result: Cancelled");
break;
case DETweetComposeViewControllerResultDone:
ALERT_VIEW(#"Successfully posted to twitter.");
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
};
DETweetComposeViewController *tcvc = [[[DETweetComposeViewController alloc] init] autorelease];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[tcvc setInitialText:quotesss];
tcvc.completionHandler = completionHandler;
[self presentViewController:tcvc animated:YES completion:nil];
}
break;
}
From your code i found that the problem is in this code coz in iOS 5 Facebook is not available in SLCompose only Twitter is available so your FaceBook is handling by DEFaceBook
DEFacebookComposeViewControllerCompletionHandler completionHandler = ^(DEFacebookComposeViewControllerResult result) {
switch (result) {
case DEFacebookComposeViewControllerResultCancelled:
NSLog(#"Facebook Result: Cancelled");
break;
case DEFacebookComposeViewControllerResultDone:
ALERT_VIEW(#"Successfully posted to facebook.");
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
};
DEFacebookComposeViewController *facebookViewComposer = [[DEFacebookComposeViewController alloc] init];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[facebookViewComposer setInitialText:quotesss];
facebookViewComposer.completionHandler = completionHandler;
[self presentViewController:facebookViewComposer animated:YES completion:nil];
[facebookViewComposer release];
}
So go to your appdelegate and check this method that a valid token(url which contains your application, token format is applicationURLSchema://"token") check it is coming or not in this method
If necessary keep break point and go through the FBSession class then handleOpenURL method and find where error is happening
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// attempt to extract a token from the url
return [FBSession.activeSession handleOpenURL:url];
}

Present Social View Controller Cocos2d

Im trying to use the social framework to present the "Post To Facebook" view controller from within Cocos2d. This is the code I would usually use in a storyboard app
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *facebook = [[SLComposeViewController alloc] init];
facebook = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[facebook setInitialText:[NSString stringWithFormat:#"text"]];
[self presentViewController:facebook animated:YES completion:nil];
[facebook setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
output = #"Posted";
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setInteger:1 forKey:#"Shared"];
[ud synchronize];
default:
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Facebook" message:output delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[alert show];
}];
}
How would I get this up and running from within Cocos2d? currently it throws up a warning for the line
[self presentViewController:facebook animated:YES completion:nil];
Thanks in advance
In cocos2d 2.0 you can use [CCDirector sharedDirector] instead of self.
[[CCDirector sharedDirector] presentViewController:facebook animated:YES completion:nil];
This works because CCDirector inherits from UIViewController.
This works for me....
-(void) facebookWithInitialText:(NSString*) text {
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
CCLOG( #"can post to Facebook");
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:text]; // set initial text
[controller addImage:[UIImage imageNamed:#"Icon-72.png"]]; //add an image
[controller addURL:[NSURL URLWithString:#"http://www.cartoonsmart.com"]]; //add a URL to it
[[app navController] presentViewController:controller animated:YES completion:nil ];
[controller setCompletionHandler:^(SLComposeViewControllerResult result){
[[app navController] dismissModalViewControllerAnimated:YES];
NSString *outout = [[NSString alloc] init];
switch (result) {
case SLComposeViewControllerResultCancelled:
outout = #"Post Cancled";
break;
case SLComposeViewControllerResultDone:
outout = #"Post Done";
default:
break;
}
UIAlertView *myalertView = [[UIAlertView alloc]initWithTitle:#"Facebook" message:outout delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[myalertView show];
}];
} else {
CCLOG( #"Facebook not accessible or one account not setup.");
}
}

iOS 6.0 Facebook Connectivity

I have to get connected to the facebook on button click in iOS 6.0. I have added the frameworks social and accounts to my project. I am able to check in to a place but, not able to tag friends for what I am posting to the facebook. How to fetch facebook friends list?
The code I have used is shown below :
- (IBAction)connectToFacebook:(id)sender
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *options = #{
#"ACFacebookAppIDKey": #"412590558803147",
#"ACFacebookAppVersionKey": #"1.0",
#"ACFacebookPermissionsKey": #"publish_stream",
#"ACFacebookPermissionGroupKey": #"write"
};
NSLog(#"options is %#",options);
[accountStore requestAccessToAccountsWithType:accountType options:options
completion:^(BOOL granted, NSError *error) {
if (granted)
{
NSArray *accounts = [accountStore
accountsWithAccountType:accountType];
NSString *facebookAccount = [accounts lastObject];
NSLog(#"facebook account %#", facebookAccount);
} else {
NSLog(#"%#",error);
// Fail gracefully...
}
}];
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(#"Cancelled");
} else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Posted!!!" message:#"your status is posted to facebook successfully" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler =myBlock;
[controller setInitialText:#"This is a ios 6.0 facebook intergration application"];
[controller addImage:[UIImage imageNamed:#"spalshimage.jpeg"]];
[self presentViewController:controller animated:YES completion:Nil];
}
else{
NSLog(#"UnAvailable");
}
}
The best way to integrate Facebook is to use the new iOS 6 Facebook SDK:
https://developer.apple.com/technologies/ios6/
Try this link for friend list:
https://developers.facebook.com/docs/tutorials/ios-sdk-tutorial/show-friends/