简体   繁体   中英

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

I have written a simple Xcode project in swift for my iPhone . I am trying to send the text of my label to facebook using facebook share option .

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. Someone guide me here . Why is the text not posting in facebook ?

Edit:Update I downloaded FBSDK files and added them to my project . I imported FBSDKShareKit into my ViewController.h file.

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" . Anyone help please !

You have to check return value of setInitialText(_:) method.

That function can return 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.

  2. For basic check the following link cocoapods .Open your terminal install cocoapods

    $ sudo gem install cocoapods

    1. open your project directory,like this eg: 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

    3. Open the podfile in TextEdit from finder.to install FBSDK add pod 'FBSDKShareKit' the entire podfile will look like this

       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.

       Installing FBSDKShareKit (4.8.0) 

      NOTE: After installing pod in your project .xcworkspace file will be created. Instead of using .xcodeproj you have to use .xcworkspace for further process of your project

  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.

  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.
  5. Then import your pod-file inside the bridging header file it should look like as follows

     #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

在此处输入图片说明

  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

      ShareKit.mode = FBSDKShareDialogMode.FeedBrowser 

I'am using .FeedBrowser,because is working fine for me.

FBShareDialogue:

在此处输入图片说明

FBTimeline:

在此处输入图片说明

Now we got everything in timeline.Title,Description,Image,SiteURL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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