Iphone app facebook connection? - iphone

hello i have a app that connects and post to user wall but when i try to post it always open the persmission page and in that page it writes you already give permission to this app.I want it to come only 1 time can anyone help me to do that?
- (id)init {
if (self == [super init]) {
facebook = [[Facebook alloc] initWithAppId:kAppId];
facebook.sessionDelegate = self;
if(permissions==nil){
permissions = [[NSArray arrayWithObjects:
#"read_stream", #"user_birthday",
#"publish_stream", nil] retain];
}
[self login];
}
return self;
}
- (void)login {
if (![_session isConnected]) {
[self postToWall];
}
// only authorize if the access token isn't valid
// if it *is* valid, no need to authenticate. just move on
if (![facebook isSessionValid]) {
[facebook authorize:permissions delegate:self];
}
is it because i init method?

See this page - https://developers.facebook.com/docs/guides/mobile/. Basically what you want to do is save the authentication information (access_token) when the user is authorized, then next time you can check for the saved values and skip the authentication.

Related

Publish Feed with SSO in Facebook (IOS)

I am working on Facebook integration and trying for Single Sign-On with publish feed functionality.
I am using latest FacebookSDK. I have Facebook's Hackbook example code but, i am new to all this so it is being difficult to understand completely all this things.
While searching on SSO i got some code, It is working fine. Here is the code i am using (At the end of this page there is a source code attached)
FBUtils.h and FBUtils.m class
ViewController.m
- (IBAction)publishFeed:(id)sender {
//For SSO
[[FBUtils sharedFBUtils] initializeWithAppID:#"3804765878798776"];
NSArray *permision = [NSArray arrayWithObjects:#"read_stream",#"publish_stream", nil];
[[FBUtils sharedFBUtils] LoginWithPermisions:permision];
[FBUtils sharedFBUtils].delegate = self;
FBSBJSON *jsonWriter = [FBSBJSON new];
/// for publishfeed
NSArray* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
#"Get Started",#"name",#"https://itunes.apple.com?ls=1&mt=8",#"link", nil], nil];
NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
// Dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"I have lot of fun preparing.", #"name",
#" exam", #"caption",
#" ", #"description",
#"https://itunes.apple.com", #"link",
#"http://mypng", #"picture",
actionLinksStr, #"actions",
nil];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[delegate facebook] dialog:#"feed"
andParams:params
andDelegate:self];
When i tap Facebook button in my app it is redirect me to Facebook and then retuning back to my app. Now , what i want is to fire publishFeed event right after returning back to the app and it should ask direct for post or cancel options to the user. But it is asking for login again like this.
Can any one help me in this or please suggest me the right way.
Your Suggestions would be a great help.
In your method, you're not checking if the app has permissions to publish post and if the user logged in before. So, every time you call this method, the app wants you to login. I think that is the problem.
If I'm right, you need to add permission and login control in your method like this. This is my sample code from another project, you can get the logic behind it.
- (IBAction)facebookShare:(id)sender
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
// You check the active session here.
if (FBSession.activeSession.isOpen)
{
// You check the permissions here.
if ([FBSession.activeSession.permissions
indexOfObject:#"publish_actions"] == NSNotFound) {
// No permissions found in session, ask for it
[FBSession.activeSession
reauthorizeWithPublishPermissions:
[NSArray arrayWithObject:#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
// If permissions granted, publish the story
[self postFacebook];
[[[UIAlertView alloc] initWithTitle:#"Result"
message:#"Posted in your wall."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil]
show];
}
}];
} else {
// If permissions present, publish the story
[self postFacebook]; // ------> This is your post method.
[[[UIAlertView alloc] initWithTitle:#"Sonuç"
message:#"Duvarında paylaşıldı."
delegate:self
cancelButtonTitle:#"Tamam"
otherButtonTitles:nil]
show];
}
}
else
{
// If there is no session, ask for it.
[appDelegate openSessionWithAllowLoginUI:YES];
}
// NSLog(#"Post complete.");
}

How to authenticate and requestWithGraphPath for facebook at the same time in iOS?

I am integrating my iOS app with facebook. I want to authenticate and send requestWithGraphPath at the same time. How can i do that? Write now i am using following code:
if (!facebook.isSessionValid) {
NSLog(#"in else login button tapped");
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_likes",
#"publish_stream",
#"user_about_me",
#"email",
nil];
[facebook authorize:permissions];
[permissions release];
[facebook requestWithGraphPath:#"me" andDelegate:self];
}
but after requestWithGraphPath is called. My app keep waiting. but next time when i come again and press button then it works fine. Can authenticate and requestWithGraphPath work at the same time when facebook.isSessionValid is false? Thanks in advance.
After you have authorized, if you've setup the delegate properly. And the user comes back to your app, the method:
-(void)fbDidLogin {
}
Should trigger, and there you can request the graphPath.

Multiple requestWithGraphPath Facebook iOS

Maybe a simple question but I can't seem to get it. I want to get the users profile picture but also want search his/her posts. Like this:
[facebook requestWithGraphPath:#"me/picture?type=large" andDelegate:self];
[facebook requestWithGraphPath:#"me/home?q=TEST" andDelegate:self];
I can get the picute out with the following code:
- (void)request:(FBRequest*)request didLoad:(id)result{
if ([result isKindOfClass:[NSData class]])
{
UIImage *image = [[UIImage alloc] initWithData:result];
UIImageView *profilePicture = [[UIImageView alloc] initWithImage:image];
[image release];
profilePicture.frame = CGRectMake(20, 20, 80, 80);
[self.view addSubview:profilePicture];
[profilePicture release];
}
}
But I don't know how i can get the search query data out.
Any help would be great! Thnx
requestWithGraphPath:andDelegate: returns a pointer to an object of type FBRequest. Save it and use it later to differentiate between requests in request:didLoad: comparing it against the first parameter passed to this method.
For example, you can have two properties to hold the request objects. Although, if you are going to deal with many requests, you can use a container like NSDictionary instead.
self.pictureRequest = [facebook requestWithGraphPath:#"me/picture?type=large" andDelegate:self];
self.homeRequest = [facebook requestWithGraphPath:#"me/home?q=TEST" andDelegate:self];
And then in request:didLoad::
- (void)request:(FBRequest*)request didLoad:(id)result {
if (request == self.homeRequest) {
// ...
} else if (request == self.pictureRequest) {
// ...
}
}

facebook-ios-sdk logout

I am building a basic app for school that gets some info from facebook using the facebook-ios-sdk. However, when I log out of the app, it does not give me the option to log back in, even in the demo from facebook. I am checking to see if the sessions is still valid, and it always comes out invalid. That is another problem. Here is the code I have. Any help is appreciated.
- (void)viewDidLoad {
_facebook = [[Facebook alloc] init];
if ([_facebook isSessionValid] == NO) {
//show proper buttons for login
loginButton.hidden = NO;
logoutButton.hidden = YES;
}
else {
//show proper buttons for logout
loginButton.hidden = YES;
logoutButton.hidden = NO;
}
}
That is to check whether I am logged in or not. Then I have the proper buttons showing, but the code above is always returning that the session is invalid. Here are the functions I call to log in or out:
- (void)login {
[_facebook authorize:kAppId permissions:_permissions delegate:self];
}
/**
* Invalidate the access token and clear the cookie.
*/
- (void)logout {
[_facebook logout:self];
}
The facebook object says it has no valid session because its accessToken and/or expirationDate are nil. That's probably because it doesn't persist them itself. You probably need to record the accessToken and expirationDate by handling the login in an FBSessionDelegate's fbDidLogin method.
- (void)fbDidLogin {
[[NSUserDefaults standardUserDefaults] setValue: _facebook.accessToken forKey: #"access_token"];
[[NSUserDefaults standardUserDefaults] setValue: _facebook.expirationDate forKey: #"expiration_date"];
}
Use NSUserDefaults, for example, to persist those values to the device. Then, every time you alloc and init your _facebook object, immediately set the accessToken and expirationDate to the values you stored in NSUserDefaults.
That should fix the facebook object saying isSessionValid = NO always. As for the other problem, I have the same one :(

To display permission page in facebook of iphone

I am new to iphone development, i want to display the permission page after logging facebook.
buttonIndex is the index of my actionsheets.
if(buttonIndex == 1)
{
session = [FBSession sessionForApplication:#"My App key" secret:#"My Key" delegate:self];
FBLoginDialog* dialog = [[[FBLoginDialog alloc] initWithSession:session] autorelease];
[dialog show];
}
by using those code successfully loggin to facebook, but i want to permission page to display,
so i can use,
- (void)session:(FBSession*)session didLogin:(FBUID)uid
{
NSLog(#"User with id %lld logged in.", uid);
FBPermissionDialog* dialog1 = [[[FBPermissionDialog alloc] init] autorelease];
dialog1.delegate = self;
dialog1.permission = #"uid";
[dialog1 show];
}
But its not working. Where can i put that code.
And I want to share my content after the permission allowed.
If i logout the facebook, it goes to the browser but i want to return my application after logout,
Please help me out, guide me plz.
I would change this dialog1.permission = #"uid"; to something like this
dialog1.permission = #"publish_stream";. Because you want to publish your content to the users stream, right?
- (void)session:(FBSession*)session didLogin:(FBUID)uid
After loggin in I would first check if you might already have the permission to publish to the user's stream, by creating a FBRequest
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: #"publish_stream", #"ext_perm", nil];
[[FBRequest requestWithDelegate:self] call:#"facebook.users.hasAppPermission" params:params];
The result you can evaluate here
- (void)request:(FBRequest*)request didLoad:(id)result
e.g. like this
if ([request.method isEqualToString:#"facebook.users.hasAppPermission"])
{
NSString *success = result;
if ([success isEqualToString:#"1"])
{
NSLog(#"User has app permission");
// publish content now
...
}
else
{ // else ask for permission, opening permission dialog
...
}
I highly recommend this guy's tutorial, Brandon Treb, on integrating Facebook. He does a very thorough presentation and takes you line-by-line, so if it does not work, its a typo on your part. His tutorial got me up and running in less than two hours.
http://brandontreb.com/