Facebook IOS SDK 试图制作单例类?

发布于 2024-12-11 21:44:08 字数 1761 浏览 0 评论 0原文

嘿,我正在尝试从 Facebook IOS SDK(2011 年 10 月)创建一个我自己的类,在其中我可以创建我需要的所有方法和函数,只需在我的任何应用程序视图控制器中导入此类并调用它即可。 因此,这样我就不需要在我使用 facebook 的地方进行所有初始化和委派给控制器。 所以我想这样做,[myfacebookclass initwithappID:myappid] [myfacebookclass postmessage:@"一些消息"];

问题是,当我在 facebook.m (原始类)中设置authorizeWithFBAppAuth:YES safariAuth:YES 标志并且我执行一条 postmessage 时,它​​会在 Facebook 应用程序或 Safari 上重定向我并检查权限,我点击“确定”,然后在我的应用程序中重定向我。应用程序并且不发布消息。

当我尝试发布消息时,我会这样做:

这是在我自己的 Facebook 类中,

myfacebookclass *facebook2;
+(void)initWithAppID:(NSString*) facebookAppId {
    if (facebook2 == nil) {
        facebook2 = [myfacebookclass new];
        [facebook2 setWithAppId:facebookAppId];        
    }
}

- (void) postMessage1:(NSString *)messageToPost {  
    if ([self.facebook isSessionValid]) {        
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                       self.applicationID, @"api_key",
                                       messageToPost,  @"message",
                                       nil];
        [self.facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:self];
    } else {
        NSArray * neededPermissions = [[[NSArray alloc] initWithObjects:@"user_about_me", @"publish_stream", nil] autorelease];
        [self.facebook authorize:neededPermissions];    
    }
}

+ (void)postMessage:(NSString*) message {
        [facebook2 postMessage1:message];
}

我还实现了:

  • (BOOL)应用程序:(UIApplication *)应用程序 openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication 注释:(id)注释 { 返回 [facebook handleOpenURL:url]; }

我没有修改 facebook.m 或 Facebook IOS SDK 中的其他类

Hy, i am trying to make from Facebook IOS SDK (Octomber 2011) a class of my own in where i can make all the methods and functions that i need and just import this class in any of my application view controllers and call it.
So this way i don't need to do all the initialization and delegation to the controllers in where i use the facebook.
So i want to do like this, [myfacebookclass initwithappID:myappid]
[myfacebookclass postmessage:@"some message"];

The thing is when i am seting authorizeWithFBAppAuth:YES safariAuth:YES flags in facebook.m ( the original class) and i do a postmessage it redirects me on Facebook Application or Safari and check's for permision, i hit ok and then redirects me in my application and doesn't post message.

WHen i am trying to post message i do like this :

This is in my own facebook class

myfacebookclass *facebook2;
+(void)initWithAppID:(NSString*) facebookAppId {
    if (facebook2 == nil) {
        facebook2 = [myfacebookclass new];
        [facebook2 setWithAppId:facebookAppId];        
    }
}

- (void) postMessage1:(NSString *)messageToPost {  
    if ([self.facebook isSessionValid]) {        
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                       self.applicationID, @"api_key",
                                       messageToPost,  @"message",
                                       nil];
        [self.facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:self];
    } else {
        NSArray * neededPermissions = [[[NSArray alloc] initWithObjects:@"user_about_me", @"publish_stream", nil] autorelease];
        [self.facebook authorize:neededPermissions];    
    }
}

+ (void)postMessage:(NSString*) message {
        [facebook2 postMessage1:message];
}

I also implemented:

  • (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
    sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    {
    return [facebook handleOpenURL:url];
    }

i've didn't modified facebook.m or other classes in Facebook IOS SDK.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

祁梦 2024-12-18 21:44:08

创建单例类时的一个好习惯是使用共享实例。正确的单例还必须包含一些内存管理重写方法。在您的情况下,可以这样做:

@implementation FacebookManager

static FacebookManager *mySharedFacebookManager;

+ (FacebookManager*)sharedFacebookManager {
    @synchronized(self) {
        if(mySharedFacebookManager == nil) {
            mySharedFacebookManager = [[self alloc] init];
        }
    }
    return mySharedFacebookManager;
}


+ (id)allocWithZone:(NSZone *)zone {    
    @synchronized(self) {
        if(mySharedFacebookManager == nil) {
            mySharedFacebookManager = [super allocWithZone:zone];   
            return mySharedFacebookManager;
        }
    }
    return nil;
}


- (id)copyWithZone:(NSZone *)zone {
    return self;    
}


- (id)retain {  
    return self;    
}


- (unsigned)retainCount {
    return UINT_MAX;
}


- (oneway void)release {

}


- (id)autorelease {
    return self;    
}

在您的情况下,您可能有 Facebook 类型对象作为 ivar,以及一些包装 FB API 的方法,例如像这样:

- (id)init {
 self = [super init];
 if(self) {
    facebook = [[Facebook alloc] initWithAppId:FB_APP_ID andDelegate:self];
// restore previous session
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"] 
        && [defaults objectForKey:@"FBExpirationDateKey"]) {
        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }

    ...
 }
 return self;
}

- (void)login {
    NSArray *permissions = [NSArray arrayWithObjects: @"user_about_me", @"publish_stream", nil];
    [facebook authorize:permissions];
}

- (void)postToFeed:(NSString*)messageToPost {
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   messageToPost, @"message",
                                   nil];


    [facebook requestWithGraphPath:@"me/feed"
                         andParams:params
                     andHttpMethod:@"POST"
                       andDelegate:self];
}

从必须处理 Facebook 的其他类中,构造很简单:

[[FacebookManager sharedFacebookManager] login];

...

[[FacebookManager sharedFacebookManager] postToFeed:@"Hello World!"];

我希望这些片段能对您有所帮助。

要管理您的 Facebook 会话,您必须在 FBSessionDelegate 方法中手动记住 accessToken,如下所示:

- (void)fbDidLogin {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];
}


- (void)fbDidLogout {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:@"FBAccessTokenKey"];
    [defaults removeObjectForKey:@"FBExpirationDateKey"];
    [defaults synchronize];
}

a good practice when making a singleton class is to use shared instance. Correct singleton also has to include some memory management overridden methods. In your case it can be done like this:

@implementation FacebookManager

static FacebookManager *mySharedFacebookManager;

+ (FacebookManager*)sharedFacebookManager {
    @synchronized(self) {
        if(mySharedFacebookManager == nil) {
            mySharedFacebookManager = [[self alloc] init];
        }
    }
    return mySharedFacebookManager;
}


+ (id)allocWithZone:(NSZone *)zone {    
    @synchronized(self) {
        if(mySharedFacebookManager == nil) {
            mySharedFacebookManager = [super allocWithZone:zone];   
            return mySharedFacebookManager;
        }
    }
    return nil;
}


- (id)copyWithZone:(NSZone *)zone {
    return self;    
}


- (id)retain {  
    return self;    
}


- (unsigned)retainCount {
    return UINT_MAX;
}


- (oneway void)release {

}


- (id)autorelease {
    return self;    
}

In your case you may have Facebook type object as ivar, and some methods to wrap over FB API, for example like this:

- (id)init {
 self = [super init];
 if(self) {
    facebook = [[Facebook alloc] initWithAppId:FB_APP_ID andDelegate:self];
// restore previous session
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"] 
        && [defaults objectForKey:@"FBExpirationDateKey"]) {
        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }

    ...
 }
 return self;
}

- (void)login {
    NSArray *permissions = [NSArray arrayWithObjects: @"user_about_me", @"publish_stream", nil];
    [facebook authorize:permissions];
}

- (void)postToFeed:(NSString*)messageToPost {
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   messageToPost, @"message",
                                   nil];


    [facebook requestWithGraphPath:@"me/feed"
                         andParams:params
                     andHttpMethod:@"POST"
                       andDelegate:self];
}

From other classes which have to deal with Facebook the construction is simple:

[[FacebookManager sharedFacebookManager] login];

...

[[FacebookManager sharedFacebookManager] postToFeed:@"Hello World!"];

I hope this snippets will help you.

To manage your facebook session you have to remember accessToken manually in FBSessionDelegate methods like that:

- (void)fbDidLogin {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];
}


- (void)fbDidLogout {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:@"FBAccessTokenKey"];
    [defaults removeObjectForKey:@"FBExpirationDateKey"];
    [defaults synchronize];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文