Updated 22 October 2024
Now these days its very common and popular to share content or post on social app like Facebook, Twitter, Email & Google+. I have some stuff that I have used to share content and posts on social apps. Add the following code in the ViewController.swift file in order to be able to post on Facebook, Twitter, Email & Google+ by tapping the relative buttons.
In this writing, I want explore how to use MFMailComposeViewController with Swift to send e-mails within your app as a walkthrough. My focus here is “quick and dirty” pragmatism, so that we can easily see what the inter-working components of MFMailComposeViewController are. That being said, here’s an important disclaimer – I’m going to overload the View Controller’s responsibilities in the examples to follow.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import Social import MessageUI class ViewController: UIViewController, MFMailComposeViewControllerDelegate {     @IBAction func tapEmail_btn(_ sender: Any) {         let emailTitle = "Subject of mail"         let messageBody = "Content of mail"         let mc: MFMailComposeViewController = MFMailComposeViewController()         mc.mailComposeDelegate = self         mc.setSubject(emailTitle)         mc.setMessageBody(messageBody, isHTML: false)         mc.setToRecipients(toRecipents)         self.present(mc, animated: true, completion: nil)     }     func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {         switch result {         case .cancelled:             print("Mail cancelled")         case .saved:             print("Mail saved")         case .sent:             print("Mail sent")         case .failed:             print("Mail sent failure: \(error?.localizedDescription)")         default:             break         }         self.dismiss(animated: true, completion: nil)     }     } | 
SLComposeViewController is part of the Social framework, provided by Apple. It presents to the users a view that enables them to edit a post that’s going to be shared on social networks, such as Facebook, Twitter and Google+. In order to share content, you’ll provide theSLComposeViewController with information such as URL and initial text. so we’re sharing our code in this blog post.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import Social import SafariServices class ViewController: UIViewController , SFSafariViewControllerDelegate{     @IBAction func tapFacebook_btn(_ sender: Any) {         if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook) {             guard let composer = SLComposeViewController(forServiceType: SLServiceTypeFacebook) else { return }             composer.add(URL(string: "https://mobikul.com/blog"))             composer.setInitialText("Content to share")             present(composer, animated: true, completion: nil)         }else {             let postText ="Content to share"             let postUrl ="https://mobikul.com/blog"             let shareString = "https://facebook.com/intent/post?text=\(postText)&url=\(postUrl)"             let escapedShareString = shareString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!             let url = URL(string: escapedShareString)             UIApplication.shared.openURL(url!)         }     } } | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import Social  import SafariServices class ViewController: UIViewController , SFSafariViewControllerDelegate{     @IBAction func tapTwitter_btn(_ sender: Any) {         if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeTwitter) {             guard let composer = SLComposeViewController(forServiceType: SLServiceTypeTwitter) else { return }             composer.add(URL(string: "https://mobikul.com/blog"))             composer.setInitialText("Content to share")             present(composer, animated: true, completion: nil)         }else {             let tweetText ="Content to share"             let tweetUrl = "https://mobikul.com/blog"             let shareString = "https://twitter.com/intent/tweet?text=\(tweetText)&url=\(tweetUrl)"             let escapedShareString = shareString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!             let url = URL(string: escapedShareString)             UIApplication.shared.openURL(url!)         }             } } | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import Social  import SafariServices class ViewController: UIViewController , SFSafariViewControllerDelegate{     @IBAction func tapGoogle_btn(_ sender: Any) {         let urlstring = "https://mobikul.com/blog"         let shareURL = NSURL(string: urlstring)         let urlComponents = NSURLComponents(string: "https://plus.google.com/share")         urlComponents!.queryItems = [NSURLQueryItem(name: "url", value: shareURL!.absoluteString) as URLQueryItem]         let url = urlComponents!.url!         if #available(iOS 9.0, *) {             let svc = SFSafariViewController(url: url)             svc.delegate = self             self.present(svc, animated: true, completion: nil)         }else {             debugPrint("Not available")         }     } } | 
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.