ARC project with delegate reference fails compilin

2019-07-25 13:30发布

I'm having trouble implementing the Facebook SDK in an iOS project and I suspect that this line is to blame:

[facebook authorize:permissions delegate:self];

When I try to run it in my ARC project, it will not allow the delegate:self to remain. Instead it says there is an "ARC Error: no visible @interface for 'Facebook' declares the selector 'authorize:delegate:'"

I can compile with this:

[facebook authorize:permissions];

but I don't think Facebook and the app communicate properly by doing so. I am able to login, but once redirected back to my app, the app does not register that it is logged in and can't execute any of its sharing functions.

I think I need to maintain that page as ARC enabled, so setting a flag to -fno-objc-arc won't resolve this.

Any thoughts?


FOLLOWUP 4/5/12:

I was able to successfully implement Facebook… but using just the popup login method in app. Here's how I made it happen (just FB aspects show):

My app delegate header:

#import "FBConnect.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>{

    Facebook *facebook;

}

@property (nonatomic, strong) Facebook *facebook;

@end

App delegate method, included:

////FB ELEMENT: IMPORTING VC TO CONNECT WITH FACEBOOK
#import "ViewController.h"

////FB ELEMENT
@synthesize facebook;

///////FB ELEMENTS
// THIS METHOD SUPPORTS IOS PRE 4.2
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{

    NSLog(@"handleOpenURL called.");

    return [facebook handleOpenURL:url];

}

// THIS METHOD SUPPORTS IOS AFTER 4.2
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{

NSLog(@"openURL called.");

return [facebook handleOpenURL:url];
}

Then I wanted the bulk of the Facebook activity to be on the page calling it. Here is the header:

#import "FBConnect.h"

@interface ViewController : UIViewController <FBSessionDelegate, FBDialogDelegate>

//FB ELEMENTS
Facebook *facebook;
BOOL login;

//////SHARE PROPERTIES FOR PHOTO DISPLAY LAYER
@property (nonatomic, strong) Facebook *facebook;

And on the view controller's method:

@synthesize facebook;

////////////////////////////////////////////

-(void)postWall{

    NSLog(@"postWall called.");

    //CREATING MORE ACCESSIBLE VARIABLE
    NSString *photoTitle = [photoTitles objectAtIndex:pt];


    //ESTABLISHES DESCRIPTION CONTENT
    NSDictionary *itemDescriptionPre = [photoDescriptions objectAtIndex:pt];
    NSString *itemDescription = [itemDescriptionPre objectForKey:@"_content"];

    NSMutableDictionary *paramsSet = 
    [NSMutableDictionary dictionaryWithObjectsAndKeys:

     //CLICKABLE LINK COMPONENT (STORY TITLE WORKS WELL HERE)
     photoTitle, @"name",

     //STORY DESCRIPTION GOES HERE
     itemDescription, @"description",

     //STORY LINK GOES HERE
     [flickrURLLink objectAtIndex:pt], @"link",

     //PHOTO TO CONNECT WITH POST
     [photoURLsLargeImage objectAtIndex:pt], @"picture",
     nil];  

    [[self facebook] dialog:@"feed" andParams:paramsSet andDelegate:self];

}

- (void) fbDidLogin {

    facebook = [[Facebook alloc] initWithAppId:@"MY_APP_ID" andDelegate:self];

    login = YES;

    NSLog(@"Logged in with Facebook credentials");

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationKey"];
    [defaults synchronize];

    [self postWall];
}

- (void)fbDidNotLogin:(BOOL)cancelled{
    ///TO COMPLETE
}

- (void)fbDidExtendToken:(NSString*)accessToken
               expiresAt:(NSDate*)expiresAt{
    ////TO COMPLETE
}


- (void)fbDidLogout{

    login = NO;
    NSLog(@"Logged out with Facebook credentials");
}

- (void)fbSessionInvalidated{

    login = NO;

    NSLog(@"Logged out with Facebook credentials");
}

After setting those up, I made sure I added the app ID into the plist as described by the Facebook tutorial and all worked as expected.

I am aware that the BOOL login value doesn't affect anything yet.

My current problem with this code is a compatibility problem before 5.1.

It works great in my 5.1 test device and simulator, but does not do anything after the login step if I go to 5.0 or below. Any idea why that would be the case? I am able to enter my login data, then returned to the app, but not presented any more options.

Thanks for all your feedback so far. Would love to hear your thoughts on this item as well.

5条回答
干净又极端
2楼-- · 2019-07-25 13:47

I was able to successfully implement Facebook… but using just the popup login method in app. Details were added as an edit to my original post.

查看更多
Ridiculous、
3楼-- · 2019-07-25 13:51

Use the Facebook Graph API, should solve your problem

// get information about the currently logged in user
[facebook requestWithGraphPath:@"me" andDelegate:self];

OR

facebook = [[Facebook alloc] initWithAppId:@"YOUR_APP_ID" andDelegate:self];

This instance is used to invoke SSO, as well as the Graph API and Platform Dialogs from within the app.

your class must implement required Facebook delegate

查看更多
该账号已被封号
4楼-- · 2019-07-25 13:54

Do you need to maintain a strong reference to the facebook object? It would seem it is not maintaining its status as a delegate.

查看更多
干净又极端
5楼-- · 2019-07-25 13:54

The ARC docs (or was it the video?) mentioned this. The compiler becomes more strict towards undeclared methods. Usually, adding the proper header file should fix it, but I'm not familiar with the Facebook API so I can't tell for sure.

查看更多
Rolldiameter
6楼-- · 2019-07-25 13:57

Are you using a version of the SDK that doesn't have an authorize:delegate: method? Maybe you need to do this:

facebook.delegate = self;
[facebook authorize:permissions];
查看更多
登录 后发表回答