iphone - Facebook SDK Login 启动一个新的 ViewController

标签 iphone ios xcode facebook

我一直在关注来自 Facebook SDK 的登录教程之一。我已经正确编码以使用相同的 ViewController 和 Button 登录和注销。我想知道如何在登录 Facebook 后激活另一个 ViewController,任何建议都很好......

我有一个 LoginViewController,登录后我想启动 MainViewController

代码:

AppDelegate.h

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

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

extern NSString *const FBSessionStateChangedNotification;
extern NSString *const SCSessionStateChangedNotification;

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;

- (void) closeSession;

@end

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

NSString *const FBSessionStateChangedNotification =
@"com.example.Login:FBSessionStateChangedNotification";

NSString *const SCSessionStateChangedNotification =
@"com.nathancleary.Login:SCSessionStateChangedNotification";

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:  (NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}

/*
* If we have a valid session at the time of openURL call, we handle
* Facebook transitions by passing the url argument to handleOpenURL
*/
- (BOOL)application:(UIApplication *)application
        openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
     annotation:(id)annotation {
// attempt to extract a token from the url
return [FBSession.activeSession handleOpenURL:url];
}

- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can    occur for certain types of temporary interruptions (such as an incoming phone call or SMS    message) or when the user quits the application and it begins the transition to the background   state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES  frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and  store enough application state information to restore your application to its current state in  case it is terminated later. 
// If your application supports background execution, this method is called instead of  applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you  can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was     inactive. If the application was previously in the background, optionally refresh the user    interface.

// this means the user switched back to this app without completing
// a login in Safari/Facebook App
if (FBSession.activeSession.state == FBSessionStateCreatedOpening) {
    [FBSession.activeSession close]; // so we close our session and start over
}
}

- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also   applicationDidEnterBackground:.

[FBSession.activeSession close];
}

/*
* Callback for session changes.
*/
- (void)sessionStateChanged:(FBSession *)session
                  state:(FBSessionState) state
                  error:(NSError *)error
{
switch (state) {
    case FBSessionStateOpen:
        if (!error) {
            // We have a valid session
            NSLog(@"User session found");
        }
        break;
    case FBSessionStateClosed:
    case FBSessionStateClosedLoginFailed:
        [FBSession.activeSession closeAndClearTokenInformation];
        break;
    default:
        break;
}

[[NSNotificationCenter defaultCenter]
 postNotificationName:FBSessionStateChangedNotification
 object:session];

if (error) {
    UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"Error"
                              message:error.localizedDescription
                              delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
    [alertView show];
}
}

/*
* Opens a Facebook session and optionally shows the login UX.
*/
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
NSArray *permissions = [[NSArray alloc] initWithObjects:
                        @"user_likes",
                        @"read_stream",
                        nil];
return [FBSession openActiveSessionWithPermissions:permissions
                                      allowLoginUI:allowLoginUI
                                 completionHandler:^(FBSession *session,
                                                     FBSessionState state,
                                                     NSError *error) {
                                     [self sessionStateChanged:session
                                                         state:state
                                                         error:error];
                                 }];
}

- (void) closeSession {
[FBSession.activeSession closeAndClearTokenInformation];
}

@end

LoginViewController.h

#import <UIKit/UIKit.h>

@interface LoginViewController : UIViewController

- (IBAction)authButtonAction:(id)sender;

@property (strong, nonatomic) IBOutlet UIButton *authButton;

@end

登录 View Controller .m

#import "LoginViewController.h"
#import "AppDelegate.h"

@interface LoginViewController ()

@end

@implementation LoginViewController
@synthesize authButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

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

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(sessionStateChanged:)
 name:FBSessionStateChangedNotification
 object:nil];

// Check the session for a cached token to show the proper authenticated
// UI. However, since this is not user intitiated, do not show the login UX.
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate openSessionWithAllowLoginUI:NO];
}

- (void)viewDidUnload
{
[self setAuthButton:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.

[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)sessionStateChanged:(NSNotification*)notification {
if (FBSession.activeSession.isOpen) {
    [self.authButton setTitle:@"Logout" forState:UIControlStateNormal];
} else {
    [self.authButton setTitle:@"Login" forState:UIControlStateNormal];
}
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)authButtonAction:(id)sender {

AppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];

// If the user is authenticated, log out when the button is clicked.
// If the user is not authenticated, log in when the button is clicked.
if (FBSession.activeSession.isOpen) {
    [appDelegate closeSession];
} else {
    // The user has initiated a login, so call the openSession method
    // and show the login UX if necessary.
    [appDelegate openSessionWithAllowLoginUI:YES];
}


}

@end

最佳答案

如果您使用的是 Facebook 的 sdk 3,您可以将它放在由完成处理程序调用的方法中:

- (void)openFacebookSession
{
    NSArray *permissions = [[NSArray alloc] initWithObjects:
                            @"user_photos",
                            nil];
    [FBSession openActiveSessionWithPermissions:permissions
                                   allowLoginUI:YES
                              completionHandler: ^(FBSession *session, FBSessionState state, NSError *error){
                                  [self sessionStateChanged:session state:state error:error];
                              }];
}

这里是启动主视图 Controller 的地方:

- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen:
        {
            NSLog(@"FBSessionStateOpen");

            //[activityIndicator startAnimating];
            // initialize the viewcontroller with a little delay, so that the UI displays the changes made above
            double delayInSeconds = 0.1;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

                UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                                         bundle: nil];

                UIViewController *controller = [mainStoryboard instantiateViewControllerWithIdentifier: @"MainViewController"];

                [self presentViewController:controller animated:YES completion:nil];

                //[activityIndicator stopAnimating];
            });

            break;
        }

        case FBSessionStateClosed:
        {
            NSLog(@"FBSessionStateClosed");
            break;
        }

        case FBSessionStateClosedLoginFailed:
        {
             [FBSession.activeSession closeAndClearTokenInformation];
             NSLog(@"Facebook State Closed or Failed");

             break;
        }
        default:
        {
             break;
        }
    }

    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:error.localizedDescription
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
        [alertView show];
    }
}

此方法专门用于当您必须加载沉重的主 Controller 时(这就是为什么您可以激活加载指示器)。您可以轻松地修改它以加载一个可以在同一线程中启动的简单程序。

关于iphone - Facebook SDK Login 启动一个新的 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12240702/

相关文章:

ios - UIActivityViewController 在电子邮件或文本而非 fb 或 Twitter 时卡住

xcode - Git Push 是通过终端工作的,但不是通过 Xcode 工作的

xcode - swift Playground : Replacement for captureValue(_:withIdentifier:)

iphone - 如果我要发布 bonjour iOS 应用程序,是否需要向 IANA 注册我的服务名称?

iPhone + JSON + 反射

iphone - IOS。 viewControllers 数组的错误?

ios - 测试飞行泄漏

ios - 如何使用 Storyboard在另一个 Controller 中 subview UITableViewController

iphone - 如何在 xCode 中使用 jpeglib

ios - 自定义 UITableViewCell 仅在 iPhone5 上限制用户触摸响应