UIImage Picker Controller

The ISN_UIImagePickerController manages the system interfaces for taking pictures, recording movies, and choosing items from the user's media library.

An image picker controller manages user interactions and delivers the results of those interactions. The role and appearance of an image picker controller depend on the ISN_UIImagePickerControllerSourceType you assign to it before you present it.

Before attempting to use ISN_UIImagePickerController it's recommended you call IsSourceTypeAvailable method to ensure that the desired source type is available. It returns a boolean value indicating whether the device supports picking media using the specified source type. Because media source may not be present or may be unavailable, devices may not always support all source types. For example, if you attempt to pick an image from the user’s library and the library is empty this method returns false. Similarly, if the camera is already in use, this method returns false.

using SA.IOSNative.UIKit;
...
var source = ISN_UIImagePickerControllerSourceType.PhotoLibrary;
bool isAvailable = ISN_UIImagePickerController.IsSourceTypeAvailable(source);

You can also see what kind of media types are available on a current device for a particular source. For example, some iOS devices support video recording and some not. Use the GetAvailableMediaTypes method, along with the IsSourceTypeAvailable method,  to determine if video recording is available on a device.

using SA.IOSNative.UIKit;
...
var source = ISN_UIImagePickerControllerSourceType.Camera;
List<string> availableMediaType = ISN_UIImagePickerController.GetAvailableMediaTypes(source);
foreach(var mediaType in availableMediaType) {
    Debug.Log(mediaType);
}

This isn't required, but could a very good practice to do it, before proceeding to the next articles, that explain how topic and save media with Camera, Album, and Photo Library sources.

Access Authorizations

Before you start working with user photo library or camera, make sure user authorized your application access.

Request Camera Permission.

Camera usage always requires explicit permission from the user. Your app must provide an explanation for its use of capture devices using the NSCameraUsageDescription Info.plist key. iOS displays this explanation when initially asking the user for permission,  and thereafter in the Settings app. Calling this method or attempting to start a capture session without a usage description raises an exception.

The NSCameraUsageDescription can be provided with the IOS Native Settings editor. See the picture below:

The following code snippet requests the user’s camera permission.

using SA.IOSNative.AVFoundation;
...
ISN_AVCaptureDevice.RequestAccess(ISN_AVMediaType.Video, (status) => {
    if(status == ISN_AVAuthorizationStatus.Authorized) {
        Debug.Log("user has granted camera permission");
    } else {
    	Debug.Log("uAVMediaType.Video Permission declined");
    }
});

You can always check current authorization status as shown on a snippet below:

using SA.IOSNative.AVFoundation;
...
ISN_AVAuthorizationStatus status;
status = ISN_AVCaptureDevice.GetAuthorizationStatus(ISN_AVMediaType.Video);
Debug.Log("Camera Authorization Status: " + status);

Request Photo Library Permission.

Accessing the Photos library always requires explicit permission from the user. Your app’s info.plist file must provide a value for the NSPhotoLibraryUsageDescription key that explains to the user why your app is requesting Photos access.  Apps linked on or after iOS 10.0 will crash if this key is not present. After the user grants permission, the system remembers the choice for future use in your app,  but the user can change this choice at any time using the Settings app. If the user has denied your app photo library access, not yet responded to the permission prompt, or cannot grant access due to restrictions.

The NSPhotoLibraryUsageDescription can be provided with the IOS Native Settings editor. See the picture below:

 

The following code snippet requests the user’s photo library permission.

using SA.IOSNative.Photos;
...
ISN_PHPhotoLibrary.RequestAuthorization((status) => {
    if(status == ISN_PHAuthorizationStatus.Authorized) {
        Debug.Log("Permission granted");
    } else {
        Debug.Log("Permission denied");
    }
});

You can always check current authorization status as shown on a snippet below:

using SA.IOSNative.Photos;
...
ISN_PHAuthorizationStatus status;
status = ISN_PHPhotoLibrary.AuthorizationStatus;
Debug.Log("Photo Library Authorization Status: " + status);

Because of the ISN_PHPhotoLibrary is part of the Photos framework, it should be also enabled with IOS Native Settings Window. Please see the picture below: