简体   繁体   中英

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM