我是swift和ios的新手,我正在开发一款应用程序来学习。 我正在尝试使用FBSDK在我的应用程序中提供登录功能
我有一个viewcontroller并使用界面构建器添加了UIButton。 我能够使用IBOutlet和IBAction连接到UIButton来查看控制器。
在按钮的操作中,我试图调用LoginManager.login 我看到一个弹出窗口,重定向到Facebook,但没有显示Facebook权限页面,并重定向回viewcontroller页面
我认为它必须在稍后完成完成处理程序 我试着像
那样调用doFacebookLoginFuncDispatchQueue.main.async {
doFacebookLoginFunc()
}
但观察到类似的行为
func doFacebookLoginFunc() {
print("use facebook login")
let permissions = ["user_profile", "email","public_profile"]
var loginManager = FBSDKLoginManager()
loginManager.logIn(withReadPermissions: permissions,from: self){
(loginResult, error) -> Void in
//println(FBSDKLoginManagerLoginResult
print("inside login completion handler")
print(loginResult)
//var fbloginresult : FBSDKLoginManagerLoginResult = loginResult!
if (error == nil){
if(loginResult!.grantedPermissions.contains("email") {
print("email success")
}
}
}
}
@IBAction func doFacebookLogin(_ sender: UIButton) {
print("doFacebookLogin start")
doFacebookLoginFunc()
print("doFacebookLogin complete")
}
AppDelegate.m
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application(_ application: UIApplication,
open url: URL,
sourceApplication: String?,
annotation: AnyObject) -> Bool {
print ("new application called")
let tmp: Bool = FBSDKApplicationDelegate.sharedInstance().application(
application,
open: url,
sourceApplication: sourceApplication,
annotation: annotation)
print ("new application called and returning \(tmp)")
return tmp
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application(_ application: UIApplication,
open url: URL,
sourceApplication: String?,
annotation: AnyObject) -> Bool {
print ("new application called")
let tmp: Bool = FBSDKApplicationDelegate.sharedInstance().application(
application,
open: url,
sourceApplication: sourceApplication,
annotation: annotation)
print ("new application called and returning \(tmp)")
return tmp
}
func applicationDidBecomeActive(_ application: UIApplication) {
// 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.
FBSDKAppEvents.activateApp()
}
当我点击按钮
时,这是控制台输出doFacebookLogin启动 使用facebook登录 2016-08-03 19:50:39.717思考[59012:4249476] -canOpenURL:URL失败:" fbauth2:/" - 错误:"(null)" 2016-08-03 19:50:39.722思考[59012:4249476] -canOpenURL:URL失败:" fbauth2:/" - 错误:"(null)" doFacebookLogin完成 新的应用程序叫 内部登录完成处理程序 零 新应用程序调用并返回true
我已经在Facebook开发人员上使用入门指南配置了应用程序 我可以使用仪表板中配置的测试用户
使用FBSDKLoginButton登录感谢大家通过这个