简体   繁体   English

如何将标签文本从iPhone应用程序共享到Facebook?

[英]How to share text of a label from iPhone app to facebook?

I have written a simple Xcode project in swift for my iPhone . 我为iPhone迅速编写了一个简单的Xcode项目。 I am trying to send the text of my label to facebook using facebook share option . 我正在尝试使用facebook share option将标签文本发送到facebook。

Below is my code 下面是我的代码

 import UIKit
import Social

class ViewController: UIViewController {

  @IBOutlet weak var musicDescription: UILabel!

  override func viewDidLoad() {
    super.viewDidLoad()

  }

  @IBAction func faceBookShareButton(sender: UIButton) {
    if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
      let fbShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)

      let text = musicDescription.text

      fbShare.setInitialText(text)


      self.presentViewController(fbShare, animated: true, completion: nil)

    } else {
      let alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)

      alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
      self.presentViewController(alert, animated: true, completion: nil)
    }

  }

} }

But i am not able to see the text of the label in my facebook timeline. 但是我无法在我的Facebook时间线中看到标签的文本。 Someone guide me here . 有人在这里引导我。 Why is the text not posting in facebook ? 为什么文本不发布在Facebook中?

Edit:Update I downloaded FBSDK files and added them to my project . 编辑:更新我下载了FBSDK文件,并将它们添加到我的项目中。 I imported FBSDKShareKit into my ViewController.h file. 我将FBSDKShareKit导入到ViewController.h文件中。

Here is my code 这是我的代码

 @IBAction func faceBookShareButton(sender: UIButton) {



 if UIApplication.sharedApplication().canOpenURL(NSURL(string: "fb:")!) {
  var content: FBSDKShareLinkContent = FBSDKShareLinkContent()

  var Subject: String = String(format: "FBSDKShareKit is an alternative to this issue")

  content.contentTitle = "FBSDKShareKit"
  content.contentDescription = Subject

  var dialog: FBSDKShareDialog = FBSDKShareDialog()
  dialog.fromViewController = self
  dialog.shareContent = content
  dialog.mode = FBSDKShareDialogModeWeb
  dialog.show()
}

I am getting an error "Use of unresolved identifier FBSDKShareDialogModeWeb" . 我收到错误消息“使用未解决的标识符FBSDKShareDialogModeWeb”。 Anyone help please ! 任何人请帮助!

You have to check return value of setInitialText(_:) method. 您必须检查setInitialText(_:)方法的返回值。

That function can return false . 该函数可以返回false Apple document describes following. 苹果文件描述如下。

Return Value 返回值

Returns a Boolean value indicating whether the text was successfully set. 返回一个布尔值,指示是否成功设置了文本。

  1. Using FBSDKShareKit is not an complete alternative but it helps to show your pre-filled content on the share dialog screen. 使用FBSDKShareKit并不是一个完整的替代方法,但是它有助于在共享对话框屏幕上显示您预先填写的内容。

  2. For basic check the following link cocoapods .Open your terminal install cocoapods 基本检查以下链接cocoapods。打开终端安装cocoapods

    $ sudo gem install cocoapods $ sudo gem install cocoapods

    1. open your project directory,like this eg: cd/desktop/TestSwift , 打开您的项目目录, 例如: cd / desktop / TestSwift

    2. Then to create pod file in your project after opening your project directory add the following line pod init this will create a podfile inside your project 然后在打开项目目录后在项目中创建pod文件,添加以下行pod init这将在项目内部创建一个podfile

    3. Open the podfile in TextEdit from finder.to install FBSDK add pod 'FBSDKShareKit' the entire podfile will look like this 从finder中打开TextEdit中的podfile,安装FBSDK,添加pod 'FBSDKShareKit' ,整个podfile如下所示

       platform :ios, '8.0' use_frameworks! target 'TestSwift' do pod 'FBSDKShareKit','~> 4.8.0' pod 'FBSDKCoreKit','~> 4.8.0' end target 'TestSwiftTests' do end target 'TestSwiftUITests' do end 
    4. Now in your terminal inside your project directory type pod install add hit enter this will install all the pod's added in your podfile. 现在,在项目目录内的终端中,输入pod install add hit,这将安装所有在podfile中添加的pod。

       Installing FBSDKShareKit (4.8.0) 

      NOTE: After installing pod in your project .xcworkspace file will be created. 注意:在项目中安装pod之后,将创建.xcworkspace文件。 Instead of using .xcodeproj you have to use .xcworkspace for further process of your project 不必使用.xcodeproj ,而必须使用.xcworkspace进行项目的进一步处理

  3. Since we are using swift we have to add Bridging Header to over project.it helps in adding Objective-C frame work in swift project. 由于我们使用SWIFT,我们必须桥接报头添加到了project.it有助于增加的Objective-C画幅的工作, 迅速的项目。

  4. How to create bridging header: Use Command ⌘ + N the under ios-source choose header file name it as *BridgingHeader.h** select location and then create it. 如何创建桥接头:使用Command ⌘ + Nios-source选择头文件,将其命名为* BridgingHeader.h **选择位置,然后创建它。
  5. Then import your pod-file inside the bridging header file it should look like as follows 然后将您的pod文件导入桥接头文件中,它应该如下所示

     #ifndef BridgingHeader_h #define BridgingHeader_h #import <FBSDKCoreKit/FBSDKCoreKit.h> #import <FBSDKShareKit/FBSDKShareKit.h> #endif /* BridgingHeader_h */ 
  6. Now we have successfully created bridging header to make sure your bridging header is added successfully open Targets then select your target open Build Setting tab in search bar type bridging.In search result under Objective-C BridgingHeader you should have your ProjectName/BridgingHeader.h 现在我们已经成功创建了桥接头,以确保成功添加了桥接头,然后打开Targets然后在搜索栏类型bridged中选择目标打开Build Setting选项卡。在Objective-C BridgingHeader下的搜索结果中,您应该拥有ProjectName / BridgingHeader.h

在此处输入图片说明

  1. Now add the code in your button action as follows: 现在,将代码添加到按钮操作中,如下所示:

      if UIApplication.sharedApplication().canOpenURL(NSURL(string: "fb:")!) { let content : FBSDKShareLinkContent = FBSDKShareLinkContent() content.contentURL = NSURL(string: "https://google.com") content.contentTitle = "Test" content.contentDescription = "FBSDKShareKit is an alternative to this issue" content.imageURL = NSURL(string:"https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg") let ShareKit: FBSDKShareDialog = FBSDKShareDialog() ShareKit.fromViewController = self; ShareKit.shareContent = content ShareKit.mode = FBSDKShareDialogMode.FeedBrowser ShareKit.show() } 
  2. Use of unresolved identifier FBSDKShareDialogModeWeb to fix this replace dialog.mode as follows 使用未解析的标识符FBSDKShareDialogModeWeb来修复此替换dialog.mode ,如下所示

      ShareKit.mode = FBSDKShareDialogMode.FeedBrowser 

I'am using .FeedBrowser,because is working fine for me. 我正在使用.FeedBrowser,因为它对我来说工作正常。

FBShareDialogue: FBShareDialogue:

在此处输入图片说明

FBTimeline: FB时间轴:

在此处输入图片说明

Now we got everything in timeline.Title,Description,Image,SiteURL. 现在,我们在时间线中获得了所有内容。标题,描述,图像,SiteURL。

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

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