我正在尝试创建与 Facebook 的连接,但是我在处理 openUrl 时遇到了问题。
过去,我能够在我的应用委托类中添加以下内容:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [[viewController facebook] handleOpenURL:url];
}
正如我所期望的那样工作。但是,这一次我的情况略有不同,因为 viewController 是在应用程序的其他地方加载的。为了解决这个问题,我想出了一个想法,即创建一个负责处理连接的新类,但也可以从我创建 Facebook 帖子的类中访问。
在这里进一步解释是我的应用委托类中的相关代码
.m
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
FacebookConnectionHandler *fbConnHandler = [[FacebookConnectionHandler alloc] init];
return [[fbConnHandler facebook] handleOpenURL:url];
}
然后是FacebookConnectionHandler
类中的代码:
。H
#import <Foundation/Foundation.h>
#import "Other_ViewController.h"
#import "Facebook.h"
@interface FacebookConnectionHandler : NSObject <FBSessionDelegate>
{
Other_ViewController *otherView;
Facebook *facebook;
}
@property(nonatomic, strong)Other_ViewController *otherView;
@property(nonatomic, strong)Facebook *facebook;
+ (id)sharedManager;
@end
.m
#import "FacebookConnectionHandler.h"
@implementation FacebookConnectionHandler
@synthesize otherView;
@synthesize facebook;
static FacebookConnectionHandler *mySingleton = nil;
+ (id)sharedManager
{
@synchronized(self)
{
if (mySingleton == nil) mySingleton = [[self alloc] init];
}
return mySingleton;
}
- (void)fbDidLogin
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
// Allow the user to create a post
[self.otherView createFacebookPost];
}
@end
最后......这是Other_ViewController
类中的相关代码(正在创建帖子):
。H
#import "FBConnect.h"
@interface Other_ViewController : UIViewController <FBSessionDelegate>
{
Facebook *facebook;
}
@property(nonatomic, retain)Facebook *facebook;
- (void)createFacebookPost;
@end
.m
- (void)createFacebookPost
{
// Create the post
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"Blah", @"name",
@"", @"caption",
@"", @"description",
@"http://www.xyz.com", @"link",
@"", @"picture",
nil];
// Post it to the users feed
[facebook dialog:@"feed" andParams:params andDelegate:nil];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex)
{
case kFacebookButton:
{
if (facebook == nil || ![facebook isSessionValid])
{
// Setup Facebook connection
facebook = [[Facebook alloc] initWithAppId:@"1111111111" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"])
{
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
// Set the connection handler
FacebookConnectionHandler *fbConnectionHandler = [[FacebookConnectionHandler alloc] init];
fbConnectionHandler.mapView = self;
fbConnectionHandler.facebook = self.facebook;
if (![facebook isSessionValid])
{
NSArray *permissions = [[NSArray alloc] initWithObjects:@"publish_actions", nil];
[facebook authorize:permissions];
}
}
else
{
// Create the post
[self createFacebookPost];
}
break;
}
default:
break;
}
}
我可能以完全错误的方式解决这个问题并且完全使问题复杂化,但是我对整个 facebook SDK 都是新手,在这一点上我真的很难过。请问有人可以提供解决方案吗?
注意:要清楚,问题是该方法fbDidLogin
没有被调用,因此其余代码没有机会运行。