简体   繁体   English

"Facebook SDK Swift - 使用未声明的标识符"

[英]Facebook SDK Swift - Use of undeclared identifier

I am getting Use of undeclared identifier for every Facebook Object I use我正在为我使用的每个 Facebook 对象使用Use of undeclared identifier

Following this tutorial: TUTORIAL: HOW TO SHARE IN FACEBOOK SDK 4.1.X FOR SWIFT遵循本教程: 教程:如何在 FACEBOOK SDK 4.1.X 中共享 SWIFT

But I've got the following error:但我有以下错误:

在此处输入图像描述

I've added Facebook framework via cocoapods:我通过 cocoapods 添加了 Facebook 框架:

pod 'FBSDKCoreKit'
pod 'FBSDKShareKit'

And it was installed successfully并且安装成功

在此处输入图像描述

I've added bridging header我添加了桥接头

#ifndef Bridging_Header_h
#define Bridging_Header_h

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKShareKit/FBSDKShareKit.h>

#endif /* Bridging_Header_h */

The bridging header is connected:桥接头连接:

在此处输入图像描述

I've configured my .plist我已经配置了我的 .plist

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>fb*****</string>
            </array>
        </dict>
    </array>
    <key>FacebookAppID</key>
    <string>*****</string>
    <key>FacebookDisplayName</key>
    <string>*****</string>
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>facebook.com</key>
            <dict>
                <key>NSIncludesSubdomains</key> <true/>
                <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/>
            </dict>
            <key>fbcdn.net</key>
            <dict>
                <key>NSIncludesSubdomains</key> <true/>
                <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>  <false/>
            </dict>
            <key>akamaihd.net</key>
            <dict>
                <key>NSIncludesSubdomains</key> <true/>
                <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/>
            </dict>
        </dict>
    </dict>

Here is the code:这是代码:

let content : FBSDKShareLinkContent = FBSDKShareLinkContent()
        content.contentURL = NSURL(string: "<INSERT STRING HERE>")
        content.contentTitle = "<INSERT STRING HERE>"
        content.contentDescription = "<INSERT STRING HERE>"
        content.imageURL = NSURL(string: "<INSERT STRING HERE>")

        let button : FBSDKShareButton = FBSDKShareButton()
        button.shareContent = content
        button.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 100) * 0.5, 50, 100, 25)
        self.view.addSubview(button)

If you use Pods and your project is on Swift you don't need to import headers from pods to Bridging_Header_h It's suficient to import needed SDK to you swift file like: 如果您使用Pod并且您的项目在Swift上,则不需要将标题从pod导入Bridging_Header_h将所需的SDK导入到swift文件中是足够的,例如:

import FBSDKCoreKit
import FBSDKLoginKit
import FBSDKShareKit

First step is create Podfile, for example: 第一步是创建Podfile,例如:

use_frameworks!
pod 'ChameleonFramework/Swift'
pod 'GBDeviceInfo'

Save file and install or update pods by command: 按命令保存文件并安装或更新pod:

pod install / pod update

Next, you should add in general settings framework: 接下来,您应该添加一般设置框架:

在此输入图像描述

For Swift users: Facebook Swift SDK 0.7.0 and 0.8.0 seem to be completely FUBAR, there is no way to get imports to work properly even on a completely new project. 对于Swift用户:Facebook Swift SDK 0.7.00.8.0似乎完全是FUBAR,即使在一个全新的项目中也无法使导入正常工作。

Revert to 0.6.0 by specifying the following in your Podfile , and the tutorials and imports will work again: 通过在Podfile指定以下内容恢复为0.6.0 ,教程和导入将再次起作用:

pod 'FacebookCore', '~> 0.6.0'
pod 'FacebookLogin', '~> 0.6.0'
pod 'FacebookShare', '~> 0.6.0' 

I was stuck at it too until I found that it is a debug and release issue.我也被困住了,直到我发现这是一个调试和发布问题。

instead of using而不是使用

#ifndef Bridging_Header_h
#define Bridging_Header_h

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKShareKit/FBSDKShareKit.h>

#endif /* Bridging_Header_h */

You should use你应该使用

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKShareKit/FBSDKShareKit.h>

#ifndef Bridging_Header_h
#define Bridging_Header_h

#endif /* Bridging_Header_h */ 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM