-1

I'm a beginner on Swift and can't really figure out why it crashes.

Simulator is fine (it's just giving me a message that device doesn't have camera) But when I open it on my actual phone the app crashes. (Using iphone x with 11.2.6)
Every other functions are fine but camera function.

How do I fix it? Thanks in advance!

import Foundation
import UIKit

class ProductInfoController: UIViewController, 
UIImagePickerControllerDelegate, UINavigationControllerDelegate, 
UITextFieldDelegate {

@IBOutlet weak var productName: UITextField!
@IBOutlet weak var productImage: UIImageView!

var product = Product()

var returningFromPicker = false

override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.navigationBar.prefersLargeTitles = true
    navigationItem.largeTitleDisplayMode = .always
    navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
    productName.delegate = self
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if !returningFromPicker {
        if productName.text == nil {
            productName.text = product.productName
        }

        if productImage.image == nil {
            productImage.image = product.productImage
        }
    }
}

@IBAction func pickImageFromCamera(_ sender: UIButton) {
    chooseImage(fromLibrary: false)
}

@IBAction func pickImageFromLibrary(_ sender: UIButton) {
    chooseImage(fromLibrary: true)
}

@IBAction func onNext(_ sender: UIButton) {
    guard let name = productName.text else {
        showAlert(message: "Provide a product name")
        return
    }

    guard !name.isEmpty else {
        showAlert(message: "Invalid product name provided")
        return
    }

    guard product.productImage != nil else {
        showAlert(message: "Provide a product photo")
        return
    }

    product.productName = name

    if let keywordsVC = storyboard?.instantiateViewController(withIdentifier: "keywordVC") as? KeywordsViewController {
        keywordsVC.product = product
        navigationController?.pushViewController(keywordsVC, animated: true)
    }
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return false
}

func chooseImage(fromLibrary: Bool) {
    let sourceType:UIImagePickerControllerSourceType = fromLibrary ? .photoLibrary:.camera

    guard UIImagePickerController.isSourceTypeAvailable(sourceType) else {
        showAlert(message: "Device doesn't have a " + (fromLibrary ? "Photo Library":"Camera"))
        return
    }

    let imagePicker = UIImagePickerController();
    imagePicker.delegate = self
    imagePicker.allowsEditing = true
    imagePicker.sourceType = sourceType

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

    returningFromPicker = true
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    productImage.image = info[UIImagePickerControllerEditedImage] as? UIImage
    product.productImage = productImage.image
    picker.dismiss(animated: true, completion: nil)
    returningFromPicker = false
}
}

extension UIViewController {
func showAlert(message: String) {
    let alert = UIAlertController(title: "Title", message: message, preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
    present(alert, animated: true, completion: nil)
}
}
conan conan
  • 31
  • 1
  • 2
  • https://stackoverflow.com/questions/39631256/request-permission-for-camera-and-library-in-ios-10-info-plist – kbunarjo Feb 23 '18 at 02:56

1 Answers1

10

You have to add the below permission in Info.plist. Permission in Info.plist

Camera :

Key       :  Privacy - Camera Usage Description   
Value     :  $(PRODUCT_NAME) camera use

Photo :

Key       :  Privacy - Photo Library Usage Description    
Value     :  $(PRODUCT_NAME) photo use
Pavan Gandhi
  • 1,729
  • 1
  • 22
  • 36