2015-11-16 4 views
0

Я использую Cordova 5.3.3 и Apache Camera Plugin 1.2.0 на IOS 9. Я могу сфотографировать с камерой, однако, когда я пытаюсь получить снимок с фотографии библиотека возвращается к камере, и я получаю сообщение об ошибке «не имеет доступа к активам» в обратном вызове ошибки cordova. Я использую следующий код.Плагин камеры Cordova в IOS 9

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

Когда я проверяю права приложения, я вижу, что у него есть доступ к Камере, но не к фотографиям. Это проблема? Не добавляет ли Кордова необходимые функции при добавлении плагина?

ответ

3

Я столкнулся с аналогичной проблемой при использовании cordova-plugin-camera на iOS: не было разрешения на сохранение фотографии в приложении «Фото» после съемки фотографии, поэтому сохранение ее в Camera Roll было неудачным.

Я работал над проблемой, используя 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(); 
}; 
+0

я, наконец, решил это issue..it не был связан с Cordova плагина однако 10xs для ответа – jimny

+0

@jimny как вы решить Это? – Anthony

+0

Coask @jimny, как это было решено ??? – Cozzbie