Facebook仅在安装后首次启动应用程序,而不是重启应用程序

时间:2017-07-22 06:51:11

标签: ios swift3 facebook-ios-sdk

我已在我的应用中集成了FB SDK。如果我安装我的应用程序并尝试首次登录,那么它工作正常。但是,如果我没有登录并重新启动我的应用程序,那么它无法正常工作并停止在webView中,如下面的屏幕截图所示。无法弄清楚为什么第二次它不起作用。

enter image description here

1 个答案:

答案 0 :(得分:0)

first import below in your viewcontroller:

    #import <FBSDKLoginKit/FBSDKLoginKit.h>
    #import <FBSDKCoreKit/FBSDKCoreKit.h>
    #import <FBSDKShareKit/FBSDKShareKit.h>
    #import <FBSDKLoginManager.h>


- (IBAction)LoginWithFacebookButton_Clicked:(id)sender
{
    NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray* facebookCookies = [cookies cookiesForURL:[NSURL URLWithString:@"http://login.facebook.com"]];
    for (NSHTTPCookie* cookie in facebookCookies)
    {
        [cookies deleteCookie:cookie];
    }

    [FBSDKAccessToken setCurrentAccessToken:nil];
    [FBSDKProfile setCurrentProfile:nil];

    FBSDKLoginManager *manager=[[FBSDKLoginManager alloc]init];
    [manager logOut];

    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus==AFNetworkReachabilityStatusNotReachable)
    {
        [STMethod showInternetToast:self.view];
        return;
    }

    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];

    login.loginBehavior = FBSDKLoginBehaviorWeb;

    [login logInWithReadPermissions: @[@"public_profile",@"email"]fromViewController:self handler:^(FBSDKLoginManagerLoginResult result,NSError error)
    {
        if (error)
         {
             NSLog(@"Process error");
         }
         else if (result.isCancelled)
         {
             NSLog(@"Cancelled");
         }
         else
         {
             NSLog(@"Logged in");

             FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                                           initWithGraphPath:@"/me"
                                           parameters:@{@"fields": @"id,name,email,first_name,last_name,gender,birthday,picture.type(large)",}
                                           HTTPMethod:@"GET"];

             [request startWithCompletionHandler:^(FBSDKGraphRequestConnection connection, id Userresult, NSError error)
              {
                  //Insert your code here
              }    


#pragma  mark - Checking for facebook user

 AFHTTPSessionManager*manager=[[AFHTTPSessionManager alloc]init];

   manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

                  NSDictionary *parameters=@{@"fb_id":[Userresult valueForKey:@"id"],@"device_token":[NSString stringWithFormat:@"%@",[USERDEFAULT valueForKey:kToken]],@"device_type":@"ios"};

                  [manager POST:WS_FacebookLogin parameters:parameters success:^(NSURLSessionDataTask * Nonnull task, id  Nonnull responseObject)
                   {


                   }
                        failure:^(NSURLSessionDataTask  _Nonnull task, NSError  _Nonnull error)
                   {

                   }];

              }];
         }
     }];
}