use FacebookComposeViewController ,but photo can't show - iphone

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";
}

Related

Unable to post image into facebook using SLComposeViewController?

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];
});

How to remove statusbar from messageviewcontroller?

I am sending sms programmatically from my view controller but now it is showing me statusbar and vertical black line
my code:
- (IBAction)SendTextBtnTapped:(id)sender {
[self sendSMS:#"Body of SMS..." recipientList:[NSArray arrayWithObjects: nil]];
}
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = bodyOfMessage;
controller.recipients = recipients;
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissModalViewControllerAnimated:YES];
if (result == MessageComposeResultCancelled)
NSLog(#"Message cancelled");
else if (result == MessageComposeResultSent)
NSLog(#"Message sent");
else
NSLog(#"Message failed");
}
just add this line
controller.wantsFullScreenLayout = YES;
OR add this line after present MessageViewController like bellow..
[self presentModalViewController:controller animated:YES];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
OR for whole this viewController use bellow loginin viewWillAppear: paste this code..
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
I think you can use wantsFullScreenLayout.
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = bodyOfMessage;
controller.recipients = recipients;
controller.messageComposeDelegate = self;
controller.wantsFullScreenLayout = YES; //<== add this
[self presentModalViewController:controller animated:YES];
}

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.");
}
}

game center unavailable player is not signed in

On iOS 6 when a player is not signed in and is trying to use GameCenter an UIAlertView with the text that i put in title pops up. "game center unavailable player is not signed in". My question is is it possible to replace that UIAlertView with anything else, with my own interface element?
[GKLocalPlayer localPlayer].authenticateHandler = ^(UIViewController *viewController, NSError *error)
{
if ([[GKLocalPlayer localPlayer] isAuthenticated])
{
NSLog(#"[gamecenter] player authenticated: %#\n", [GKLocalPlayer localPlayer]);
[self gamecenterLoadAchievements];
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite)
{
// Insert application-specific code here to clean up any games in progress.
if (acceptedInvite)
{
NSLog(#"Accepted invitation");
isInvited = YES;
[[GameLevel sharedGameLevel] setCurrentGameMode:GameModeGameCenter];
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite] autorelease];
mmvc.matchmakerDelegate = self;
AppDelegate * delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
[delegate.viewController presentModalViewController:mmvc animated:YES];
}
else if (playersToInvite)
{
NSLog(#"Players to invite");
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = 2;
request.maxPlayers = 2;
request.playersToInvite = playersToInvite;
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
mmvc.matchmakerDelegate = self;
[presentingViewController presentModalViewController:mmvc animated:YES];
}
};
}
else
if (viewController)
{
AppDelegate *delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
NavigationController *_viewController = delegate.viewController.navController;
[_viewController presentViewController: viewController animated: YES completion:nil];
}
else
{
NSLog(#"[gamecenter] %#\n", error);
gcLoginFailed = YES;
if ([[error domain] isEqualToString:GKErrorDomain])
{
if ([error code] == GKErrorNotSupported)
gcIsSupported = NO;
else
if ([error code] == GKErrorCancelled)
gcLoginCancelled = YES;
}
}
};
Just not use UIAlertView Use your own graphics with a UIButton
If you are using the disableGameCenter method in authenticating , just don't use it , instead of it use a integer ilk int isPlayerSignedIn . And just return it to 1 or 0 . So ;
if (isPlayerSignedIn==0) {
//display your graphics
}
else do nothing .
edit :
else
if (viewController)
{
AppDelegate *delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
NavigationController *_viewController = delegate.viewController.navController;
[_viewController presentViewController: viewController animated: YES completion:nil];
}
just if you don't use these lines of code you will see that you don't get that UIAlertView
Then you can implement a "game center is unavailable" staff , but additionally you have to implement a login screen too.

MFMessageComposeViewController dismiss keyboard

i'm having troubles with my MFMessageComposeViewController. I would like to use SMS in-app.
Everything work fine for sending SMS, so far so good. But when i hit the cancel button (or send button too) top of my view disapeared but the keyboard did not. It's maybe because i don't use modale view, but only a addSubview.
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
if (result == MessageComposeResultCancelled)
{
NSLog(#"Message annulé");
[controller resignFirstResponder];
[controller.view removeFromSuperview];
[controller release];
}
else if (result == MessageComposeResultSent)
{
NSLog(#"Message envoyé");
...
}
else
{
NSLog(#"Message non envoyé");
...
}
}
-(void)sendSMS:(NSString *)bodyOfMessage :(Phone *)recipient
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;
NSMutableArray *toRecipients = [[NSMutableArray alloc]init];
[toRecipients addObject:recipients.phoneNumber];
[picker setRecipients:(NSArray *)toRecipients];
[toRecipients release];
NSString *bodyString = nil;
bodyString = bodyOfMessage;
[picker setBody:bodyString];
[self addSubView:picker.view];
[picker release];
}
}
Any idea ? Had I to use only modalView ?
sorry for spelling mistake...
Thank you. Tommy
Yes, you have to use the modalviewcontroller.
[self presentModalViewController:picker];
Also, you're creating two instances of the MFMessageComposeViewController, first for checking if it can send text and then another to actually show it. I advise to create just one, it's better for the memory :) also the first one is leaking since you didn't release it. Good luck!
if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;
NSString *bodyString = nil;
NSMutableArray *toRecipients = [[NSMutableArray alloc]init];
[toRecipients addObject:#"phone here"];
[picker setRecipients:(NSArray *)toRecipients];
[toRecipients release];
bodyString = [NSString stringWithFormat: #"Message body"];
[picker setBody:bodyString];
[self presentModalViewController:picker animated:YES];
[picker release];
}
Try close existing keyboard, before presenting modal view controller with MFMessageComposeViewController:
[self.view endEditing:YES]; //close keyboard if it opened
[self presentModalViewController:messageController animated:YES];