尽管没有.plist的许可,iOS应用程序在访问照片时不会崩溃

时间:2017-12-04 03:54:43

标签: ios swift xcode

我有一个奇怪的“问题” - 我正在工作的应用程序没有崩溃。我正在学习iOS开发并关注this tutorial on Apple's website。有一次,您被指示运行您的应用程序(尝试访问用户的照片库),而无需请求权限或在.plist文件中添加说明。根据教程,该应用程序预计将与SIGABRT崩溃,但我的运行没有抱怨。我使用的是xCode 9,iOS 11和SWIFT 4,本教程是用xCode 8,iOS 10和SWIFT 3编写的。

有谁知道为什么这不会崩溃?是否由于iOS 10和11之间的某些变化,我应该知道前进的哪些方面?或者也许在xCode 9中更改模拟器?

我只是想确保这种行为上的差异不是我错过的一些概念性冰山的一角。我试图在网上找到答案,但并不是很多人似乎抱怨他们的代码工作,所以没有太多可以继续下去。

这是我的ViewController的代码,如果有帮助的话,但我的直觉是差异可能与我的代码无关:

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 {
        //Hides the keyboard
        textField.resignFirstResponder()
        return true
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        mealNameLabel.text = nameTextField.text
        nameTextField.text = nil
    }

    // MARK: UIImagePickerControllerDelegate:

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

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        //The image dictionary may contain multiple representations of the user's chosen image, but we will use the original. Make sure the image exists:
        guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else{
            fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
        }
        //Set the image for photoImageView to the selected image
        photoImageView.image = selectedImage
        //Dismiss the picker
        dismiss(animated: true, completion: nil)
    }

    // MARK: Actions

    @IBAction func imageFromPhotoLibrary(_ sender: UITapGestureRecognizer) {
        //Hide the keyboard
        nameTextField.resignFirstResponder()

        //Creates a view controller that will allow the user to choose a photo
        let imagePickerController = UIImagePickerController()

        //Allows user to choose a photo from their photo library
        imagePickerController.sourceType = .photoLibrary

        //Make sure this view controller will get notified when the user chooses a picture
        imagePickerController.delegate = self
        present(imagePickerController, animated: true, completion: nil)
    }

    @IBAction func setDefaultTextLabel(_ sender: UIButton) {
        mealNameLabel.text = "Default Text"
    }
}

0 个答案:

没有答案