Ich habe das Swift SDK von Facebook über Cocoapods installiert:
pod 'FacebookCore', :git => "https://github.com/facebook/facebook-sdk-Swift"
pod 'FacebookLogin', :git => "https://github.com/facebook/facebook-sdk-Swift"
Ich habe die Anmeldeanweisungen von Swift SDK von Facebook ( https://developers.facebook.com/docs/Swift/login ) befolgt, kann jedoch nicht funktionieren.
In meinem View Controller habe ich Folgendes:
@objc fileprivate func facebookSignIn() {
let loginManager = LoginManager()
print("LOGIN MANAGER: \(loginManager)")
loginManager.logIn([ .publicProfile, .email ], viewController: self) { loginResult in
print("LOGIN RESULT! \(loginResult)")
switch loginResult {
case .failed(let error):
print("FACEBOOK LOGIN FAILED: \(error)")
case .cancelled:
print("User cancelled login.")
case .success(let grantedPermissions, let declinedPermissions, let accessToken):
print("Logged in!")
print("GRANTED PERMISSIONS: \(grantedPermissions)")
print("DECLINED PERMISSIONS: \(declinedPermissions)")
print("ACCESS TOKEN \(accessToken)")
}
}
}
"Login Manager: usw." Wenn ich auf die Schaltfläche klicke, wird die Webansicht geöffnet. Ich kann mich über Facebook anmelden und dann wird die Webansicht leer und nichts passiert. Die App wird in meinen Facebook-Apps angezeigt. Es wurde also eine Autorisierung vorgenommen. Wenn ich jedoch in der Webansicht auf die Schaltfläche "Fertig" klicke, werden als Rückruf .cancelled
und "Anmeldung vom Benutzer abgebrochen" angezeigt. wird gedruckt.
Ich habe die Berechtigung "Schlüsselbund" zu meinen Berechtigungen hinzugefügt und meine info.plist aktualisiert:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string> [ FB STRING ] </string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>[ APP ID ]</string>
<key>FacebookDisplayName</key>
<string>[ APP NAME ]</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
Ich habe sogar meine NSAppTransportSecurity
-Einstellungen geändert.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Ich weiß, dass dies ein Duplikat von iOS 9 ist. Facebook Login-Simulator -canOpenURL: Fehler bei URL: "fbauth2: ///" - error: "(null)" und Verwendung von Facebook iOS SDK unter iOS 10 aber es scheint keine der Lösungen dort zu funktionieren, es sei denn, ich habe etwas verpasst? Anregungen werden gebeten.
Einige Fehler werden auch gedruckt:
[App] if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction
[error] error: (6922) I/O error for database at /var/mobile/Containers/Data/Application/F3D8E126-B0CC-4C06-81A5-D7A7F849B3BD/Documents/OfflineModel2.sqlite. SQLite error code:6922, 'disk I/O error'
Und wenn ich die FB-App nicht auf meinem Gerät installiert habe, bekomme ich:
-canOpenURL: failed for URL: "fbauth2:/" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"
Sie müssen die Methoden didfinishlaunching und openurl AppDelegate durch folgende ersetzen:
public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool
{
return SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
}
public func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
{
return SDKApplicationDelegate.shared.application(app, open: url, options: options)
}
Swift 3 und Swift 4
fügen Sie dies Ihrer AppDelegate.Swift hinzu
import FBSDKCoreKit
import FBSDKLoginKit
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, options: options)
}
Die FBSDK-Bibliotheken sind veraltet. Die neueren Bibliotheken finden Sie im Github-Repository von Facebook .
Implementieren Sie für diesen bestimmten Fehler die folgenden Anwendungsmethoden.
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return SDKApplicationDelegate.shared.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return SDKApplicationDelegate.shared.application(app, open: url, options: options)
}
Swift 3.0
auf Ihrer AppDelete.Swift:
import FBSDKCoreKit
import FBSDKLoginKit
in deinem:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
hinzufügen:
FBSDKApplicationDelegate .sharedInstance() .application(application, didFinishLaunchingWithOptions: launchOptions)
erstellen Sie dann eine neue Funktion wie folgt:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, options: options)
}
Cocoapods in Swift 3.0
Installieren Sie Kokosapoden
pod 'FacebookCore'
pod 'FacebookLogin'
in AppDelegate.Swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
und abgeschrieben Callback-Methode
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, open: url as URL!, sourceApplication: sourceApplication, annotation: annotation)return FBSDKApplicationDelegate.sharedInstance().application(application, open: url as URL!, sourceApplication: sourceApplication, annotation: annotation)
}
in Ihrem viewController
import FacebookCore
import FacebookLogin
import FBSDKLoginKit
schreiben Sie anschließend in Ihrem fblogin-Button diesen Code auf
@IBAction func faceBookLoginBtnAction(_ sender: Any) {
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result!
if fbloginresult.grantedPermissions != nil {
if(fbloginresult.grantedPermissions.contains("email")) {
if((FBSDKAccessToken.current()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil){
self.dict = result as! [String : AnyObject]
print(result!)
print(self.dict)
}
})
}
}
}
}
}
}
und noch eine wichtige Sache, fügen Sie Ihre Facebook-ID in Ihr Projekt ein. Legen Sie im Abschnitt Projekt> Ziel> Info> URL-Typen die richtige Facebook-ID und die URL-Schemas fest.
Als Erstes müssen Sie eine Anwendung erstellen, um das Facebook-Login in Ihrer Anwendung zu verwenden. Sie können eine App auf Facebook von hier erstellen.
Sie müssen URL types
, FacebookAppID
, FacebookDisplayName
und LSApplicationQueriesSchemes
in Ihren info.plist
setzen.
Fügen Sie das Login-Framework von Facebook manuell hinzu, oder verwenden Sie Kakaohülsen wie folgt:.
pod 'FacebookCore' //latest one
pod 'FacebookLogin' // for fb login
pod 'FacebookShare' //for sharing on fb
und dann pod install
zum Installieren von Pods für Ihre Anwendung.
import FacebookCore
in Ihrem Anwendungsdelegierten, um das Facebook-Login zu verwenden
Richten Sie folgende Elemente in der AppDelegate.Swift
-Datei ein:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
return SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if url.absoluteString.contains(<Your FBAppID>) {
return SDKApplicationDelegate.shared.application(app, open: url, options: options)
} else {
debugPrint("Error in openURL from AppDelegate Facebook Sign in")
}
return true
}
Nun haben Sie die Facebook-Anmeldung für Ihre App erfolgreich konfiguriert.
In Ihrem View Controller müssen Sie Folgendes tun:
import FacebookLoginKit
Führen Sie in Ihrer IBAction-Methode Folgendes aus:
@IBAction func btnFacebookSignInTapped(_ sender: UIButton) {
let fbManager = FBSDKLoginManager()
fbManager.logIn(withReadPermissions: ["public_profile", "email"], from: self) { (result, error) in
if let loginResult = result, error == nil {
if let permissions = loginResult.grantedPermissions {
if permissions.contains("email") || permissions.contains("public_profile") {
if FBSDKAccessToken.current() != nil {
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).start(completionHandler: { (connection, graphResult, error) in
if error == nil {
debugPrint("Result: \(graphResult!)")
if let data = graphResult as? [String: Any] {
print("User details from FB: \(data)")
}
}
})
} else {
print("Invalid FB access token");
}
}
} else {
print("Fail to login using facebook.")
}
} else {
print("Error in facebook login")
}
}
}
Auf diese Weise können Sie sich bei Facebook anmelden.
Antworten hier empfehlen die folgende Implementierung von application(_,didFinishLaunchingWithOptions:)
:
public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
return SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
}
Beachten Sie jedoch, dass offizielle Dokumentation den Rückgabewert von nicht verwendet
SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
als Rückgabewert von application(_,didFinishLaunchingWithOptions:)
.
Das mag zwar unwichtig erscheinen, aber ich hatte große Probleme bei der Integration von Facebook-SDK mit Branch-Deep-Linking, da genau diese Zeile Probleme bereitet. Wenn Sie später einige unnötige Probleme vermeiden möchten, verwenden Sie die folgende Lösung:
public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool
{
SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
// always return true
return true
}
public func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
{
return SDKApplicationDelegate.shared.application(app, open: url, options: options)
}
aktualisiert für Swift 3:
// In Ihrer AppDelegate ():
// Schritt 1:
import FacebookCore
// Schritt - 2: In didFinishLaunchingWithOptions ()
// FB Settings
SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
// Instanzmethode ():
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
print("URL AbsoluteString = \(url.absoluteString)")
if url.absoluteString.contains("fb1279300242197274") {
return SDKApplicationDelegate.shared.application(app,
open: url,
options: options)
}
else {
//return PDKClient.sharedInstance().handleCallbackURL(url)
print("Error while open URL from app delegate facebook sign in")
}
return true
}
Andere Möglichkeiten, dies zu tun . Hoffe, dass dieses Video für Sie hilfreich ist https://youtu.be/bOS4jqRc8Io
Inhalte in diesem Video: Lernprogramm für die Integration von Facebook SDK mit Swift lastest 2018 Xcode Neues Projekt einrichten Swift-App für iOS-App mit Facebook aktivieren Tutorial Facebook-Anmeldefunktion in der iOS-App Hinzufügen von Facebook zur Swift-iOS-App Facebook-Anmeldung in der iOS-App
Schritte: 1. Erstellen Sie ein einfaches, einfaches Swift Xcode-Projekt. 2- Melden Sie sich auf der Facebook-Entwicklungsseite an, um denselben Namen zu erstellen. APP 3- Fügen Sie das Login mit der Facebook-Schaltfläche, den Login- und Logout-Methoden hinzu , 5- Führen Sie die Anwendung aus und testen Sie mit dem Simulator