SLComposeViewController - Post a message and an image to Facebook
So this example using the Facebook SDK for iOS is written. A share button is created to post a message and an image to Facebook. The steps are as below:
1. Include the FBSDK frameworks into an Xcode project and modify AppDelegate.swift:
See this tutorial: Facebook SDK and Swift - Create a Facebook Login Button
2. Modify ViewController.swift as:
import UIKit
import FBSDKLoginKit
import FBSDKShareKit
class ViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
let urlImage = NSURL(string: "https://upload.wikimedia.org/wikipedia/commons/0/0e/THSR_700T_Modern_High_Speed_Train.jpg")
let imageView = UIImageView(frame: CGRectMake(0, 0, 200, 200))
imageView.center = CGPoint(x: view.center.x, y: 200)
imageView.image = UIImage(data: NSData(contentsOfURL: urlImage!)!)
imageView.contentMode = UIViewContentMode.ScaleAspectFit
view.addSubview(imageView)
let content = FBSDKShareLinkContent()
content.contentTitle = "Taiwan High Speed Rail. Posted with my iOS App."
content.imageURL = urlImage
let shareButton = FBSDKShareButton()
shareButton.center = CGPoint(x: view.center.x, y: 500)
shareButton.shareContent = content
view.addSubview(shareButton)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Result:
Press the Share button.
Press the Share button.
Login and press the Share button.
The message and image are shown with the Facebook app.
Note: An HTTPS image URL is required for iOS9. If an HTTP URL is used instead, this error will happen:
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
This is because Apple has introduced App Transport Security (ATS) in iOS9. To allow the HTTP network connection, modify Info.plist by adding:
More details regarding ATS is here:
Apple will require HTTPS connections for iOS apps by the end of 2016
References:
Facebook SDK for iOS - Getting Started
SLComposeViewController - Post a message and an image to Facebook (Deprecated)
Facebook SDK and Swift - Create a Facebook Login Button
Facebook SDK and Swift - Display User Name and Profile Picture
Facebook SDK and Swift - Post a message using Graph API and post an image using FBSDKShareKit
Best and easy tutorial. Thank you so much.
ReplyDeleteit don't run in iPad.... not add image into share of Facebook, can you help me?
ReplyDeleteAny error message?
DeleteHow to attach location with post?
ReplyDeletei am getting an error :(
ReplyDelete"com.apple.social.facebook returning NO"
i don't knw why it is returning NO
but could't be able to share anything :(
Thanks for sharing information.How to Fix http Error 500 Facebook
ReplyDelete