ios - iOS 9 中的 Cordova 相机插件

标签 ios cordova camera ios9 photolibrary

我在 IOS 9 上使用 Cordova 5.3.3 和 Apache Camera Plugin 1.2.0。 我可以成功地用相机拍照,但是当我尝试从照片库获取图片时,它会返回到相机,并且我在 Cordova 错误回调中收到错误“无法访问资源”。我正在使用以下代码。

 navigator.camera.getPicture(onSuccess, onFail, {
       quality: 75,
       destinationType: Camera.DestinationType.DATA_URL,
       sourceType : Camera.PictureSourceType.PHOTOLIBRARY
   });

当我检查应用程序权限时,我发现它可以访问相机,但不能访问照片。这是问题所在吗? Cordova在添加插件时不是添加了所需的功能吗?

最佳答案

我在 iOS 上使用 cordova-plugin-camera 时遇到了类似的问题:拍照后未请求将照片存储在照片应用中的权限,因此将其存储到相机胶卷中失败了。

我通过使用 cordova-plugin-diagnostic 解决了这个问题确保在尝试拍照之前已获得相机和照片的授权。这也解决了用户在最初授予访问权限后撤销访问权限的边缘情况。根据我的实现和您对相机插件的使用,您可以尝试如下操作:

var userMessages = {
    noCamera: "The device doesn't have a working camera",
    cameraUnauthorized:{
        title: "Camera unavailable",
        message: "The app is not authorised to access the camera, which means it can't take photos. Would you like to switch to the Settings app to allow access?"
    },
    cameraRollUnauthorized:{
        title: "Photos unavailable",
        message: "The app is not authorised to access your Photos, which means it can't take photos. Would you like to switch to the Settings app to allow access?"
    },
    cameraAuthorisationError:{
        title: "Camera authorisation error",
        message: "The app could not request access to the camera due to the following error: "
    }
};

// Request camera authorisation
checkCameraIsUsable({
    successFn: onCameraAuthorised,
    errorFn: onCameraAuthorisationError,
    requireCameraRoll: true
});

// Called on successful authorisation of camera/camera roll
function onCameraAuthorised(){
    navigator.camera.getPicture(onSuccess, onFail, {
           quality: 75,
           destinationType: Camera.DestinationType.DATA_URL,
           sourceType : Camera.PictureSourceType.PHOTOLIBRARY
   });
}

// Called on error during authorisation of camera/camera roll
function onCameraAuthorisationError(error){
    console.error("An error occurred authorising use of the camera"):
    navigator.notification.alert(userMessages.cameraAuthorisationError.message, null, userMessages.cameraAuthorisationError.title);
}


/**
 * Checks if camera is available for use; i.e. camera is present and authorized for use.
 * If not and authorization has not yet been requested, requests authorization.
 * If authorization is denied, informs user and offers to switch to settings page to allow.
 * Optionally also checks if camera roll access has been authorized.
 * * If not and authorization has not yet been requested, requests authorization.
 * If authorization is denied, informs user and offers to switch to settings page to allow.
 *
 * @param {Object} params - parameters:
 * <ul>
 *    <li>{Function} successFn - callback to invoke if camera is available for use.</li>
 *    <li>{Function} errorFn - callback to to invoke if camera is unavailable for use. The function will be passed a single {String} argument which contains the error message.</li>
 *    <li>{Boolean} requireCameraRoll (optional) - if true, checks for/requests camera roll authorization. Defaults to false.</li>
 * </ul>
 */
 function checkCameraIsUsable(params){

    function requestCameraRollAuthorization(){
        cordova.plugins.diagnostic.requestCameraRollAuthorization(function(granted){
            if(granted){
                params.successFn();
            }else{
                onCameraRollAuthorizationDenied();
            }
        }, params.errorFn);
    }

    function onCameraRollAuthorizationDenied(){
        navigator.notification.confirm(
            userMessages.cameraRollUnauthorized.message,
            function(i){
                if(i==1){
                    cordova.plugins.diagnostic.switchToSettings();
                }
            },
            userMessages.cameraRollUnauthorized.title,
            ["Yes","No"]
        );
    }

    function getCameraRollAuthorizationStatus(){
        cordova.plugins.diagnostic.getCameraRollAuthorizationStatus(function(status){
            switch(status){
                case "denied":
                    onCameraRollAuthorizationDenied();
                    break;
                case "not_determined":
                    requestCameraRollAuthorization();
                    break;
                default:
                    params.successFn();
            }
        }, params.errorFn);
    }

    function requestCameraAuthorization(){
        cordova.plugins.diagnostic.requestCameraAuthorization(function(granted){
            if(granted){
                if(params.requireCameraRoll){
                    getCameraRollAuthorizationStatus();
                }else{
                    params.successFn();
                }
            }else{
                onCameraAuthorizationDenied();
            }
        }, params.errorFn);
    }

    function onCameraAuthorizationDenied(){
        navigator.notification.confirm(
            userMessages.cameraUnauthorized.message,
            function(i){
                if(i==1){
                    cordova.plugins.diagnostic.switchToSettings();
                }
            },
            userMessages.cameraUnauthorized.title,
            ["Yes","No"]
        );
    }

    function getCameraAuthorizationStatus(){
        cordova.plugins.diagnostic.getCameraAuthorizationStatus(function(status){
            switch(status){
                case "denied":
                    onCameraAuthorizationDenied();
                    break;
                case "not_determined":
                    requestCameraAuthorization();
                    break;
                default:
                    if(params.requireCameraRoll){
                        getCameraRollAuthorizationStatus();
                    }else{
                        params.successFn();
                    }

            }
        }, params.errorFn);
    }

    function isCameraPresent(){
        cordova.plugins.diagnostic.isCameraPresent(function(present){
            if(present){
                getCameraAuthorizationStatus();
            }else{
                params.errorFn(userMessages.noCamera);
            }
        }, params.errorFn);
    }
    isCameraPresent();
};

关于ios - iOS 9 中的 Cordova 相机插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33732651/

相关文章:

ios - 使 iOS 文件输入直接进入相机应用程序?

c++ - 强制Qt摄像机视频格式

objective-c - CLPlacemark.subThoroughfare 奇怪的字符?

iphone - 如何将ios音频上传到服务器?

android - 使用 chrome 进行远程调试仅适用于较旧的人行横道版本 [cordova 4]

android - 在 Android 上打开手电筒?最兼容的方法?

objective-c - 无法缩短 NSString 长度

ios - 创建一个带有 Storyboard的自定义 cocoa pod

javascript - 以 Angular 返回页面时加载提要不起作用

javascript - 在 Android 4.1.2 Phonegap/Sencha 触摸应用程序中隐藏文本选择图标