简体   繁体   English

如何在Facebook登录iOS应用程序的授权中拒绝电子邮件许可,再次询问电子邮件许可

[英]how to ask permissions for email again if its permission is denied in authoriztion on facebook login into iOS app

In my app,there is facebook login am creating custom ui for facebook login.I am fetching users public profile and email.On first time logining in with facebook there is an authorization screen shown as shown in 1.jpg.In this screen there is a option to user which displays edit the info you provide.On clicking that edit button user is directed to next screen where he can deny access for email.My issue is if there is a provision that edit the info you provide button is hidden or is there any possibilty that user is asked for email permission again. 在我的应用程序中,有Facebook登录名正在为Facebook登录名创建自定义ui。我正在获取用户的公共资料和电子邮件。首次登录Facebook时,显示授权屏幕,如1.jpg所示。在此屏幕中有用户的一个选项,显示编辑您提供的信息。单击该编辑按钮后,用户将转到下一个屏幕,在该屏幕上他可以拒绝访问电子邮件。我的问题是是否存在编辑您提供的信息按钮的条款被隐藏或有可能再次要求用户授予电子邮件权限。

1.jpg 1.JPG

My code is below: *********Appdelegate.h 我的代码如下:********* Appdelegate.h

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

@interface AppDelegate : UIResponder <UIApplicationDelegate>


@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSString *strBasePath;
-(void)openActiveSessionWithPermissions:(NSArray *)permissions allowLoginUI:(BOOL)allowLoginUI;

@end

******Appdelegate.m


#import "AppDelegate.h"

@implementation AppDelegate

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



return YES;
}

-(void)openActiveSessionWithPermissions:(NSArray *)permissions allowLoginUI:(BOOL)allowLoginUI{
[FBSession openActiveSessionWithReadPermissions:permissions
                                   allowLoginUI:allowLoginUI
                              completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {

                                  // Create a NSDictionary object and set the parameter values.
                                  NSDictionary *sessionStateInfo = [[NSDictionary alloc] initWithObjectsAndKeys:
                                                                    session, @"session",
                                                                    [NSNumber numberWithInteger:status], @"state",
                                                                    error, @"error",
                                                                    nil];

                                  // Create a new notification, add the sessionStateInfo dictionary to it and post it.
                                  [[NSNotificationCenter defaultCenter] postNotificationName:@"SessionStateChangeNotification"
                                                                                      object:nil
                                                                                    userInfo:sessionStateInfo];

                              }];
}

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

 - (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.

if ([FBSession activeSession].state == FBSessionStateCreatedTokenLoaded) {
    [self openActiveSessionWithPermissions:nil allowLoginUI:NO];
}

[FBAppCall handleDidBecomeActive];

}
#import "ViewController.h"
#import "AppDelegate.h"
#import <QuartzCore/QuartzCore.h>
#import <FacebookSDK/FacebookSDK.h>

@interface ViewController ()

@property (nonatomic, strong) AppDelegate *appDelegate;

-(void)hideUserInfo:(BOOL)shouldHide;

-(void)handleFBSessionStateChangeWithNotification:(NSNotification *)notification;

@end

@implementation ViewController

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

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];


self.imgProfilePicture.layer.masksToBounds = YES;
self.imgProfilePicture.layer.cornerRadius = 30.0;
self.imgProfilePicture.layer.borderColor = [UIColor whiteColor].CGColor;
self.imgProfilePicture.layer.borderWidth = 1.0;

[self hideUserInfo:YES];
self.activityIndicator.hidden = YES;

self.appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleFBSessionStateChangeWithNotification:) name:@"SessionStateChangeNotification" object:nil];



}

-(void)hideUserInfo:(BOOL)shouldHide{
self.imgProfilePicture.hidden = shouldHide;
self.lblFullname.hidden = shouldHide;
self.lblEmail.hidden = shouldHide;
}

- (IBAction)toggleLoginState:(id)sender {
if ([FBSession activeSession].state != FBSessionStateOpen &&
    [FBSession activeSession].state != FBSessionStateOpenTokenExtended) {

[self.appDelegate openActiveSessionWithPermissions:@[@"public_profile", @"email"] allowLoginUI:YES];


}
else{
    // Close an existing session.
    [[FBSession activeSession] closeAndClearTokenInformation];

    // Update the UI.
    [self hideUserInfo:YES];
    self.lblStatus.hidden = NO;
    self.lblStatus.text = @"You are not logged in.";
  }

  }

 -(void)handleFBSessionStateChangeWithNotification:(NSNotification *)notification{
// Get the session, state and error values from the notification's userInfo dictionary.
NSDictionary *userInfo = [notification userInfo];

FBSessionState sessionState = [[userInfo objectForKey:@"state"] integerValue];
NSError *error = [userInfo objectForKey:@"error"];

self.lblStatus.text = @"Logging you in...";
[self.activityIndicator startAnimating];
self.activityIndicator.hidden = NO;

// Handle the session state.
// Usually, the only interesting states are the opened session, the closed session and the failed login.
if (!error) {
    // In case that there's not any error, then check if the session opened or closed.
    if (sessionState == FBSessionStateOpen) {
        // The session is open. Get the user information and update the UI.
        [FBRequestConnection startWithGraphPath:@"me"
                                     parameters:@{@"fields": @"first_name, last_name, picture.type(normal), email"}
                                     HTTPMethod:@"GET"
                              completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                  if (!error) {

                                      // Set the use full name.
                                      self.lblFullname.text = [NSString stringWithFormat:@"%@ %@",
                                                               [result objectForKey:@"first_name"],
                                                               [result objectForKey:@"last_name"]
                                                               ];

                                      // Set the e-mail address.
                                      self.lblEmail.text = [result objectForKey:@"email"];

                                      // Get the user's profile picture.
                                      NSURL *pictureURL = [NSURL URLWithString:[[[result objectForKey:@"picture"] objectForKey:@"data"] objectForKey:@"url"]];
                                      self.imgProfilePicture.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:pictureURL]];


                                      strFbEmail=[result objectForKey:@"email"];

                                      strFbFirstName=[result objectForKey:@"first_name"];
                                      strFbLastName=[result objectForKey:@"last_name"];
                                      strFbAccessToken = [[[FBSession activeSession] accessTokenData] accessToken];
                                      NSLog(@"%@ -------- %@ -------- %@ -------%@",strFbAccessToken,strFbEmail,strFbFirstName,strFbLastName);

                                      //[self sendFbData];
                                      // Make the user info visible.
                                      [self hideUserInfo:NO];

                                      // Stop the activity indicator from animating and hide the status label.
                                      self.lblStatus.hidden = YES;
                                      [self.activityIndicator stopAnimating];
                                      self.activityIndicator.hidden = YES;
                                  }
                                  else{
                                      NSLog(@"%@", [error localizedDescription]);
                                  }
                              }];

        [self.btnToggleLoginState setTitle:@"Logout" forState:UIControlStateNormal];
    }
    else if (sessionState == FBSessionStateClosed || sessionState == FBSessionStateClosedLoginFailed){
        // A session was closed or the login was failed. Update the UI accordingly.
        [self.btnToggleLoginState setTitle:@"Login" forState:UIControlStateNormal];
        self.lblStatus.text = @"You are not logged in.";
        self.activityIndicator.hidden = YES;
    }
}
else{
    // In case an error has occurred, then just log the error and update the UI accordingly.
    NSLog(@"Error: %@", [error localizedDescription]);
    [self hideUserInfo:YES];        
    [self.btnToggleLoginState setTitle:@"Login" forState:UIControlStateNormal];

}


}
if ([FBSession.activeSession.permissions indexOfObject:@"email"] == NSNotFound) {
    [FBSession.activeSession requestNewReadPermissions:@[@"email"]
                         completionHandler:^(FBSession *session,
                                             NSError *error) 
    {
         // Handle new permissions callback
    }];
} else {
    // permission exists
}

Try this. 尝试这个。 Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM