SwiftUI Facebook 登录按钮对话框仍然打开?
Posted
技术标签:
【中文标题】SwiftUI Facebook 登录按钮对话框仍然打开?【英文标题】:SwiftUI Facebook login button dialog still open? 【发布时间】:2021-04-18 11:14:17 【问题描述】:我正在尝试在我的项目中实现 facebook 登录按钮,一切都很好,但是从 Facebook 返回后的对话框仍然打开而不是关闭。我认为它与 URL 应用程序委托相关的东西不会触发,因为我使用的是没有场景委托的 SwiftUI。
完美的登录功能:
fileprivate func facebookLogin()
let fbLoginManager = LoginManager()
fbLoginManager.logIn(permissions: ["public_profile", "email"], from: Tools.topViewController()! )
(result, error) in
if ( result != nil && (result?.isCancelled)! )
print("[LOGIN][FACEBOOK] cancecled by User")
return
if result != nil && (result?.grantedPermissions != nil)
print("[LOGIN][FACEBOOK] grantedPermissions")
if((AccessToken.current) != nil)
GraphRequest(graphpath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).start(completionHandler: (connection, result, error) -> Void in
if (error == nil)
print("[LOGIN][FACEBOOK SUCCESS] \(String(describing: result))")
var fb_result = result as! [String : Any]
let name = fb_result["name"] as! String
else
print("[LOGIN][FACEBOOK ERROR] \(String(describing: error))")
)
else
//Something went Wrong !
print("[LOGIN][FACEBOOK ERROR] Something went wrong! #1")
//Something went Wrong !
else
应用:
@main
struct LW_APP: App
@Environment(\.scenePhase) var scenePhase
var body: some Scene
WindowGroup
w_landingTabs()
.onChange(of: scenePhase) (newScenePhase) in
switch newScenePhase
case .background:
print("[APP] State : Background")
case .inactive:
print("[APP] State : Inactive")
case .active:
print("[APP] State : Active")
@unknown default:
print("[APP] State : Unknown")
应用委托:
import UIKit
import FBSDKCoreKit
let APP_DELEGATE:APP_delegate = UIApplication.shared.delegate as! APP_delegate
class APP_delegate: UIResponder, UIApplicationDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool
ApplicationDelegate.shared.application(
application,
didFinishLaunchingWithOptions: launchOptions
)
print("[APP DELEGATE] MyAppDelegate didFinishLaunchingWithOptions called")
return true
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool
print("[APP DELEGATE] options: [UIApplication.OpenURLOptionsKey : Any] ")
return ApplicationDelegate.shared.application(app, open: url, options: options)
func applicationDidBecomeActive(_ application: UIApplication)
print("[APP DELEGATE] applicationDidBecomeActive")
AppEvents.activateApp()
登录后返回时卡住的屏幕:
【问题讨论】:
【参考方案1】:我找到了解决这个问题的唯一方法。
这部分代码:
let _ = ApplicationDelegate.shared.application(
UIApplication.shared,
open: url,
sourceApplication: nil,
annotation: [UIApplication.OpenURLOptionsKey.annotation])
必须在 onOpenURL
中添加 SwiftUI 视图,在我的情况下,我这样做了:
@main
struct LW_APP: App
@UIApplicationDelegateAdaptor(APP_delegate.self) private var appDelegate
@Environment(\.scenePhase) var scenePhase
var body: some Scene
WindowGroup
if Env.sharedInstance.openApp
w_landingTabs()
.environmentObject(env)
.onOpenURL (url) in
let _ = ApplicationDelegate.shared.application(
UIApplication.shared,
open: url,
sourceApplication: nil,
annotation: [UIApplication.OpenURLOptionsKey.annotation])
.....
【讨论】:
以上是关于SwiftUI Facebook 登录按钮对话框仍然打开?的主要内容,如果未能解决你的问题,请参考以下文章
在 Android 中单击 Facebook 登录按钮后未显示 Facebook 身份验证对话框
swift中的Facebook Native Share对话框
在Facebook对话框中单击关闭(交叉)按钮时未调用onCancel()方法
Facebook 登录 + SwiftUI:登录/注销后不调用 loginButton 和 loginButtonDidLogOut