How to solve login issue while sharing post on Facebook - iphone

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

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

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

Crash On MFMailComposeViewController For iPad

- (void) mailshareClick:(UIButton *)sender
{
NSString *_message = #"wait for set up Mail";
[self waitForWhile:_message];
if ([MFMailComposeViewController canSendMail]){
[NSThread detachNewThreadSelector:#selector(mailFunction) toTarget:self withObject:nil];
}
else {
_message = #"Please set Mail account";
[self remove:_message];
}
}
- (void) mailFunction
{
NSData *data = nil;
if ([self.files.imageArr count]>0)
{
XXImage *single = [self.files.imageArr objectAtIndex:0];
UIImage *image = [[SDImageCache sharedImageCache] imageFromKey:[[single.imagearray objectAtIndex:0] columnImage]];
data = [UIImageJPEGRepresentation(image, 1.0f) retain];
}
[self performSelectorOnMainThread:#selector(mailFinished:) withObject:data waitUntilDone:YES];
[data release];
}
- (void) mailFinished:(NSData *)_data
{
if ([MFMailComposeViewController canSendMail]){
NSData *data = [_data retain];
MFMailComposeViewController *message = [[MFMailComposeViewController alloc] init];
//Title
[message setSubject:self.files.title];
//Body
[message setMessageBody:#"111" isHTML:YES];
[message setToRecipients:[NSArray arrayWithObject:#"mail"]];
//Content
if (data != nil) {
NSString *picStr = [[NSString alloc] initWithFormat:#"%#%#",OutsideWebsite_Normal,self.files.middlePicPath];
[message addAttachmentData: data mimeType: #"" fileName:picStr];
[picStr release];
[data release];
}
message.mailComposeDelegate = self;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
message.modalPresentationStyle = UIModalPresentationFormSheet;
}
[self presentModalViewController:message animated:YES];
[self remove:#"Set up Ok"];
[message release];
}
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result) {
case MFMailComposeResultSent:
{
NSLog(#"MFMailComposeResultSent");
break;
}
case MFMailComposeResultSaved:
{
NSLog(#"MFMailComposeResultSaved");
break;
}
case MFMailComposeResultFailed:
{
NSLog(#"MFMailComposeResultFailed");
break;
}
case MFMailComposeResultCancelled:
{
NSLog(#"MFMailComposeResultCancelled");
break;
}
default:
break;
}
[self performSelector:#selector(delayDismissModalView) withObject:nil afterDelay:1];
}
-(void)delayDismissModalView
{
[self dismissModalViewControllerAnimated:YES];
}
After invoke a few times the email method , there will be
[MFSearchResultsViewController hash]: message sent to deallocated instance
Or
[MFMailComposeViewController hash]: message sent to deallocated instance
crash.
As you think of that, what is the MFSearchResultsViewController function.
Whether it can solve the problem,please give me a hand.
You have to setDelegate for the mail controller. If you set the delegate to "self", make sure that the "self" has <MFMailComposeViewControllerDelegate>. If all these can't make it work, make sure you didn't call presentModalViewController twice.

How to send mail from iphone app?

I am try to send mail from my app. I want to type(dynamically) recipient id,ccid,sub and message.
Thanks
Senthilkumar
on iOS 3+
Import #import in your view controller header.
Then:
-(void)showMailPanel {
MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
// only on iOS < 3
//if ([MFMailComposeViewController canSendMail] == NO)
// [self launchMailApp]; // you need to
mailComposeViewController.mailComposeDelegate = self;
[mailComposeViewController setToRecipients:[NSArray arrayWithObjects:#"email address1",#"email address 2",nil]];
[mailComposeViewController setSubject:#"your subject"];
[mailComposeViewController setMessageBody:#"your body" isHTML:YES];
mailComposeViewController.delegate = self;
[self.navigationController presentModalViewController:mailComposeViewController animated:YES];
[mailComposeViewController release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Result: canceled");
break;
case MFMailComposeResultSaved:
NSLog(#"Result: saved");
break;
case MFMailComposeResultSent:
NSLog(#"Result: sent");
break;
case MFMailComposeResultFailed:
NSLog(#"Result: failed");
break;
default:
NSLog(#"Result: not sent");
break;
}
[controller dismissModalViewControllerAnimated:YES];
}
-(void)launchMailApp {
{
NSString *recipients = #"dest_email";
NSString *email = [NSString stringWithFormat:#"%#%#", recipients, subject];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
You need things like this:
//Import this in your .h file
#import <MessageUI/MessageUI.h>
...
MFMailComposeViewController *mailController = [[[MFMailComposeViewController alloc] init] autorelease];
mailController.mailComposeDelegate = self;
[mailController setSubject:[NSString stringWithFormat:#"Report a problem - #%#", yourUserID]];
[mailController setToRecipients:[NSArray arrayWithObject:#"support#yourWebsite.com"]];
[self presentModalViewController:mailController animated:YES];
Besides, the parent view controller (the delegate) needs to conform to the MFMailComposeViewControllerDelegate protocol:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
//Extra implementation here...
[self dismissModalViewControllerAnimated:YES];
}

MFMailComposeViewController : exit on cancel

i have the following code presenting the user with email from within the app :
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Subject"];
NSString* link = [NSString stringWithFormat:#"<a href='%#'>Report</a>",link];
[picker setMessageBody:link isHTML:YES];
picker.navigationBar.barStyle = UIBarStyleDefault;
[self presentModalViewController:picker animated:YES];
[picker release];
and allso :
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{
[self dismissModalViewControllerAnimated:YES];
}
everything works just fine , expect that i'd like the cancel to skip the delete draft/save draft/cancel stage and basicly chose the delete draft (not present it to the user)
can this be done ?
thanks
- (void)mailComposeController:(MFMailComposeViewController *) controller didFinishWithResult:(MFMailComposeResult) result error:(NSError *)error
{
switch (result) {
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
break;
case MFMailComposeResultSent:
break;
case MFMailComposeResultFailed:
break;
default:
break;
}
//Dismiss the mailViewController.
[self dismissModalViewControllerAnimated:YES];
}
Is this what you are looking for? You can then handle each error or completion individually!