Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTom Coomer
1,331 PointsSwift Social Framework
I would like to add the ability to send a tweet from my app using the social framework but I am stuck and do not know where to start. I am able to do it using Objective-C but can't seem to translate it over. Thanks
2 Answers
Dimitris Sotiris Tsolis
27,661 PointsHere is a helper method i use
func tweet(message: String? = nil, image: UIImage? = nil, url: NSURL? = nil) {
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter) {
let controller = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
controller.setInitialText(message)
controller.addImage(image)
controller.addURL(url)
controller.completionHandler = { (result:SLComposeViewControllerResult) -> Void in
switch result {
case SLComposeViewControllerResult.Cancelled:
NSLog("result: cancelled")
case SLComposeViewControllerResult.Done:
// TODO: ADD SOME CODE FOR SUCCESS
NSLog("result: done")
}
}
self.presentViewController(controller, animated: true, completion: { () -> Void in
// controller is presented... do something if needed
})
} else {
NSLog("Twitter is not available")
}
}
Setting default values for the parameters gives you the ability to make a more flexible call. Something like this
self.tweet()
// or
self.tweet(image: UIImage(named: "example_image"))
// or
self.tweet(message: "Sample message")
// or
self.tweet(url: NSURL(string: "http://example.com/"))
// or
self.tweet(message: "Sample message", image: UIImage(named: "example_image"), url: NSURL(string: "http://example.com/"))
// etc
Tom Coomer
1,331 PointsGreat. Thank you. I'll give that a try.
Dimitris Sotiris Tsolis
27,661 PointsNo problem!
Tom Coomer
1,331 PointsTom Coomer
1,331 PointsFantastic! Thank you very much.
Dimitris Sotiris Tsolis
27,661 PointsDimitris Sotiris Tsolis
27,661 PointsI'm glad that I helped!
Tom Coomer
1,331 PointsTom Coomer
1,331 PointsIs this the same as sending an iMessage too or does that use another framework?
Dimitris Sotiris Tsolis
27,661 PointsDimitris Sotiris Tsolis
27,661 PointsNo but you can use Facebook share if you replace SLServiceTypeTwitter with SLServiceTypeFacebook.
In order to send iMessage or e-mail you have to use MessageUI framework, conform to the MFMessageComposeViewControllerDelegate (for iMessage) and MFMailComposeViewControllerDelegate (for email).
Then, somewhere in your code you can use
After that, you have to dismiss the controller. It won't dismissed by default so, a default implementation could be
Tom Coomer
1,331 PointsTom Coomer
1,331 PointsThere is an error on this line
controller.messageComposeDelegate.self
The message says: "Expression resolves to an unused l-value"
Do you know what could be wrong? Thanks
Dimitris Sotiris Tsolis
27,661 PointsDimitris Sotiris Tsolis
27,661 PointsIt's a typo error. You added a dot instead of the equal sign. Try this
controller.messageComposeDelegate = self
Tom Coomer
1,331 PointsTom Coomer
1,331 PointsI have changed that and also imported the MessageUI framework. At the top should I add:
class ViewController: UIViewController, MFMessageComposeViewControllerDelegate
I have tried but it shows an error saying "Type 'ViewController' does not conform to protocol 'MFMessageComposeViewControllerDelegate'". Thanks
Dimitris Sotiris Tsolis
27,661 PointsDimitris Sotiris Tsolis
27,661 PointsYes you have to add MFMessageComposeViewControllerDelegate and it should be ok. If no, try to clean the project and rebuild. That's how it's fixed on my projects.