Swift-如何写share safari camera

来源:互联网 发布:软件缺陷的生命周期 编辑:程序博客网 时间:2024/06/08 16:44

下面是实现 Share /Safari /Camera 过程


import UIKit

import SafariServices

import MessageUI


class ViewController:UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate, MFMailComposeViewControllerDelegate {

    @IBOutlet weakvar imageView: UIImageView!

    @IBOutlet weakvar 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.imageelse { 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

        ifUIImagePickerController.isSourceTypeAvailable(.camera) {

            

            let cameraAction = UIAlertAction(title: "Camera", style: .default, handler: { actionin

                imagePicker.sourceType = .camera

                self.present(imagePicker, animated:true, completion: nil) })

            alertController.addAction(cameraAction)

        }

        //add photo library action

        ifUIImagePickerController.isSourceTypeAvailable(.photoLibrary) {

            let photolibraryAction = UIAlertAction(title: "Photo Library", style: .default, handler: { actionin

                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]asUIImage {

            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.

    }



}







原创粉丝点击