I am using Document Picker in Xamarin iOS. In the sample of the DocPicker, they called something like StartAccessingSecurityScopedResource with comment on top.
// IMPORTANT! You must lock the security scope before you can // access this file.
Why do they have to lock this file? Is it prevent the file from modify from somewhere else while accessing the file.
In iOS (and MacOS sandboxed apps), accessing an external document, a document that is outside of your own app's sandbox, requires special permission that is tracked by the OS. So once you have a NSUrl that points to an external file, you actually do not have access to it until you definitively ask for it as there is an security/resource overhead in doing so,
Note: Make sure that you call
StopAccessingSecurityScopedResourceotherwise your app leaks kernel-based resources (try/finallyis a wise coding pattern here)Ref: Security-Scoped URLs