我有以下代码:
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)
我已将这段代码移到我的文件中,并且没有影响。
答案 0 :(得分:2)
您已经创建并设置了一个Facebook应用吗?您可以在这里https://developers.facebook.com/apps/进行操作:
现在请注意测试email
用户和password
的用户,以便您调试应用程序。
在“设置”>“基本”中记下App ID
和Display Name
,以便稍后将其放入Info.plist。
确保已将其添加到Podfile :
pod 'FacebookCore'
pod 'FacebookLogin'
然后在终端机中进行pod install
(如果尚未安装两个Pod的话)。
在您的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 App Dashboard上记下的Display App Name代替)和{your-app-id}
(两次,也是从第一步开始记下此记号)上面的代码片段中的“ Facebook App Dashboard”。
现在转到您的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
。最终,为了简化起见,它也应该链接到一个与ID相同名称的***ViewController.swift
文件(例如HomeAfterLogIn.swift)。
在我的示例中,我将其命名为HomeAfterLogInViewController
,在此屏幕快照中可以看到HomeAfterLogInViewController.swift
文件和HomeAfterLogInViewController的Storyboard ID
,但我将Storyboard文件名保留为{{1} }。
如果您没有将HomeAfterLogIn.storyboard
嵌入ViewController
的{{1}}(我想您已经在其中)中,则必须这样做才能使其工作到导航堆栈另一个ViewController,例如Home ... ViewController。
您可以这样操作:在Main.storyboard中单击ViewController,然后在菜单中转到Main.storyboard
: