如何将标签的文字从iPhone应用程序共享到Facebook? [英] How to share text of a label from iPhone app to facebook?
问题描述
以下是我的代码
import UIKit
import Social
class ViewController:UIViewController {
@IBOutlet weak var musicDescription:UILabel!
覆盖func viewDidLoad(){
super.viewDidLoad()
}
@IBAction func faceBookShareButton(sender:UIButton){
如果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 (标题:帐户,消息:请登录到Facebook帐户共享,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(发件人:UIButton){
如果UIApplication.sharedApplication()。canOpenURL(NSURL(string:fb:)!)
var content:FBSDKShareLinkContent = FBSDKShareLinkContent()
var主题:String = String(格式:FBSDKShareKit是此问题的替代方案)
content.contentTitle =FBSDKShareKit
content.contentDescription =主题
var对话框:FBSDKShareDialog = FBSDKShareDialog()
dialog.fromViewController = self
dialog.shareContent = content
dialog.mode = FBSDKShareDialogModeWeb
dialog.show()
}
我我收到错误使用未解析的标识符FBSDKShareDialogModeWeb
。
任何人都有帮助!
-
使用 FBSDKShareKit 不是一个完整的选择,但它有助于在共享对话框屏幕上显示您预先填写的内容。
-
对于基本检查以下链接
-
现在添加按钮操作中的代码如下:
如果UIApplication.sharedApplication()。canOpenURL(NSURL (string:fb:)!){
let content:FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string:https://google.com)
content.contentTitl e =Test
content.contentDescription =FBSDKShareKit是此问题的替代方案
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()
}
-
使用未解析的标识符FBSDKShareDialogModeWeb 修复此替换
dialog.mode
如下ShareKit.mode = FBSDKShareDialogMode.FeedBrowser
/ li>
我使用.FeedBrowser,因为对我来说很好。
FBShare对话:
FBTimeline:
现在我们在时间线上得到了所有的东西le,说明,图像,SiteURL。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 !
解决方案Using FBSDKShareKit is not an complete alternative but it helps to show your pre-filled content on the share dialog screen.
For basic check the following link cocoapods.Open your terminal install cocoapods
$ sudo gem install cocoapods
open your project directory,like this eg: cd/desktop/TestSwift,
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 projectOpen the podfile in TextEdit from finder.to install FBSDK add
pod 'FBSDKShareKit'
the entire podfile will look like thisplatform :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
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
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.
- How to create bridging header: Use
Command ⌘ + N
the underios-source
choose header file name it as *BridgingHeader.h** select location and then create it. 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 */
Now we have successfully created bridging header to make sure your bridging header is added successfully open
Targets
then select your target openBuild Setting
tab in search bar type bridging.In search result under Objective-C BridgingHeader you should have your ProjectName/BridgingHeader.h
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() }
Use of unresolved identifier FBSDKShareDialogModeWeb to fix this replace
dialog.mode
as followsShareKit.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.
这篇关于如何将标签的文字从iPhone应用程序共享到Facebook?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-