connect Iphone with Facebook API - iphone

I want to connect facebook API's with my IPhone. For this I have downloaded the FBConnect sdk and included the FBConnect group of FBConnect.xcodeproj in my application and then I have written the following code in FacebookAPPViewController.m
- (void)viewDidLoad{
[super viewDidLoad];
session = [FBSession sessionForApplication:#"a31c3e53bba4a5f2b3955d6e5e 876717" secret:#"6ecbefa3807406bd13187297e58efae9" delegate:self];
FBLoginButton *button = [[[FBLoginButton alloc] init] autorelease];
[self.view addSubview:button];
FBLoginDialog* dialog = [[[FBLoginDialog alloc] init] autorelease];
[dialog show];
}
but it is showing error that session undeclared. That is right also because I have not initialized it but if I declare it with class `FBSession then also It displays some error and if I exclude this line then the button to connect to facebook does not appear.
Can some one help me?
Thanks in advance
Gaurav

After setting up the session, you should pass it to the login dialog:
FBSession *session = [FBSession
sessionForApplication:#"a31c3e53bba4a5f2b3955d6e5e 876717"
secret:#"6ecbefa3807406bd13187297e58efae9" delegate:self];
FBLoginDialog* dialog = [[[FBLoginDialog alloc] initWithSession:session] autorelease];
[dialog show];
In your example, you're opening the login dialog right away, so there's no need to add a login button to your view.
PS. I hope you didn't use your real secret key in your code sample...

Related

iOS: how to post to Facebook with the contents of a label, rather than static text

I have an iOS project, where I have on a .xib page, a column of Labels consisting of text, and to the right of which, are a set of play buttons to play each audio, such as in the .m code:
-(IBAction)pushButton {
NSString *path = [[NSBundle mainBundle] pathForResource:#"mysound" ofType:#"mp3"];
if(theAudio)[theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
theAudio.volume = 1.0;
[theAudio play];
}
To the right of the play button, I have a Post to Facebook button. I know that I can easily post static text to Facebook like so:
-(IBAction)ShareFB1 {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
slComposeViewController = [[SLComposeViewController alloc] init];
slComposeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[slComposeViewController setInitialText:#"My static text that has the contents of the label manually entered"];
[slComposeViewController addURL:[NSURL URLWithString:#"http://stackoverflow.com"]];
[self presentViewController:slComposeViewController animated:YES completion:NULL];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No Facebook Account" message:#"There are no Facebook accounts confiured, configure or create accounts in Settings." delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
What I would like to do, is when the play button is pressed, I want to assign a variable to be equal to the text that is in the associated label. I imagine that should be easy enough. Then, I want to get rid of the individual Post to Facebook buttons to the right of the Play buttons, and I want to have an app bar button for single button access to post to Facebook, with the contents of the label, which would be assigned to the variable when one hits the play button. Having one Facebook button means having to dynamically assign the text to the variable be posted.
Does anyone know how to configure the code to post the contents of a label, or a variable, rather than static text? I have searched for a way to do this, but have not been successful.
What about using:
[slComposeViewController setInitialText:[NSString stringWithFormat:#"Your text no.%d here: %#",yourCounter, yourLabel.text]];
EDIT:
NSString *posttofacebooktext=[mylabelname text];
[slComposeViewController setInitialText:[NSString stringWithFormat:#"Posting to Facebook: %#", posttofacebooktext]];

Twitter login with compatibility of ios5.0,5.1,6.0

As twitter framework is added in ios5.0 I am using its class TWTweetComposeViewController
if([TWTweetComposeViewController canSendTweet])
{
NSLog(#"can send tweet");
}
else{
NSString *message = #"The application cannot send a tweet at the moment. This is because it cannot reach Twitter or you don't have a Twitter account associated with this device.";
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Oops" message:message delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[alertView show];
}
On this step I need to show user the login page of twitter.For that I am confused if i should redirect user to settings to login to user?
And if yes this code is not working
NSURL *twitterURL = [NSURL URLWithString:#"prefs:root=TWITTER"];
[[UIApplication sharedApplication] openURL:twitterURL];
Also I have read some where that this will not work in ios6. So what should i do to just make user login for a moment and not send tweet directly. Right now i just want to make user login.
I also refer to this question It is working with ios6 well but in that case how do i handle for ios5.1 ?
Any help will be appreciated....
I figured it out with below conditional coding..
I got only this way. If anyone have any other way please suggest me.
if (SYSTEM_VERSION_LESS_THAN(#"6.0")) {
if(SYSTEM_VERSION_EQUAL_TO(#"5.0"))
{
NSURL *twitterURL = [NSURL URLWithString:#"prefs:root=TWITTER"];
[[UIApplication sharedApplication] openURL:twitterURL];
}else{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"No_Tw", nil) message:NSLocalizedString(#"No_TW_Ac", nil) delegate:nil cancelButtonTitle:NSLocalizedString(#"Button_Ok", nil) otherButtonTitles:nil, nil];
[alertView show];
}
}
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"6.0")) {
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
tweetSheet.view.hidden=TRUE;
[self presentViewController:tweetSheet animated:NO completion:^{
[tweetSheet.view endEditing:YES];
}];
}
If iOS is 5.0 then it will redirect to login of Twitter otherwise it will show alert to user to go to settings and do login. And for for iOS 6.0 and larger SLComposeViewController is working fine.

FBStreamDialog doesn't show attachment

I use the simple FBStreamDialog example in my iPad app:
FBStreamDialog* pDialog = [[[FBStreamDialog alloc] init] autorelease];
pDialog.delegate = self;
pDialog.userMessagePrompt = #"Example prompt";
pDialog.attachment = #"{\"name\":\"Facebook Connect for iPhone\",\"href\":\"http://developers.facebook.com/connect.php?tab=iphone\",\"caption\":\"Caption\",\"description\":\"Description\",\"media\":[{\"type\":\"image\",\"src\":\"http://img40.yfrog.com/img40/5914/iphoneconnectbtn.jpg\",\"href\":\"http://developers.facebook.com/connect.php?tab=iphone/\"}],\"properties\":{\"another link\":{\"text\":\"Facebook home page\",\"href\":\"http://www.facebook.com\"}}}";
NSLog(#"prompt: %#", pDialog.userMessagePrompt);
NSLog(#"attachment: %#", pDialog.attachment);
[pDialog show];
NSLogs are OK.
The problem is that neither message prompt nor attachment are shown in FBStreamDialog.
Can anyone help?
The problem was in simulator. It works on device.

Publish to Facebook wall from iPhone application

No Matter what i do, i cannot get a post published to an application page wall (the app being logged into) via an iPhone application. I'm able to log in using FBLoginDialog and then retrieve data to populate a tableview, however when i click a button to publish some test content it doesn't work. Here is the button action:
- (void)compose:(id)sender;
{
NSString *tid=#"115372005166292";
NSString *body = #"My Test";
NSArray *obj = [NSArray arrayWithObjects:body,[NSString stringWithFormat:#"%#", tid],nil];
NSArray *keys = [NSArray arrayWithObjects:#"message",#"target_id",nil];
NSDictionary *params = [NSDictionary dictionaryWithObjects:obj forKeys:keys];
[[FBRequest requestWithDelegate:self] call:#"facebook.stream.publish" params:params];
}
I have also used the FBStreamDialog which works, however i'm faced with two issues there. The dialog lacks customization and i'm unable to handle the callback when the item is posted (e.g. reload the tableview)
I've been searching the internet and all of the examples are similar to the code above, so i'm not sure what i could be missing.
Thanks
You need to ask for extended permissions. After login show this:
FBPermissionDialog* dialog = [[[FBPermissionDialog alloc] init] autorelease];
dialog.delegate = self;
dialog.permission = #"status_update";
[dialog show];
Doc: http://github.com/facebook/facebook-iphone-sdk/#readme

Post message on facebook wall from iPhone

I want to post some message on Facebook wall.
I am able to log in and connect with Facebook from iPhone. But I have hard luck to finding out the solution.
Can you please tell me how I can achieve this functionality?
Assuming that you have a valid FBSession Object:
FBStreamDialog* dialog = [[[FBStreamDialog alloc] init] autorelease];
dialog.delegate = self; dialog.userMessagePrompt = #"Example prompt";
dialog.attachment = #"{\"name\":\"Facebook Connect for iPhone\"," "\"href
\":\"http://developers.facebook.com/connect.php?tab=iphone\"," "\"caption\":\"Caption
\",\"description\":\"Description\"," "\"media\":[{\"type\":\"image\"," "\"src
\":\"http://img40.yfrog.com/img40/5914/iphoneconnectbtn.jpg\"," "\"href
\":\"http://developers.facebook.com/connect.php?tab=iphone/\"}]," "\"properties \":{\"another link\":{\"text\":\"Facebook home page\",\"href\":\"http://www.facebook.com\"}}}"; // replace this with a friend's UID // dialog.targetId = #"999999";
[dialog show];
Source: Facebook Connect Reference