gpt4 book ai didi

ios - Facebook iOS SDK : Login with Facebook failed

转载 作者:行者123 更新时间:2023-11-29 03:39:12 26 4
gpt4 key购买 nike

在我在互联网上阅读了一些引用资料后,我决定使用 Facebook SDK for iOS 而不是 Xcode Social Framework

因为我的应用程序需要的是“使用 facebook 登录” session ,并且我正在遵循 Sample 文件夹下名为 SessionLoginSample 的示例代码,但当我添加代码时它不起作用。这些代码在 iPad 模拟器上成功构建,但当我单击按钮时,没有任何反应。

你能告诉我我错过了什么吗...谢谢...

我有这个AppDelegate.h:

#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;

@property (strong, nonatomic) FBSession *session;

@end

这是我的AppDelegate.m:

#import "AppDelegate.h"
#import "ViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;
@synthesize session = _session;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
[FBAppEvents activateApp];
[FBAppCall handleDidBecomeActiveWithSession:self.session];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
[self.session close];
}


- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// attempt to extract a token from the url
return [FBAppCall handleOpenURL:url
sourceApplication:sourceApplication
withSession:self.session];
}

#pragma mark Template generated code

@end

我的ViewController.h:

#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *fieldEmail;
@property (strong, nonatomic) IBOutlet UITextField *fieldPassword;
@property (strong, nonatomic) IBOutlet UILabel *titleLogin;
@property (strong, nonatomic) IBOutlet UILabel *labelOutput;

- (IBAction)buttonRegister;
- (IBAction)buttonLogin;
- (IBAction)loginFacebook;
- (IBAction)loginTwitter;

@end

我的ViewController.m:

#import "ViewController.h"
#import "AppDelegate.h"

@interface ViewController ()

@property (strong, nonatomic) UINavigationController* navController;

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

- (void) alertWithTitle:(NSString *)title andMessage:(NSString *)msg
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}

- (IBAction)loginFacebook {
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];

if (appDelegate.session.isOpen) {
[appDelegate.session closeAndClearTokenInformation];

} else {
if (appDelegate.session.state != FBSessionStateCreated) {
appDelegate.session = [[FBSession alloc] init];
}

[appDelegate.session openWithCompletionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
[self alertWithTitle:(@"FB Login") andMessage:(@"It works!!")];
}];
} }
}
@end

更新:我在项目的Framework文件夹下添加了FacebookSDK.framework,还根据this introduction修改了plist文件。也是:

最佳答案

在我看来,您的任务是像 -setActiveSession 中的调用

if (appDelegate.session.state != FBSessionStateCreated) 
{
appDelegate.session = [[FBSession alloc] init];
[FBSession setActiveSession:appDelegate.session];
}

一个月前,我在集成 iOS Facebook sdk 3.5 时遇到了同样的问题,并且我发现您当前的事件 session 不会自动设置。因此,只要有机会获得不同的 session 实例,我就会进行此调用。

另外,

里面

    - (void)applicationDidBecomeActive:(UIApplication *)application
{
[FBAppEvents activateApp];
[FBAppCall handleDidBecomeActiveWithSession:self.session];
}

您应该使用 FBSession.activeSession 而不是 self.session,以确保 FBAppCall 获得相同的 FBSession 实例,并获得结果。

希望对您有所帮助。

关于ios - Facebook iOS SDK : Login with Facebook failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18692910/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com