Supporting both iOS 5 and iOS 6 with the Facebook

2019-02-19 01:35发布

问题:

I have an app that currently supports posting to Facebook through the feed dialog using the old Facebook SDK for iOS.

After updating to the Facebook SDK 3.1 for iOS, it seems that I can either:

  1. use the old style API (Facebook.h, instantiate a Facebook, call dialog:...)
  2. or use the new style API (FacebookSDK.h, use shared FBSession, native dialogs)

The different header files collide and seem completely incompatible.

Can I do both? If so, then how?

回答1:

Just include Facebook.h instead. To do this first, Copy the DeprecatedHeaders folder into your Frameworks project. The DeprecatedHeaders are found under ~/Documents/FacebookSDK/FacebookSDK.frameworks/Versions/A/. When you copy it over do not copy the items into your project, so they stay copied as a reference.

Next, in your code where you have:

#import <FacebookSDK/FacebookSDK.h>

Replace with this:

#import "Facebook.h"

You may get an error, in which case close and reopen the project.

Next you want to declare a Facebook object and set the session or clear it whenever your Session is open or closed.

Take as an example, the sample: https://github.com/fbsamples/ios-3.1-howtos/tree/master/ShareNativeDialogsHowTo that is documented here, https://developers.facebook.com/docs/howtos/share-native-dialogs-ios-sdk/

You could make the following changes to that sample to fallback to the feed dialog instead of falling back to a view controller with a share UI. In ViewController.m you would make these changes after including the Facebook header:

....
@property (unsafe_unretained, nonatomic) IBOutlet UIButton *publishButton;
@property (nonatomic, retain) Facebook *facebook;

....
@synthesize authButton;
@synthesize facebook = _facebook;

....
- (void)sessionStateChanged:(NSNotification*)notification {
    if (FBSession.activeSession.isOpen) {
        self.publishButton.hidden = NO;
        [self.authButton setTitle:@"Logout" forState:UIControlStateNormal];
        if (nil == self.facebook) {
            self.facebook = [[Facebook alloc]
                             initWithAppId:FBSession.activeSession.appID
                             andDelegate:nil];
            // Store the Facebook session information
            self.facebook.accessToken = FBSession.activeSession.accessToken;
            self.facebook.expirationDate = FBSession.activeSession.expirationDate;
        }
    } else {
        self.publishButton.hidden = YES;
        [self.authButton setTitle:@"Login" forState:UIControlStateNormal];
        self.facebook = nil;
    }
}

- (void) publishUsingFeedDialog {
    // Put together the dialog parameters
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"Facebook SDK for iOS", @"name",
                                   @"Build great social apps and get more installs.", @"caption",
                                   @"The Facebook SDK for iOS makes it easier and faster to develop Facebook integrated iOS apps.", @"description",
                                   @"https://developers.facebook.com/ios", @"link",
                                   @"https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png", @"picture",
                                   nil];

    // Invoke the dialog
    [self.facebook dialog:@"feed" andParams:params andDelegate:nil];
}


- (IBAction)publishButtonAction:(id)sender {
    BOOL displayedNativeDialog =
    [FBNativeDialogs
     presentShareDialogModallyFrom:self
     initialText:@""
    ....

    if (!displayedNativeDialog) {
        /*ShareViewController *viewController =
        [[ShareViewController alloc] initWithNibName:@"ShareViewController"
                                              bundle:nil];
        [self presentViewController:viewController
                           animated:YES
                         completion:nil];*/
        [self publishUsingFeedDialog];
    }
}


回答2:

I think you will get an "duplicate error" if you use both. I know it sucks.

Ive been messing with the duplicate error for days now. Please let me know if you got there.



回答3:

You'll probably have to abstract out a common interface and implement it twice in separate source files (so the compiler never sees Facebook.h and FacebookSDK.h in the same file).



回答4:

It's impossible, you should use Graph API https://developers.facebook.com/docs/howtos/publish-to-feed-ios-sdk/ instead of Feed Dialog :(



回答5:

Try to import

"FBSession.h" 

instead of

#import<FacebookSDK/FacebookSDK.h>

it will solve the duplicate error..