0

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 !

iPhoneDeveloper
  • 958
  • 1
  • 14
  • 23
  • Are you facing this problem when FB app is installed ..? if it so check this one [link](http://stackoverflow.com/questions/37320157/i-cant-share-a-content-from-my-ios-app-to-the-facebook-when-facebook-app-is-ins/37324241#37324241) – Gokul G May 27 '16 at 11:58
  • Why am i not able to send the text ? Is there any requirement that we need to use latest version of facebook in iPhone? – iPhoneDeveloper May 27 '16 at 12:16
  • @Gokulvivid First i logged into my facebook account . Then i ran my app in xcode . Its just showing blank empty lines . – iPhoneDeveloper May 27 '16 at 12:24
  • Because FB addd some privacy policy's [Check](https://developers.facebook.com/policy/#control),it does't want its developer's add pre-filled text.If we want to share some text better go with FBShareKit as suggested in the above link. – Gokul G May 27 '16 at 12:25
  • if possible add the screenshot of FBsharedialogue which showing empty lines – Gokul G May 27 '16 at 12:27
  • Hi @Gokulvivid . I want to implement FBShareKit . I don't know anything about pod files. How do i create pod files and use FBShareKit – iPhoneDeveloper May 28 '16 at 09:13
  • @Gokulvivid I have used FBSDKShareKit in my Xcode. I am getting an error called "Use of unresolved identifier FBSDKShareDialogModeWeb". I tried changing the mode to FBSDKShareDialogModeFeedBrowser,FBSDKShareDialogModeBrowser, FBSDKShareDialogModeShareSheet and FBSDKShareDialogModeWeb. But none of them is working . – iPhoneDeveloper May 30 '16 at 03:56
  • Check my answer it may be help-full. – Gokul G May 30 '16 at 07:32

2 Answers2

0

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.

HS Song
  • 1
  • 1
0
  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 Settingtab in search bar type bridging.In search result under Objective-C BridgingHeader you should have your ProjectName/BridgingHeader.h

enter image description here

  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:

enter image description here

FBTimeline:

enter image description here

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

Gokul G
  • 2,046
  • 15
  • 22
  • Thanks a lot Gokul :) – iPhoneDeveloper May 30 '16 at 10:17
  • When i gave the command pod install in terminal i get this error [!] Unable to add a source with url `https://github.com/CocoaPods/Specs.git` named `master`. You can try adding it manually in `~/.cocoapods/repos` or via `pod repo add`. – iPhoneDeveloper May 30 '16 at 10:33
  • Make sure you have done everything correct,if it so try this command in terminal `sudo xcode-select -r` command above resets it to the default command line path. and try again – Gokul G May 30 '16 at 11:13