2

I am following a Swift tutorial on Apple's website titled 'Start Developing iOS Apps (Swift)'.
Xcode version: Version 9.2 (9C40b)

In the third section Work with View Controllers I expected a SIGABRT error as shown in the tutorial towards the end; instead, my app ran smoothly.

This is what the tutorial says about the error I was expecting:

The app terminates with a SIGABRT signal. This means an error occurred that was serious enough to cause the app to abort. In this case, the problem occurs when you attempt to present the image picker. The system must ask the user for permission before accessing their photo library. In iOS 10 and later, you must provide a photo library usage description. This description explains why your app wants to access the photo library.

As mentioned before, no prompts whatsoever was shown, my app ran smoothly in the simulator. Strange. What could be the reason?

Below is my code(an exact replica of the code in the tutorial):

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    //MARK: Properties
    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var mealNameLabel: UILabel!
    @IBOutlet weak var photoImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Handle the text field's user input through delegate callbacks.
        nameTextField.delegate = self

    }

    //MARK: UITextFieldDelegate
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // Hide the keyboard.
        textField.resignFirstResponder()
        return true
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
        mealNameLabel.text = textField.text
    } 

    //MARK: UIImagePickerControllerDelegate
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        // Dismiss the picker if the user canceled.
        dismiss(animated: true, completion: nil)
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        // The info dictionary may contain multiple representations of the image. You want to use the original.
        guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
            fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
        }

        // Set photoImageView to display the selected image.
        photoImageView.image = selectedImage

        // Dismiss the picker
        dismiss(animated: true, completion: nil)
    }
    //MARK: Actions
    @IBAction func selectImageFromPhotoLibrary(_ sender: UITapGestureRecognizer) {
        // Hide the keyboard.
        nameTextField.resignFirstResponder()

        // UIImagePickerController is a view controller that lets a user pick media from their photo library.
        let imagePickerController = UIImagePickerController()

        // Only allow photos to be picked, not taken.
        imagePickerController.sourceType = .photoLibrary

        // Make sure ViewController is notified when the user picks an image.
        imagePickerController.delegate = self
        present(imagePickerController, animated: true, completion: nil)
    }
    @IBAction func setDefaultLabelText(_ sender: UIButton) {
        mealNameLabel.text = "Default Text"
    }
}

info.plist: info.plist

3 Answers3

1

The reason is that your are using iOS 11, but the tutorial was written for iOS 10 or before. New in iOS 11, you do not need user authorization to use a UIImagePickerController to get an image from the user's photo library.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • This link and the apple link within accepted answer mentions otherwise : https://stackoverflow.com/questions/44527321/photo-capture-permission-problems-in-ios-11 – Nitish Jan 28 '18 at 19:54
  • My apologies. This is somewhat confusing. This link clearly mentions that permission is not required for selecting photo from library : https://github.com/KrauseFx/detect.location/issues/6 – Nitish Jan 28 '18 at 19:58
  • I have undone my downvote and converted it to upvote. This should perhaps be the accepted answer. – Nitish Jan 28 '18 at 19:58
0

For using Photo Library, you will want this one to allow app user to browse the photo library.

<key>NSPhotoLibraryUsageDescription</key>
<string>You can select photos to attach to reports.</string>

For using Camera:

<key>NSCameraUsageDescription</key>
<string>You can take photos to document your job.</string>
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

I downloaded the code from the mentioned website. The plist file you have shared in the question is of the FoodTrackerTests. The one which needs to be referred in in FoodTracker folder and already has the required key :

enter image description here

So app is behaving exactly the same way it should.

Nitish
  • 13,845
  • 28
  • 135
  • 263