如何通过按取消修复 Facebook 登录按钮中的崩溃
How to fix crash in Facebook LogIn button by pressing cancel
出于教育目的,我正在尝试创建一个简单的应用程序来使用 Facebook 登录。
我没有使用 Cocoa Pods.
手动导入了 SD
这是我的应用委托中的代码:
import UIKit
import FBSDKCoreKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}
func applicationDidBecomeActive(_ application: UIApplication) {
FBSDKAppEvents.activateApp()
}
}
在ViewController中,
我声明了标签和 immageView,我将在其中输入从 facebook 下载的数据。
在这一点上,我向您展示了 ViewController:
中编写的代码
import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
class ViewController: UIViewController, FBSDKLoginButtonDelegate {
@IBOutlet weak var lbl_fbid: UILabel!
@IBOutlet weak var lbl_fbdirstname: UILabel!
@IBOutlet weak var lbl_fblastname: UILabel!
@IBOutlet weak var lbl_fbemail: UILabel!
@IBOutlet weak var img_fbprofilepic: UIImageView!
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if (error == nil) {
print("Connected")
let r = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,first_name,last_name,picture.type(large)"], tokenString: FBSDKAccessToken.current()?.tokenString, version: nil, httpMethod: "GET")
r?.start(completionHandler: { (test, result, error) in
if let id : NSString = (result! as AnyObject).value(forKey: "id") as? NSString {
print("id: \(id)")
self.lbl_fbid.text = "ID: \(id)"
}
if let imageURL = (((((result! as AnyObject).value(forKey: "picture")) as AnyObject).value(forKey: "data")) as AnyObject).value(forKey: "url") as? NSString{
print("img url: \(imageURL)")
let url = URL(string: imageURL as String)
DispatchQueue.global().async {
let data = try? Data(contentsOf: url!)
DispatchQueue.main.async {
self.img_fbprofilepic.image = UIImage(data: data!)
}
}
}
if let first_name : NSString = (result! as AnyObject).value(forKey: "first_name") as? NSString {
print("first_name: \(first_name)")
self.lbl_fbdirstname.text = "First_Name: \(first_name)"
}
if let last_name : NSString = (result! as AnyObject).value(forKey: "last_name") as? NSString {
print("last_name: \(last_name)")
self.lbl_fblastname.text = "First_Name: \(last_name)"
}
if let email : NSString = (result! as AnyObject).value(forKey: "email") as? NSString {
print("email: \(email)")
self.lbl_fbemail.text = "Email: \(email)"
}
if(error == nil)
{
print(result as Any)
}
})
} else {
print(error.localizedDescription)
}
}
func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
print("Disconnected")
self.lbl_fbid.text = "ID: "
self.lbl_fbdirstname.text = "First_Name: "
self.lbl_fblastname.text = "First_Name: "
self.lbl_fbemail.text = "Email: "
self.img_fbprofilepic.image = nil
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let loginButton = FBSDKLoginButton()
loginButton.readPermissions = ["public_profile", "email"]
loginButton.center = self.view.center
loginButton.delegate = self
self.view.addSubview(loginButton)
}
}
这里一切正常。我可以通过 facebook 登录,我可以用下载的数据填充标签和图像。
所以问题是另一个。如果碰巧,在登录过程中,我点击了应用程序崩溃的取消按钮,控制台给我这个:
Connected
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
我被重定向到 AppDelegate 中的这一行:
class AppDelegate: UIResponder, UIApplicationDelegate {
此消息为红色 "Thread 1: signal SIGABRT"。
我通过以这种方式构造函数 loginButton 来解决:
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if (error == nil) {
if(result.isCancelled){
print("login cancelled")
}else{
//code here
print("Connected")
}
} else {
print(error.localizedDescription)
}
}
再见;)
出于教育目的,我正在尝试创建一个简单的应用程序来使用 Facebook 登录。
我没有使用 Cocoa Pods.
手动导入了 SD这是我的应用委托中的代码:
import UIKit
import FBSDKCoreKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}
func applicationDidBecomeActive(_ application: UIApplication) {
FBSDKAppEvents.activateApp()
}
}
在ViewController中, 我声明了标签和 immageView,我将在其中输入从 facebook 下载的数据。 在这一点上,我向您展示了 ViewController:
中编写的代码import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
class ViewController: UIViewController, FBSDKLoginButtonDelegate {
@IBOutlet weak var lbl_fbid: UILabel!
@IBOutlet weak var lbl_fbdirstname: UILabel!
@IBOutlet weak var lbl_fblastname: UILabel!
@IBOutlet weak var lbl_fbemail: UILabel!
@IBOutlet weak var img_fbprofilepic: UIImageView!
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if (error == nil) {
print("Connected")
let r = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,first_name,last_name,picture.type(large)"], tokenString: FBSDKAccessToken.current()?.tokenString, version: nil, httpMethod: "GET")
r?.start(completionHandler: { (test, result, error) in
if let id : NSString = (result! as AnyObject).value(forKey: "id") as? NSString {
print("id: \(id)")
self.lbl_fbid.text = "ID: \(id)"
}
if let imageURL = (((((result! as AnyObject).value(forKey: "picture")) as AnyObject).value(forKey: "data")) as AnyObject).value(forKey: "url") as? NSString{
print("img url: \(imageURL)")
let url = URL(string: imageURL as String)
DispatchQueue.global().async {
let data = try? Data(contentsOf: url!)
DispatchQueue.main.async {
self.img_fbprofilepic.image = UIImage(data: data!)
}
}
}
if let first_name : NSString = (result! as AnyObject).value(forKey: "first_name") as? NSString {
print("first_name: \(first_name)")
self.lbl_fbdirstname.text = "First_Name: \(first_name)"
}
if let last_name : NSString = (result! as AnyObject).value(forKey: "last_name") as? NSString {
print("last_name: \(last_name)")
self.lbl_fblastname.text = "First_Name: \(last_name)"
}
if let email : NSString = (result! as AnyObject).value(forKey: "email") as? NSString {
print("email: \(email)")
self.lbl_fbemail.text = "Email: \(email)"
}
if(error == nil)
{
print(result as Any)
}
})
} else {
print(error.localizedDescription)
}
}
func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
print("Disconnected")
self.lbl_fbid.text = "ID: "
self.lbl_fbdirstname.text = "First_Name: "
self.lbl_fblastname.text = "First_Name: "
self.lbl_fbemail.text = "Email: "
self.img_fbprofilepic.image = nil
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let loginButton = FBSDKLoginButton()
loginButton.readPermissions = ["public_profile", "email"]
loginButton.center = self.view.center
loginButton.delegate = self
self.view.addSubview(loginButton)
}
}
这里一切正常。我可以通过 facebook 登录,我可以用下载的数据填充标签和图像。
所以问题是另一个。如果碰巧,在登录过程中,我点击了应用程序崩溃的取消按钮,控制台给我这个:
Connected
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
我被重定向到 AppDelegate 中的这一行:
class AppDelegate: UIResponder, UIApplicationDelegate {
此消息为红色 "Thread 1: signal SIGABRT"。
我通过以这种方式构造函数 loginButton 来解决:
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if (error == nil) {
if(result.isCancelled){
print("login cancelled")
}else{
//code here
print("Connected")
}
} else {
print(error.localizedDescription)
}
}
再见;)