Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
设置与Facebook的连接未按预期工作(iPhone)_Iphone_Ios_Facebook_Delegates - Fatal编程技术网

设置与Facebook的连接未按预期工作(iPhone)

设置与Facebook的连接未按预期工作(iPhone),iphone,ios,facebook,delegates,Iphone,Ios,Facebook,Delegates,我正在尝试创建一个到Facebook的连接,但是我在处理openUrl时遇到了一个问题 过去,我可以在我的应用程序委派类中添加以下内容: - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [[viewController f

我正在尝试创建一个到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];
}
#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
- (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;
    }
}
下面是
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
#import "FBConnect.h"

@interface Other_ViewController : UIViewController <FBSessionDelegate>
{
    Facebook *facebook;
}

@property(nonatomic, retain)Facebook *facebook;

- (void)createFacebookPost;

@end
最后。。。以下是
Other_ViewController
类(创建帖子的地方)中的相关代码:

.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
#import "FBConnect.h"

@interface Other_ViewController : UIViewController <FBSessionDelegate>
{
    Facebook *facebook;
}

@property(nonatomic, retain)Facebook *facebook;

- (void)createFacebookPost;

@end
我可能用了完全错误的方法来处理这个问题,并且已经完全把问题复杂化了,但是我对整个facebook SDK都是新手,现在我真的很困惑。请问有人能提供一个解决方案吗


注意:需要明确的是,问题是没有调用方法
fbdilogin
,因此其余代码没有机会运行。

如果在应用程序委托中没有使用单例,那么您正在创建连接处理程序类的新实例:

而不是

FacebookConnectionHandler *fbConnHandler = [[FacebookConnectionHandler alloc] init];
return [[fbConnHandler facebook] handleOpenURL:url];
试一试

您也没有在
Other\u ViewController
类中使用单例

如果要使用单例体系结构模式,您必须记住始终使用
sharedManager
,并且永远不要分配/初始化新模式:)

我有时会使
init
抛出一个异常,以提醒我存在一个单例方法

static FacebookConnectionHandler *mySingleton = nil;

- (id)init {
    @throw [NSException exceptionWithName:self.class.description reason:@"Please use the sharedManager, don't make a new one of these!" userInfo:nil];
}

- (id)initInternal {
    // Put your real init stuff in here
}

+ (id)sharedManager
{
    @synchronized(self) 
    {
        if (mySingleton == nil) mySingleton = [[self alloc] initInternal];
    }

    return mySingleton;
}

PS使用单独的脸谱网类正是我在以前编写的应用程序中所做的事情-你的架构很好:)我也会考虑让脸谱网连接HANDER类负责它自己的脸谱网实例而不是视图控制器必须这样做: