Reputation: 595
I have the following code:
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)
}
}
It asks the user to log in to Facebook in order to use the app. See images:
And this works fine. However, when the user is logged in, I need the app to get them to a storyboard called "HomeAfterLogIn.storyboard" which looks like this:
However, this is what the screen looks like after the user has signed in:
I asked this question earlier and got this code:
let storyboard = UIStoryboard(name: "HomeAfterLogIn", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "HomeAfterLogIn")
self.navigationController?.pushViewController(vc, animated: true)
I have moved this code around my file and it has made no difference.
Upvotes: 2
Views: 219
Reputation: 6547
Did you already created and setup a facebook app? You can do it here https://developers.facebook.com/apps/:
Take note now of the test email
user and password
so you can debug your app.
In Settings > Basic take note of App ID
and Display Name
to put in your Info.plist later.
Make sure you added to your Podfile:
pod 'FacebookCore'
pod 'FacebookLogin'
Then do a pod install
in terminal (if you haven't already installed both pods).
In your AppDelegate.swift you should have this at least:
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
)
}
}
Right click on Info.plist and Open As > Source Code, paste this before </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>
And substitute {your-app-name}
(once, with the Display App Name you took note before from your Facebook App Dashboard) and {your-app-id}
(twice, as well you took note of this in the first step from your Facebook App Dashboard) in the above snippet.
Now go to your 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)
}
}
Make sure your HomeAfterLogIn.storyboard's ViewController (which I am not sure with what ***ViewController.swift
you linked it) has a HomeAfterLogIn Stoyboard ID
. Eventually it should also be linked to a ***ViewController.swift
file having the same name as the ID for simplicity (HomeAfterLogIn.swift for example).
In my example I called them HomeAfterLogInViewController
as you can see in this screenshot, both the HomeAfterLogInViewController.swift
file and the HomeAfterLogInViewController's Storyboard ID
but I kept the Storyboard file name as HomeAfterLogIn.storyboard
.
If you didn't embed your ViewController
within the Main.storyboard
(I guess you have it there) in a NavigationController
then you have to do it to make it work pushing into the Navigation stack another viewcontroller such as the Home...ViewController.
You can do it like this: Click ViewController from your Main.storyboard and then in the menu up go to Editor > Embed In > Navigation Controller
:
Upvotes: 2