swift share url code example
Example 1: swift share with
import UIKit
class ViewController: UIViewController {
@IBAction func shareTextButton(_ sender: UIButton) {
let text = "This is some text that I want to share."
let textToShare = [ text ]
let activityViewController = UIActivityViewController(activityItems: textToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.postToFacebook ]
self.present(activityViewController, animated: true, completion: nil)
}
@IBAction func shareImageButton(_ sender: UIButton) {
let image = UIImage(named: "Image")
let imageToShare = [ image! ]
let activityViewController = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.postToFacebook ]
self.present(activityViewController, animated: true, completion: nil)
}
}
Example 2: swift open url
guard let url = URL(string: "http://www.google.com") else {
return
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}