Search code examples
iosswift4uiimagepickercontrolleralert

alert view to pick camera or gallery doesn't pop up in swift 4


I have a viewcontroller with navigation controller. I have button for uploading images.But after pressing the button it doesn't popup the alert for camera or photo library options although i have written codes for alert. Why doesn't the alert popup appear? I have also given Privacy - Photo Library Usage Description in info.plist. My code is given :

import UIKit
import  Alamofire

class ProfilePicController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {


var imagePicker = UIImagePickerController()


@IBOutlet weak var uploadPic: UIButton!
@IBOutlet weak var profilePic: UIImageView!


override func viewDidLoad() {
    super.viewDidLoad()

  imagePicker.delegate = self
  profilePic.layer.cornerRadius = profilePic.frame.size.width / 2
  profilePic.clipsToBounds = true


    // Do any additional setup after loading the view.
}


@IBAction func uploadPic(_ sender: Any) {

 print("image add")

let alert = UIAlertController(title: nil, message: "Choose your source", preferredStyle: UIAlertControllerStyle.alert)

alert.addAction(UIAlertAction(title: "Camera", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
  print("Camera selected")
  self.imagePicker.sourceType = UIImagePickerControllerSourceType.camera

})
alert.addAction(UIAlertAction(title: "Photo library", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
  print("Photo selected")
  self.imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary

})

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

}
 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
 let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

 profilePic.image = selectedImage
 // self.savePic()
 dismiss(animated: true, completion: nil)
 }

 func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
 dismiss(animated: true, completion: nil)
}

//Alert.swift file

import UIKit

class Alert {

class func showBasic(title: String ,message: String , vc: UIViewController) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(defaultAction)
vc.present(alert, animated: true, completion: nil)
}
}

Solution

  • Actually you don't present the alert

    @IBAction func uploadPic(_ sender: Any) {
    
      print("image add")
    
      let alert = UIAlertController(title: nil, message: "Choose your source", preferredStyle: UIAlertControllerStyle.alert)
    
      alert.addAction(UIAlertAction(title: "Camera", style: .default) { (result : UIAlertAction) -> Void in
        print("Camera selected")
        self.imagePicker.sourceType = .camera
        self.present(self.imagePicker, animated: true, completion: nil)
      })
      alert.addAction(UIAlertAction(title: "Photo library", style: .default) { (result : UIAlertAction) -> Void in
        print("Photo selected")
        self.imagePicker.sourceType = .photoLibrary
        self.present(self.imagePicker, animated: true, completion: nil)
      })
    
      self.present(alert, animated: true, completion: nil)
    
    }