Swift NSPhotoLibrary使用说明密钥不读

问题描述:

我已经将密钥和字符串消息添加到.plist,但由于某种原因我的应用程序不断崩溃,我不知道为什么。我从swift添加到.plist文件,我也尝试手动添加到代码。当我运行应用程序时,它不工作,当我到达那个点时它仍然崩溃。Swift NSPhotoLibrary使用说明密钥不读

https://useyourloaf.com/assets/images/2016/2016-07-03-001.png

错误消息:

This app has crashed because it attempted to access privacy-sensitive data 
without a usage description. The app's Info.plist must contain an 
NSPhotoLibraryUsageDescription key with a 
string value explaining to the user how the app uses this data. 




import UIKit 
import Firebase 
import FirebaseStorage 
import KeychainSwift 


class UserProfile: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

    //Username UI Label 
    @IBOutlet weak var changeImageButton: UIButton! 


    @IBOutlet weak var usernameLabel: UILabel! 
    @IBOutlet weak var userProfileImage: UIImageView! 

    var ref: FIRDatabaseReference! 
    var storage: FIRStorageReference! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     ref = FIRDatabase.database().reference() 
     storage = FIRStorage.storage().reference() 

     //Make Porfile Image Cirlce 
     userProfileImage.layer.cornerRadius = userProfileImage.frame.size.width/2 
     userProfileImage.clipsToBounds = true 

     let userID = FIRAuth.auth()?.currentUser?.uid 

     ref.child("Users").child(userID!).observeSingleEvent(of: .value, with: {(snapshot) in 

      let snapDict = snapshot.value as? NSDictionary 

      let username = snapDict?["Username"] as? String ?? "" //Get Username 


      //print("Username: " + username) 

      self.usernameLabel.text = username //Label set to UserName 


     }) 

    }//View Did load 


    @IBAction func choosePicture(_ sender: AnyObject) { 

     let pickerController = UIImagePickerController() 
     pickerController.allowsEditing = true 

     let alertController = UIAlertController(title: "Add Picture", message: "Choose From", preferredStyle: .actionSheet) 

     let cameraAction = UIAlertAction(title: "Camera", style: .default) { (action) in 
      pickerController.sourceType = .camera 
      self.present(pickerController, animated: true, completion: nil) 

    } 


     let photoLibraryAction = UIAlertAction(title: "Photo Library", style: .default) { (action) in 
      pickerController.sourceType = .photoLibrary 
      self.present(pickerController, animated: true, completion: nil) 
} 

     let savedPhotosAction = UIAlertAction(title: "Saved Photos Album", style: .default) { (action) in 
      pickerController.sourceType = .savedPhotosAlbum 
      self.present(pickerController, animated: true, completion: nil) 
     } 


     let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil) 
     alertController.addAction(cameraAction) 
     alertController.addAction(photoLibraryAction) 
     alertController.addAction(savedPhotosAction) 
     alertController.addAction(cancelAction) 
     present(alertController, animated: true, completion: nil) 


    }//END CHOOSE PICTURE 

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController, didFinishPackageImage image: UIImage, editingInfo: [String : AnyObject]?) { 

     self.dismiss(animated: true, completion: nil) 
     self.userProfileImage.image = image 
    } 


    func updateProfilePicture() { 

     //Assign current user ID 
     let userID = FIRAuth.auth()?.currentUser?.uid 

     //Create an access point for the Firebase Storage 
     let storageItem = storage.child("profile_images").child(userID!) 

     guard let image = userProfileImage.image else {return} 

     if let newImage = UIImagePNGRepresentation(image) { 

      //upload to Firebase 
      storageItem.put(newImage, metadata: nil, completion: { (metadata, error) in 
       if error != nil{ 
       print(error!) 
       return 

      } 

       storageItem.downloadURL(completion: { (url, error) in if error != nil{ 
        print(error!) 
        return 
        } 
     }) 
    }) 

} 


} 


    //UI Button: Sign Out the User 
    @IBAction func SignOut(_ sender: UIButton) { 

     let firebaseAuth = FIRAuth.auth() 
        do { 
         try firebaseAuth?.signOut() 
        } catch let signOutError as NSError { 
         print ("Error signing out: %@", signOutError) 
        } 
        //KeychainSwift().delete("uid") 
        DataService().keyChain.delete("uid") 
        dismiss(animated: true , completion: nil) 
    } 








}//End UserProfile Class 
+0

难道这是因为我在模拟器上测试吗?这是允许在模拟器上? – nil

您使用的是错误的。应该是“隐私照片库使用”

+0

NOt工作在最新的ios,非常奇怪的东西 – KOTIOS

+0

@MyMasterPeice最新的你的意思是10.2?它应该可以工作,因为API根本没有改变。请注意,如果您使用相机,您需要同时询问相机“隐私 - 相机使用说明”和相片“隐私 - 相片使用说明”,且该值不能为空,因此您需要放置相片。 – apineda

+0

我试了两个事实我累了几个许可也是不需要但与媒体有关,但它仍然给我错误说NSPhotoLibraryUsageDescription应该在info.plist中定义,试图清洗anf构建,但不知何故xcode不拿起权限很奇怪但它并不适合 – KOTIOS

您需要设置NSPhotoLibraryUsageDescription关键和你的描述你的info.plist