gpt4 book ai didi

ios - iOS 应用程序中的 Facebook 连接

转载 作者:行者123 更新时间:2023-11-29 13:09:10 25 4
gpt4 key购买 nike

在我的新 iOS 应用程序中,我必须登录 Facebook 并获取用户详细信息。我在这里阅读了教程 - http://developers.facebook.com/docs/tutorials/ios-sdk-tutorial/authenticate/并研究了 Scrumptious 和其他样本。但是我无法理解流程。

我已经创建了一个示例项目来进行 Facebook 登录(下面的代码)- 但有以下主要问题:

  • 为什么当我再次点击登录按钮时应用程序要求授权 - 我在 Facebook 上点击了一次“确定”之后

下面是我的代码-

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// See if the app has a valid token for the current state.
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded)
{
// To-do, show logged in view --- LOGGED IN : Already Login
// Yes, so just open the session (this won't display any UX).
UIWindow *window = [UIApplication sharedApplication].keyWindow;
ViewController *rootViewController = (ViewController *)window.rootViewController;
[rootViewController openSession];
}

return YES;
}

- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [FBSession.activeSession handleOpenURL:url];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
[FBSession.activeSession handleDidBecomeActive];
}


@implementation ViewController

@synthesize lblStatus;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

lblStatus.text = @"Login Now";
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)login:(id)sender
{
[self openSession];
}

- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
{
// Connection is Open
lblStatus.text = @"FBSessionStateOpen";
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
{
[FBSession.activeSession closeAndClearTokenInformation];

// Connection is Closed / Login Failed
lblStatus.text = @"FBSessionStateClosed";
}
break;
default:
break;
}
}

- (void)openSession
{
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error)
{
[self sessionStateChanged:session state:state error:error];
}];
}

回答---->

感谢 nerowolfe 的建议。

以下是我连接到 Facebook 的完整解决方案。我的下一个任务是立即获取用户数据。

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [FBSession.activeSession handleOpenURL:url];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
[FBSession.activeSession handleDidBecomeActive];
}

--------------------------

@implementation ViewController

@synthesize lblStatus;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

lblStatus.text = @"Login Now";
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)login:(id)sender
{
[self openSession];
}

- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
{
// Connection is Open
lblStatus.text = @"FBSessionStateOpen";
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
{
[FBSession.activeSession closeAndClearTokenInformation];

// Connection is Closed / Login Failed
lblStatus.text = @"FBSessionStateClosed";
}
break;
default:
break;
}
}

- (void)openSession
{
if(FBSession.activeSession.isOpen)
{
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:NO
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error)
{
[self sessionStateChanged:session state:state error:error];
}];
}
else
{
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error)
{
[self sessionStateChanged:session state:state error:error];
}];
}
}

- (IBAction)logout:(id)sender
{
[FBSession.activeSession closeAndClearTokenInformation];

if(FBSession.activeSession.isOpen)
lblStatus.text = @"Open Still";
else
lblStatus.text = @"Close";

}

最佳答案

只是不显示 LoginUI。

 - (void)openSession
{
if(FBSession.activeSession.isOpen)
{

[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:NO
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error)
{
[self sessionStateChanged:session state:state error:error];
}];
}

}

关于ios - iOS 应用程序中的 Facebook 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17809620/

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