下面是实现 Share /Safari /Camera 过程
import UIKit
import SafariServices
import MessageUI
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, MFMailComposeViewControllerDelegate {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var SharePressed: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func SharePressed(_ sender: UIButton) {
guard let image = imageView.image else { return }
let activityController = UIActivityViewController(activityItems: [image], applicationActivities: nil)
activityController.popoverPresentationController?.sourceView = sender
present(activityController, animated: true, completion: nil)
}
@IBAction func SafariPressed(_ sender: UIButton) {
if let url = URL(string: "http://www.sample.net") {
let safariViewController = SFSafariViewController(url: url)
present(safariViewController,animated: true, completion: nil)
}
}
@IBAction func CameraPressed(_ sender: UIButton) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
let alertController = UIAlertController(title: "Choice a photo", message: nil, preferredStyle: .actionSheet)
//ipad使用,不加ipad上会崩溃
if let popoverController = alertController.popoverPresentationController {
popoverController.sourceView = sender
popoverController.sourceRect = sender.bounds
}
//add cancel action
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
//add camera action
if UIImagePickerController.isSourceTypeAvailable(.camera) {
let cameraAction = UIAlertAction(title: "Camera", style: .default, handler: { action in
imagePicker.sourceType = .camera
self.present(imagePicker, animated: true, completion: nil) })
alertController.addAction(cameraAction)
}
//add photo library action
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let photolibraryAction = UIAlertAction(title: "Photo Library", style: .default, handler: { action in
imagePicker.sourceType = .photoLibrary
self.present(imagePicker,animated: true, completion: nil) })
alertController.addAction(photolibraryAction)
}
//add all choices
present(alertController, animated: true, completion: nil)
// //image handler
// func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// if let selectedImage =
// info[UIImagePickerControllerOriginalImage] as? UIImage {
// imageView.image = selectedImage
// dismiss(animated: true,completion: nil)
// }
// }
}
//image handler
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let selectedImage =
info[UIImagePickerControllerOriginalImage] as? UIImage {
imageView.image = selectedImage
dismiss(animated: true,completion: nil)
}
}
@IBAction func EmailPressed(_ sender: UIButton) {
if !MFMailComposeViewController.canSendMail() {
print("Can not send mail!")
return
}
// optional public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}