I am trying to log in to my application using facebook but every time I try to log in, it says `The operation couldn't be completed. (com.facebook.edk error2)

The code I have written is as follows:

 - (void)openSession

{

NSArray *permissions = [NSArray arrayWithObjects:@"email", nil];

[FBSession openActiveSessionWithPermissions:permissions
                               allowLoginUI:YES
                          completionHandler:
 ^(FBSession *session,
   FBSessionState state, NSError *error) {
     [self sessionStateChanged:session state:state error:error];
 }];

permissions = nil;
}

And the sessionStateChanged code is:

- (void)sessionStateChanged:(FBSession *)session
                  state:(FBSessionState) state
                  error:(NSError *)error {

  switch (state) {

    case FBSessionStateOpen: {
        // Pull information on user to send to server
        if (FBSession.activeSession.isOpen) {
            [[FBRequest requestForMe] startWithCompletionHandler:
             ^(FBRequestConnection *connection,
               NSDictionary<FBGraphUser> *user,
               NSError *error) {
                 if (!error) {
                     //login api call
                     NSLog(@"(UserID, Token) :: (%@, %@)\n", session.accessToken, user.id);
                 }
             }];
        }
    }
        break;
    case FBSessionStateClosed:
    case FBSessionStateClosedLoginFailed:
        // Once the user has logged in, we want them to
        // be looking at the root view.
        [self.navigationController popToRootViewControllerAnimated:NO];

        [FBSession.activeSession closeAndClearTokenInformation];

        break;
    default:
        break;
}


if (error) {

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

Please format the code, I have tried but can't figure out the proper formatting

有帮助吗?

解决方案

 //  use facebook SDK 3.8 

add the following methods in AppDelegate.m

 -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:  (NSString *)sourceApplication annotation:(id)annotation
{
return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication     fallbackHandler:^(FBAppCall *call)
    {
        NSLog(@"Facebook handler");
    }
    ];
}


- (void)applicationDidBecomeActive:(UIApplication *)application
{
[FBAppEvents activateApp];
[FBAppCall handleDidBecomeActive];
// 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.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
 [FBSession.activeSession close];
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

se the follwing code in your viewcontroler .h

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

@interface ViewController : UIViewController<FBLoginViewDelegate>


@property (strong, nonatomic) IBOutlet UILabel *lblUserName;
 @property (strong, nonatomic) IBOutlet UITextField *txtEmailId;
@property (strong, nonatomic) IBOutlet UIButton *lblCreate;
@property (strong, nonatomic) IBOutlet FBProfilePictureView *profilePic;

@property (strong, nonatomic) id<FBGraphUser> loggedInUser;

- (IBAction)butCreate:(id)sender;

- (void)showAlert:(NSString *)message
   result:(id)result
    error:(NSError *)error;

 @end

// apply the below code to your view controller.m

- (void)viewDidLoad
{
[super viewDidLoad];
FBLoginView *loginview=[[FBLoginView alloc]initWithReadPermissions:@[@"email",@"user_likes"]];
loginview.frame=CGRectMake(60, 50, 200, 50);
loginview.delegate=self;
[loginview sizeToFit];
[self.view addSubview:loginview];

}

-(void)loginViewShowingLoggedInUser:(FBLoginView *)loginView
{
self.lblCreate.enabled=YES;
self.txtEmailId.enabled=YES;
self.lblUserName.enabled=YES;


}

 -(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
{
self.lblUserName.text=[NSString stringWithFormat:@"%@",user.name];
self.txtEmailId.text=[user objectForKey:@"email"];
//self.profilePic.profileID=user.id;
self.loggedInUser=user;
}

-(void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView
 {

 self.txtEmailId.text=nil;
 self.lblUserName.text=nil;
 self.loggedInUser=nil;
 self.lblCreate.enabled=NO;

}
 -(void)loginView:(FBLoginView *)loginView handleError:(NSError *)error{
   NSLog(@"Show the Error ==%@",error);
 }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top