私は次のコードを持っています:
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/:
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).
私の例でHomeAfterLogInViewController
は、このスクリーンショットにHomeAfterLogInViewController.swift
示されているように、ファイルとHomeAfterLogInViewControllerの両方を呼び出しましたStoryboard ID
が、ストーリーボードのファイル名はHomeAfterLogIn.storyboard
。のままにしました。
あなたを埋め込むなかった場合はViewController
内Main.storyboard
に(私はあなたがそれをそこに持っていると思います)NavigationController
、あなたはそれがナビゲーションにプッシュ動作させるためにそれをしなければならない、このようなホーム...のViewControllerとして別のViewControllerをスタック。
次のように実行できます。Main.storyboardからViewControllerをクリックし、メニューで次の場所に移動しEditor > Embed In > Navigation Controller
ます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加