Facebook仅适用于安装后首次启动应用程序,而不是重新启动应用程序
Posted
技术标签:
【中文标题】Facebook仅适用于安装后首次启动应用程序,而不是重新启动应用程序【英文标题】:Facebook is working only on first time app start after install, not if restart the app 【发布时间】:2017-07-22 06:51:10 【问题描述】:我在我的应用程序中集成了 FB SDK。如果我安装我的应用程序并尝试第一次登录,那么它工作正常。但是,如果我没有登录并重新启动我的应用程序,那么它就无法工作并在 webView 中停止,如下面的屏幕截图所示。无法弄清楚为什么第二次它不起作用。
【问题讨论】:
【参考方案1】: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)
];
];
];
【讨论】:
以上是关于Facebook仅适用于安装后首次启动应用程序,而不是重新启动应用程序的主要内容,如果未能解决你的问题,请参考以下文章