Skip to content

Instantly share code, notes, and snippets.

@anjnkmr
Forked from anonymous/SocialService.swift
Last active March 27, 2017 06:20
Show Gist options
  • Save anjnkmr/b9537d5fbcd5deeb312533b15c488aea to your computer and use it in GitHub Desktop.
Save anjnkmr/b9537d5fbcd5deeb312533b15c488aea to your computer and use it in GitHub Desktop.
Social Sharing iOS Swift 2.3
//
// SocialService.swift
// SocialSharing
//
// Created by Zonup Mac 3 on 10/11/16.
// Copyright © 2016 Zonup. All rights reserved.
//
import UIKit;
import Social;
class SocialService{
static let shared = SocialService();
private init(){
}
func shareWithFacebook(sender: UIViewController, text: String, url: NSURL?, image: UIImage?, completion: (result: SLComposeViewControllerResult) -> Void){
makeSocialCall(sender, serviceType: SLServiceTypeFacebook, text: text, url: url, image: image, completion: completion);
}
func shareWithTwitter(sender: UIViewController, text: String, url: NSURL?, image: UIImage?, completion: (result: SLComposeViewControllerResult) -> Void){
makeSocialCall(sender, serviceType: SLServiceTypeTwitter, text: text, url: url, image: image, completion: completion);
}
func makeSocialCall(sender: UIViewController, serviceType: String, text: String, url: NSURL?, image: UIImage?, completion: (result: SLComposeViewControllerResult) -> Void){
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter){
let controller = SLComposeViewController(forServiceType: serviceType);
controller.setInitialText(text);
if image != nil{
controller.addImage(image!);
}
if url != nil{
controller.addURL(url!)
}
controller.completionHandler = completion;
sender.presentViewController(controller, animated: true, completion: nil);
}
else{
completion(result: .Cancelled);
}
}
}
@IBAction func twitterTapped(sender: UIButton) {
SocialService.shared.shareWithTwitter(self, text: textView.text, url: nil, image: imageVIew.image, completion: shareCompleted);
}
func shareCompleted(result: SLComposeViewControllerResult){
self.view.userInteractionEnabled = true;
if result == .Cancelled{
//"Cancelled";
}
else
{
//"Completed";
}
}
@anjnkmr
Copy link
Author

anjnkmr commented Mar 27, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment