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

时间:2016-05-27 11:39:42

标签: iphone swift facebook uilabel xcode7

我已经在swift中为我的iPhone编写了一个简单的Xcode项目。我正在尝试使用Facebook共享选项将我的标签文本发送到Facebook。

以下是我的代码

 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)
    }

  }

}

但我无法在facebook时间线上看到标签的文字。有人在这里指导我。为什么文字没有在Facebook上发布?

编辑:更新 我下载了FBSDK文件并将它们添加到我的项目中。我将FBSDKShareKit导入到我的ViewController.h文件中。

这是我的代码

 @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()
}

我收到错误"使用未解析的标识符FBSDKShareDialogModeWeb" 。 任何人都可以帮忙!

2 个答案:

答案 0 :(得分:0)

您必须检查setInitialText(_:)方法的返回值。

该函数可以返回false。 Apple文档描述了以下内容。

  

返回值

     

返回一个布尔值,指示文本是否已成功设置。

答案 1 :(得分:0)

  1. 使用 FBSDKShareKit 不是一个完整的选择,但它有助于在共享对话框屏幕上显示预先填写的内容。

  2. 基本检查以下链接cocoapods。打开终端安装cocoapods

      

    $ sudo gem install cocoapods

    1. 打开您的项目目录,例如例如: cd / desktop / TestSwift

    2. 然后在打开项目目录后在项目中创建pod文件,添加以下行pod init这将在项目中创建一个podfile

    3. 从finder中打开 TextEdit 中的podfile。安装FBSDK add 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. 现在,在项目目录中的终端类型pod install中添加命中输入,这将安装podfile中添加的所有pod。

      Installing FBSDKShareKit (4.8.0)
      

      注意:在项目中安装pod后,将创建.xcworkspace文件。您必须使用.xcodeproj来进一步处理项目,而不是使用.xcworkspace

  3. 由于我们使用swift,我们必须在项目中添加桥接标题。有助于在 swift <中添加 Objective-C 框架工作/ strong> project。

  4. 如何创建桥接标头:使用Command ⌘ + Nios-source选择标题文件名称* BridgingHeader.h **选择位置然后创建它。
  5. 然后在桥接头文件中导入您的pod文件,它应如下所示

    #ifndef BridgingHeader_h
    #define BridgingHeader_h
    
    #import <FBSDKCoreKit/FBSDKCoreKit.h>
    #import <FBSDKShareKit/FBSDKShareKit.h>
    
    #endif /* BridgingHeader_h */
    
  6. 现在,我们已成功创建桥接标头,以确保成功添加桥接标头Targets,然后在搜索栏类型桥接中选择目标开放Build Setting标签。在<搜索结果中< strong> Objective-C BridgingHeader 您应该拥有 ProjectName / BridgingHeader.h

  7. enter image description here

    1. 现在在按钮操作中添加代码,如下所示:

       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. 使用未解析的标识符FBSDKShareDialogModeWeb 来修复此替换dialog.mode,如下所示

       ShareKit.mode = FBSDKShareDialogMode.FeedBrowser
      
    3. 我正在使用.FeedBrowser,因为我的工作正常。

      <强> FBShareDialogue:

      enter image description here

      <强> FBTimeline:

      enter image description here

      现在我们得到了时间轴上的所有内容。标题,描述,图像,SiteURL。