将 Facebook SDK 与 Xcode Swift 项目集成 - AppDelegate 错误?

Posted

技术标签:

【中文标题】将 Facebook SDK 与 Xcode Swift 项目集成 - AppDelegate 错误?【英文标题】:Integrating Facebook SDK with Xcode Swift project - AppDelegate errors? 【发布时间】:2019-02-09 19:45:25 【问题描述】:

我正在尝试根据此处的指南集成 Facebook SDK: https://developers.facebook.com/docs/swift/getting-started

我已经完成了 CocoaPods 步骤,现在正处于“连接您的应用程序代表”的阶段。但是我从 Xcode 中收到错误,阻止编译: 使用未解析的标识符:SDKApplicationDelegate 未解析标识符的用户:SDKApplicationDelegate

这是我的 AppDelegate。我是否缺少导入声明或其他内容?

    //
//  AppDelegate.swift
//  TestFBSDK
//
//  Created by laurie hocking on 09/02/2019.
//  Copyright © 2019 laurie hocking. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate 

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool 
        SDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
        return true
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool 
        return SDKApplicationDelegate.sharedInstance().application(app, open: url, options: options)
    
    func applicationWillResignActive(_ application: UIApplication) 
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    

    func applicationDidEnterBackground(_ application: UIApplication) 
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    

    func applicationWillEnterForeground(_ application: UIApplication) 
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    

    func applicationDidBecomeActive(_ application: UIApplication) 
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    

    func applicationWillTerminate(_ application: UIApplication) 
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    



【问题讨论】:

【参考方案1】:

我只能认为我一直尝试使用 Cocoapods 的方式不正确。当我最终手动导入框架时,它工作正常,如下所示:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate 

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool 
        FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
        incrementAppRuns()
        return true
    

    func applicationWillResignActive(_ application: UIApplication) 
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    

    func applicationDidEnterBackground(_ application: UIApplication) 
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    

    func applicationWillEnterForeground(_ application: UIApplication) 
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
                incrementAppRuns()
    

    func applicationDidBecomeActive(_ application: UIApplication) 
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    

    func applicationWillTerminate(_ application: UIApplication) 
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool 
        return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, options: options)
    

【讨论】:

【参考方案2】:

您应该导入 SDK:

import FacebookCore

我认为电话应该是:

SDKApplicationDelegate.shared

不是

SDKApplicationDelegate.sharedInstance()

希望这会有所帮助。

【讨论】:

它确实接受了 *.shared 的机会,我不明白为什么 Facebook 开发者网站上的指南不同?! developers.facebook.com/docs/swift/getting-started

以上是关于将 Facebook SDK 与 Xcode Swift 项目集成 - AppDelegate 错误?的主要内容,如果未能解决你的问题,请参考以下文章

链接器命令失败,退出代码 1、XCode 4.5、Facebook SDK 3.1

Facebook IOS SDK 3.1.1 与 Xcode 4.5.2 错误

无法使用 Swift 在 Xcode 中使用 Parse 1.7.2 和 Facebook SDK 4.1 创建新用户

发布前xcode自动登录中的Facebook SDK Singleton?

iOS 8 中的 Facebook SDK 与 Swift/Xcode beta 5 返回 accessToken 但更新后没有用户 ID

facebook SDK3.1 和 Xcode 4.5 链接器错误