ios - 使用 Facebook SDK 登录从一个情节提要转到另一个情节提要
问题描述
我有以下代码:
import UIKit
import FacebookLogin
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
var loginButton = FBLoginButton(permissions: [ .publicProfile ])
let screenSize:CGRect = UIScreen.main.bounds
let screenHeight = screenSize.height // real screen height
//let's suppose we want to have 10 points bottom margin
let newCenterY = screenHeight - loginButton.frame.height - 20
let newCenter = CGPoint(x: view.center.x, y: newCenterY)
loginButton.center = newCenter
view.addSubview(loginButton)
loginButton = FBLoginButton(permissions: [ .publicProfile, .email, .userFriends ])
if AccessToken.current != nil
{
}
let storyboard = UIStoryboard(name: "HomeAfterLogIn", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "HomeAfterLogIn")
self.navigationController?.pushViewController(vc, animated: true)
}
}
它要求用户登录 Facebook 才能使用该应用程序。见图片:
这很好用。但是,当用户登录时,我需要应用程序将他们带到一个名为“HomeAfterLogIn.storyboard”的故事板,如下所示:
但是,这是用户登录后屏幕的样子:
我之前问过这个问题并得到了这个代码:
let storyboard = UIStoryboard(name: "HomeAfterLogIn", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "HomeAfterLogIn")
self.navigationController?.pushViewController(vc, animated: true)
我已经在我的文件周围移动了这段代码,它没有任何区别。
解决方案
您是否已经创建并设置了 Facebook 应用程序?你可以在这里https://developers.facebook.com/apps/:
- 创建应用程序 > 设置显示名称 > 创建应用程序 ID
- 然后你需要去设置>基本>向下滚动直到你看到添加平台并选择iOS
- 放置您的 iOS 项目的包 ID(您可以在 Xcode > Project > Target > Bundle Identifier 中找到)并启用 Single Sign ON 然后保存更改
- 角色 > 测试用户 > 添加 > 创建测试用户 > 编辑 > 更改名称/密码并输入您会记住的密码
现在记下测试email
用户,password
这样您就可以调试您的应用程序。在 Settings > Basic 中记下App ID
并Display Name
稍后放入您的 Info.plist。
确保您已添加到您的 Podfile:
pod 'FacebookCore'
pod 'FacebookLogin'
然后做一个pod install
终端(如果你还没有安装两个吊舱)。
在你的 AppDelegate.swift 你至少应该有这个:
import UIKit
import FacebookLogin
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
ApplicationDelegate.shared.application(
application,
didFinishLaunchingWithOptions:
launchOptions
)
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
return ApplicationDelegate.shared.application(
app,
open: url,
options: options
)
}
}
右键单击 Info.plist 并打开为 > 源代码,将其粘贴到之前</dict>
:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb{your-app-id}</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>{your-app-id}</string>
<key>FacebookDisplayName</key>
<string>{your-app-name}</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-share-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
并在上面的代码段中替换{your-app-name}
(一次,使用您之前从 Facebook 应用程序仪表板记下的显示应用程序名称)和{your-app-id}
(两次,以及您在第一步中从 Facebook 应用程序仪表板记下的)。
现在转到您的 ViewController.swift:
import UIKit
import FacebookLogin
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if AccessToken.current != nil {
// Already logged-in
// Redirect to Home View Controller
goToHome()
}
// Add LoginButton
let loginButton = FBLoginButton(permissions: [ .publicProfile, .email, .userFriends ])
let screenSize:CGRect = UIScreen.main.bounds
let screenHeight = screenSize.height // real screen height
//let's suppose we want to have 10 points bottom margin
let newCenterY = screenHeight - loginButton.frame.height - 20
let newCenter = CGPoint(x: view.center.x, y: newCenterY)
loginButton.center = newCenter
view.addSubview(loginButton)
// Triggered after every successfully login / logout
NotificationCenter.default.addObserver(forName: .AccessTokenDidChange, object: nil, queue: OperationQueue.main) { [weak self] _ in
if AccessToken.current != nil {
// Successfully just Logged in
// Redirect to Home View Controller
self?.goToHome()
} else {
// Successfully just Logged out
}
}
}
func goToHome() {
let storyboard = UIStoryboard(name: "HomeAfterLogIn", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "HomeAfterLogInViewController") // I called mine like that (check screenshot below)
self.navigationController?.pushViewController(vc, animated: true)
}
}
确保您的 HomeAfterLogIn.storyboard 的 ViewController (我不确定***ViewController.swift
您链接的内容)具有 HomeAfterLogIn Stoyboard ID
。最后,为了简单起见,它还应该链接到***ViewController.swift
与 ID 同名的文件(例如 HomeAfterLogIn.swift)。
在我的示例中,HomeAfterLogInViewController
正如您在此屏幕截图中看到的那样,我将它们称为HomeAfterLogInViewController.swift
文件和 HomeAfterLogInViewController,Storyboard ID
但我将 Storyboard 文件名保留为HomeAfterLogIn.storyboard
.
如果你没有将你ViewController
的Main.storyboard
(我猜你有它)嵌入到 a 中,NavigationController
那么你必须这样做才能使其工作推入导航堆栈另一个视图控制器,例如 Home...ViewController。
您可以这样做:从 Main.storyboard 单击 ViewController,然后在菜单中转到Editor > Embed In > Navigation Controller
:
推荐阅读
- java - 如何使用 Searchablespinner 功能?
- python - 我如何处理可读但不接收任何内容的套接字?
- android - 使用 RxJava 从两个 api 获取数据
- swift - 滚动视图对齐
- javascript - 无法读取 ArcGIS API 中未定义的属性“人口统计”
- python - dask.dataframe.tail() 返回空数据框
- while-loop - 第一个 `readLine` 在 Nim-lang 中的 `case - of` 控制流中被跳过
- java - 有没有办法从表格中列出列表?
- directory - delphi DirectoryListBox 获取选中的目录名
- firebase - 不同地区的多个 Firebase 环境