iOS 9中没有Facebook登录的本机Facebook应用程序 [英] Native Facebook app does not open with Facebook login in iOS 9
问题描述
我有Facebook sdk
FB_IOS_SDK_VERSION_STRING @3.24.0
FB_IOS_SDK_TARGET_PLATFORM_VERSION @v2.2
我正在使用以下方法执行Facebook登录
NSArray * permissions = @ [@email,@user_birthday,@public_profile
FBSessionStateHandler completionHandler = ^(FBSession * session,FBSessionState status,NSError * error){
[self sessionStateChanged:session state:status error:error];
};
if([FBSession activeSession] .state == FBSessionStateCreatedTokenLoaded){
//我们有一个缓存的令牌,所以打开会话
[[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent
fromViewController:nil
completionHandler:completionHandler];
} else {
[self clearAllUserInfo];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
//创建一个新的facebook会话
FBSession * fbSession = [[FBSession alloc] initWithPermissions:permissions];
[FBSession setActiveSession:fbSession];
[fbSession openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent
fromViewController:nil
completionHandler:completionHandler];
}
我在plist文件下有以下设置
< key> LSApplicationQueriesSchemes< / key>
< array>
< string> fbapi< / string>
< string> fbapi20130214< / string>
< string> fbapi20130410< / string>
< string> fbapi20130702< / string>
< string> fbapi20131010< / string>
< string> fbapi20131219< / string>
< string> fbapi20140410< / string>
< string> fbapi20140116< / string>
< string> fbapi20150313< / string>
< string> fbapi20150629< / string>
< string> fb-messenger-api20140430< / string>
< string> fbauth< / string>
< string> fbauth2< / string>
< array>
请让我知道我在这里缺少什么。首先检查 iPhone设备设置 - > Facebook凭据
,但从不打开Facebook应用程序进行登录。似乎无法识别安装在设备上的Facebook应用程序。
以下是新的Facebook登录的完整过程。
这是我如何修改我的Facebook登录集成,以使其能够在最新的更新中工作。 p>
Xcode 7.x , iOS 9 , Facebook SDK 4.x
Step-1。下载最新的Facebook SDK(包括重大变化)。
Step-2。将 FBSDKCoreKit.framework 和 FBSDKLoginKit.framework 添加到您的项目中。
步骤3。现在去项目>构建阶段>添加 SafariServices.framework
Step-4。 info.plist 中有三个更改,我们需要验证。
4.1确保您的info.plist文件中有以下内容
< key> CFBundleURLTypes< / key>
< array>
< dict>
< key> CFBundleURLSchemes< / key>
< array>
< string><你的fb id这里例如。 fbxxxxxx>< /串GT;
< / array>
< / dict>
< / array>
< key> FacebookAppID< / key>
< string><你的FacebookAppID>< / string>
< key> FacebookDisplayName< / key>
< string>< Your_App_Name_Here>< / string>
4.2现在添加下面的白名单Facebook服务器,这是必须为iOS 9
< key> NSAppTransportSecurity< / key>
< dict>
< key> NSExceptionDomains< / key>
< dict>
< key> facebook.com< / key>
< dict>
< key> NSIncludesSubdomains< / key>
< true />
< key> NSExceptionRequiresForwardSecrecy< / key>
< false />
< / dict>
< key> fbcdn.net< / key>
< dict>
< key> NSIncludesSubdomains< / key>
< true />
< key> NSExceptionRequiresForwardSecrecy< / key>
< false />
< / dict>
< key> akamaihd.net< / key>
< dict>
< key> NSIncludesSubdomains< / key>
< true />
< key> NSExceptionRequiresForwardSecrecy< / key>
< false />
< / dict>
< / dict>
< / dict>
4.3添加URL方案
< key> LSApplicationQueriesSchemes< / key>
< array>
< string> fbapi< / string>
< string> fb-messenger-api< / string>
< string> fbauth2< / string>
< string> fbshareextension< / string>
< / array>
Step-5。现在打开 AppDelegate.m文件
5.1添加以下import语句,(删除旧的)。
#import< FBSDKLoginKit / FBSDKLoginKit.h>
#import< FBSDKCoreKit / FBSDKCoreKit.h>
5.2更新以下方法
- (BOOL)应用程序:(UIApplication *)应用程序
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
注释:(id)注释{
return [[FBSDKApplicationDelegate sharedInstance]应用程序:应用程序
openURL:url
sourceApplication:sourceApplication
注释:注释]
}
- (void)applicationDidBecomeActive :( UIApplication *)应用程序{
[FBSDKAppEvents activateApp];
}
- (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions :( NSDictionary *)launchOptions {
return [[FBSDKApplicationDelegate sharedInstance]应用程序:应用程序
didFinishLaunchingWithOptions :launchOptions];
}
Step-6。现在我们需要修改我们的登录控制器,我们在哪里登录任务
6.1在Login ViewController.m
$中添加这些导入b$ b
#import< FBSDKCoreKit / FBSDKCoreKit.h>
#import< FBSDKLoginKit / FBSDKLoginKit.h>
6.2添加Facebook登录按钮
FBSDKLoginButton * loginButton = [[FBSDKLoginButton alloc] init];
loginButton.center = self.view.center;
[self.view addSubview:loginButton];
6.3手柄登录按钮点击
- (IBAction)facebookLogin:(id)sender
{
FBSDKLoginManager * login = [[FBSDKLoginManager alloc] init];
if([FBSDKAccessToken currentAccessToken])
{
NSLog(@令牌可用:%@,[[FBSDKAccessToken currentAccessToken] tokenString]);
[self fetchUserInfo];
}
{
[login logInWithReadPermissions:@ [@email] fromViewController:self handler:^(FBSDKLoginManagerLoginResult * result,NSError * error)
{
如果(错误)
{
NSLog(@登录过程错误);
}
else if(result.isCancelled)
{
NSLog(@用户取消登录);
}
else
{
NSLog(@登录成功);
if([result.grantedPermissions containsObject:@email])
{
NSLog(@result is:%@,result);
[self fetchUserInfo];
}
else
{
[SVProgressHUD showErrorWithStatus:@Facebook电子邮件权限错误];
}
}
}];
}
}
6.4获取用户信息(姓名,电子邮件等)
- (void)fetchUserInfo
{
if([FBSDKAccessToken currentAccessToken])
{
NSLog(@Token可用:%@,[[FBSDKAccessToken currentAccessToken] tokenString]);
[[FBSDKGraphRequest alloc] initWithGraphPath:@me参数:@ {@fields:@id,name,email}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection * ,id result,NSError * error){
if(!error)
{
NSLog(@results:%@,result);
NSString * email = [result objectForKey:@email];
NSString * userId = [result objectForKey:@id];
if(email.length> 0)
{
//开始你的应用程序Todo
}
else
{
NSLog(@Facebook电子邮件未验证);
}
}
else
{
NSLog(@Error%@,error);
}
}];
}
}
步骤7,现在你可以构建项目,你应该在屏幕下面。
希望这将有助于你们。
参考文献:感谢Facebook文档,Stackoverflow文章和Google。 p>
I have updated iPhone 6 plus to iOS 9 beta and trying to perform Facebook login but each time its presenting UIWebView with Facebook login form.
I have Facebook sdk
FB_IOS_SDK_VERSION_STRING @"3.24.0"
FB_IOS_SDK_TARGET_PLATFORM_VERSION @"v2.2"
And I am using following methods to perform Facebook Login
NSArray *permissions = @[@"email",@"user_birthday",@"public_profile"];
FBSessionStateHandler completionHandler = ^(FBSession *session, FBSessionState status, NSError *error) {
[self sessionStateChanged:session state:status error:error];
};
if ([FBSession activeSession].state == FBSessionStateCreatedTokenLoaded) {
// we have a cached token, so open the session
[[FBSession activeSession]openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent
fromViewController:nil
completionHandler:completionHandler];
} else {
[self clearAllUserInfo];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
// create a new facebook session
FBSession *fbSession = [[FBSession alloc] initWithPermissions:permissions];
[FBSession setActiveSession:fbSession];
[fbSession openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent
fromViewController:nil
completionHandler:completionHandler];
}
I have following setting under plist file
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fbapi20130214</string>
<string>fbapi20130410</string>
<string>fbapi20130702</string>
<string>fbapi20131010</string>
<string>fbapi20131219</string>
<string>fbapi20140410</string>
<string>fbapi20140116</string>
<string>fbapi20150313</string>
<string>fbapi20150629</string>
<string>fb-messenger-api20140430</string>
<string>fbauth</string>
<string>fbauth2</string>
<array>
Please let me know what I am missing here. First it is checking for iPhone device Setting-> Facebook credentials
but never open Facebook app for login. Seems it does not recognize Facebook app installed on device.
Below is complete process for new "Facebook login".
this is how I have revised my Facebook Login integration to get it work on latest update.
Xcode 7.x , iOS 9 , Facebook SDK 4.x
Step-1. Download latest Facebook SDK (it includes major changes).
Step-2. Add FBSDKCoreKit.framework and FBSDKLoginKit.framework to your project.
Step-3. Now go to Project > Build Phases > add SafariServices.framework
Step-4. There are three changes in info.plist we need to verify.
4.1 Make sure you have below in your info.plist file
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string><your fb id here eg. fbxxxxxx></string> </array> </dict> </array> <key>FacebookAppID</key> <string><your FacebookAppID></string> <key>FacebookDisplayName</key> <string><Your_App_Name_Here></string>
4.2 Now add below for White-list Facebook Servers, this is must for iOS 9
<key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>facebook.com</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSExceptionRequiresForwardSecrecy</key> <false/> </dict> <key>fbcdn.net</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSExceptionRequiresForwardSecrecy</key> <false/> </dict> <key>akamaihd.net</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSExceptionRequiresForwardSecrecy</key> <false/> </dict> </dict> </dict>
4.3 Add URL schemes
<key>LSApplicationQueriesSchemes</key> <array> <string>fbapi</string> <string>fb-messenger-api</string> <string>fbauth2</string> <string>fbshareextension</string> </array>
Step-5. Now open AppDelegate.m file
5.1 Add below import statements, (remove old one).
#import <FBSDKLoginKit/FBSDKLoginKit.h> #import <FBSDKCoreKit/FBSDKCoreKit.h>
5.2 update following following methods
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSDKAppEvents activateApp];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return [[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
}
Step-6. Now we need to modify our Login Controller, where we do Login task
6.1 Add these imports in Login ViewController.m
#import <FBSDKCoreKit/FBSDKCoreKit.h> #import <FBSDKLoginKit/FBSDKLoginKit.h>
6.2 Add Facebook Login Button
FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.center = self.view.center;
[self.view addSubview:loginButton];
6.3 Handle Login button click
-(IBAction)facebookLogin:(id)sender
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
if ([FBSDKAccessToken currentAccessToken])
{
NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
[self fetchUserInfo];
}
else
{
[login logInWithReadPermissions:@[@"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error)
{
NSLog(@"Login process error");
}
else if (result.isCancelled)
{
NSLog(@"User cancelled login");
}
else
{
NSLog(@"Login Success");
if ([result.grantedPermissions containsObject:@"email"])
{
NSLog(@"result is:%@",result);
[self fetchUserInfo];
}
else
{
[SVProgressHUD showErrorWithStatus:@"Facebook email permission error"];
}
}
}];
}
}
6.4 Get user info (name, email etc.)
-(void)fetchUserInfo
{
if ([FBSDKAccessToken currentAccessToken])
{
NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, email"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(@"results:%@",result);
NSString *email = [result objectForKey:@"email"];
NSString *userId = [result objectForKey:@"id"];
if (email.length >0 )
{
//Start you app Todo
}
else
{
NSLog(@"Facebook email is not verified");
}
}
else
{
NSLog(@"Error %@",error);
}
}];
}
}
Step-7. Now you can build project, you should get below screen.
Hope this will help you guys.
References : Thanks to Facebook docs, Stackoverflow posts and Google.
这篇关于iOS 9中没有Facebook登录的本机Facebook应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!