简体   繁体   English

将FacebookSDK导入Swift应用

[英]Importing FacebookSDK into a Swift app

I'm using the FacebookSDK.framework, freshly downloaded an hour or two ago (version 3.17, I think), in a new iOS app written in Swift. 我正在使用FacebookSDK.framework,它在一两个小时前刚刚下载完毕(我认为是3.17版),它是用Swift编写的新iOS应用程序。 I have Xcode 6.01. 我有Xcode 6.01。 I can build and successfully run the HelloFacebookSample project from the SDK folder on a device. 我可以从设备上的SDK文件夹构建并成功运行HelloFacebookSample项目。 But that is in Objective-C. 但这是在Objective-C中。 I don't see any Swift samples. 我没有看到任何Swift样本。

I am unable to import the SDK into a Swift source unit via import FacebookSDK, which gives the error: No such module. 我无法通过导入FacebookSDK将SDK导入到Swift源单元中,这给出了错误:没有这样的模块。

If I add #import <FacebookSDK/FacebookSDK.h> to the project's bridging header, it cannot find the header file. 如果我将#import <FacebookSDK / FacebookSDK.h>添加到项目的桥接头文件中,它将找不到头文件。 The framework is visible among the other frameworks used by the project, and I can see the headers. 该框架在项目使用的其他框架中可见,并且可以看到标题。 The framework search path in the build settings looks reasonable. 构建设置中的框架搜索路径看起来很合理。 It includes ~/Documents/FacebookSDK. 它包括〜/ Documents / FacebookSDK。

I've seen this behavior before with another third-party SDK framework. 在使用另一个第三方SDK框架之前,我已经看到了此行为。 I am successfully linking against a framework of my own, but that is built from source. 我已成功链接到我自己的框架,但这是从源代码构建的。

Are there any further preparatory steps required for using the FacebookSDK with a Swift app? 将FacebookSDK与Swift应用程序结合使用还需要其他准备步骤吗?

Thanks. 谢谢。

Like some people mentioned in the comments to your question, I would suggest you use CocoaPods. 就像在您的问题的评论中提到的某些人一样,我建议您使用CocoaPods。 I switched over and its been great! 我切换了,它很棒! I apologize in advance that this will be a long answer, but I tried to be thorough. 我预先向您道歉,这将是一个漫长的回答,但是我试图做到彻底。

1) Install CocoaPods. 1)安装CocoaPods。 Should be no problem I don't think (instructions here under the 'install' tab http://cocoapods.org ) 我认为应该没有问题(这里的“安装”标签下的说明http://cocoapods.org

2) Use CocoaPods to handle the Facebook SDK dependency. 2)使用CocoaPods处理Facebook SDK依赖性。 This means you do not need to download the Facebook SDK Framework and drag it into your project (if you already downloaded it, you can delete it now). 这意味着你不需要下载了Facebook SDK框架,并将其拖动到您的项目(如果你已经下载了它,你就可以将其删除)。 To use CocoaPods: 要使用CocoaPods:

-using Mac Terminal, cd to your project directory -使用Mac Terminal,将cd转到您的项目目录

-use the 'pod init' command, which creates PodFile in your project directory and sets it up. -使用“ pod init”命令,该命令会在您的项目目录中创建PodFile并进行设置。 If you ls in your project directory now, you should see a file named PodFile 如果现在在项目目录中,请看到一个名为PodFile的文件。

-open PodFile and add the Facebook SDK dependency. -打开PodFile并添加Facebook SDK依赖项。 Here is what my PodFile for my project using Facebook SDK looks like: 这是我使用Facebook SDK的项目的PodFile的样子:

platform :ios, '7.0'

target 'RatingApp' do
    pod 'Facebook-iOS-SDK', '~> 3.22'
end

Note that for other dependencies you want to add, you can look them up on the CocoaPods website. 请注意,对于要添加的其他依赖项,可以在CocoaPods网站上查找它们。 The homepage should have a big SEARCH field where you can type in the name of the dependency. 主页应该有一个很大的SEARCH字段,您可以在其中键入依赖项的名称。 For example, type in 'Facebook' and the first result is 'Facebook-iOS-SDK', with '3.23.0' to the right of it (that is the most recent version as of this post). 例如,键入“ Facebook”,第一个结果是“ Facebook-iOS-SDK”,其右边是“ 3.23.0”(这是本文中的最新版本)。 To the right of the version number is a clipboard icon. 版本号右边是剪贴板图标。 If you scroll over it, you see what you should input to your PodFile. 如果滚动它,您会看到应该输入到PodFile的内容。 Note that scrolling over it shows pod 'Facebook-iOS-SDK', '~> 3.22' This is exactly what I have in my PodFile. 请注意,滚动到它会显示pod'Facebook-iOS-SDK','〜> 3.22'。这正是我在PodFile中拥有的。

-close your PodFile, and run 'pod install' -关闭您的PodFile,然后运行“ pod install”

-open YourProject.xcworkspace, where YourProject is the name of your project. -打开YourProject.xcworkspace,其中YourProject是您的项目的名称。 Now you can start working! 现在您可以开始工作了! do not use YourProject.xcodeproj anymore. 不要再使用YourProject.xcodeproj。

These instructions are also here: http://guides.cocoapods.org/using/using-cocoapods.html 这些说明也位于此处: http : //guides.cocoapods.org/using/using-cocoapods.html

3) Lastly, you need the bridging header. 3)最后,您需要桥接头。 The Facebook SDK is still in Objective-C (as of now), and the bridging header lets you use Objective-C frameworks in your Swift project. Facebook SDK仍在Objective-C中(到目前为止),桥接头使您可以在Swift项目中使用Objective-C框架。

-create a new file in your project. -在您的项目中创建一个新文件。 select 'Header File' for file type 选择“头文件”作为文件类型

-in that file, make your import. -在该文件中,进行导入。 Here is what my file looks like, which I named 'Bridging-Header.h' 这是我的文件的样子,我将其命名为“ Bridging-Header.h”

#ifndef Bridging_Header_h
#define Bridging_Header_h

#import <FacebookSDK/FacebookSDK.h>

#endif

-tell your Swift project to use your bridging header file. 告诉您的Swift项目使用桥接头文件。 Click on your project target file, which is the one with the blue paper and 'A' icon (hopefully you know what this is). 单击项目目标文件,该文件带有蓝纸和“ A”图标(希望您知道这是什么)。 You should see 6 tabs (as of XCode 6). 您应该看到6个选项卡(从XCode 6开始)。 These are 'General', 'Capabilities', 'Info', 'Build Settings', 'Build Phases', and 'Build Rules'. 这些是“常规”,“功能”,“信息”,“构建设置”,“构建阶段”和“构建规则”。 Go to Build Settings, and go all the way to near the bottom under 'Swift Compiler - Code Generation'. 转到“构建设置”,然后一直到“快速编译器-代码生成”下的底部。 Set 'Install Objective-C Compatibility Header' to Yes. 将“安装Objective-C兼容性标头”设置为“是”。 For 'Objective-C Bridging Header', type YourProject/BridgingHeaderFileName.h, where YourProject is the name of your project and BridgingHeaderFileName is the name of the bridging header file you created. 对于“ Objective-C桥接头”,键入YourProject / BridgingHeaderFileName.h,其中YourProject是项目的名称,而BridgingHeaderFileName是您创建的桥接头文件的名称。

You should be all set now to use the Facebook SDK. 您现在应该已经准备就绪,可以使用Facebook SDK。 The benefit is that now you don't need to make any Facebook import statements in any of your files (I actually don't make any import statements at all). 好处是,现在您无需在任何文件中进行任何Facebook导入声明(我实际上根本不需要进行任何导入声明)。 If you need another SDK, you can add it like the Facebook one using CocoaPods (assuming it is available through CocoaPods). 如果您需要另一个SDK,则可以使用CocoaPods像Facebook一样添加它(假定可通过CocoaPods获得)。 So to me, the nice thing is you never have to make import statements in files, and you don't need to download any SDKs yourself = ) 因此,对我来说,好事是您不必在文件中创建import语句,也不需要自己下载任何SDK =)

With cocoapods : 与cocoapods:

Use facebook sdk in iOS. 在iOS中使用facebook sdk。

Follow this tutorial to configure cocoapods with swift. 请按照本教程来快速配置cocoapods。

Then add the following in your podfile. 然后在您的podfile中添加以下内容。

platform :ios, "8.0"
use_frameworks!

target 'ProjectName' do
pod 'FBSDKCoreKit'
pod 'FBSDKLoginKit'
pod 'FBSDKShareKit'
pod 'FBSDKMessengerShareKit'
end

Without cocoapods : 没有cocoapods:

Drag drop FacebookSDK ShareKit, CoreKit, LoginKit and Bolts frameworks in your project's Framework folder and make sure you "Copy" (by clicking check box while copying) those frameworks in your project and add the frameworks in your bridging header file. 将FacebookSDK ShareKit,CoreKit,LoginKit和Bolts框架拖放到项目的Framework文件夹中,并确保您在项目中“复制”(通过在复制时单击复选框)并将这些框架添加到桥接头文件中。

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

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