swift中的相机和照片库的隐私问题

时间:2016-08-11 19:17:50

标签: ios swift

我的应用程序最初在各种设备上访问相机和照片库时都没有问题。

现在我发现在某些设备上我无法访问相机或照片库,在我尝试访问相机和照片后,应用程序在隐私设置中根本不显示。

无论我做什么,我都无法让IOS识别我的应用程序。

如何访问相机和相片库,让我的应用显示在隐私设置中? 代码:

<html>
  <head>
     <title>My experiment</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script src="jspsych-5.0.3/jspsych.js"></script>
        <script src="jspsych-5.0.3/plugins/jspsych-text.js"></script>
        <script src="jspsych-5.0.3/plugins/jspysch-single-stim.js"></script>
        <link href="jspsych-5.0.3/css/jspsych.css" rel="stylesheet" type="text/css"></link>
    </head>
    <body>
    </body>
    <script>

    /* define welcome message block */
    var welcome_block = {
        type: "text"
        text: "Welcome to the experiment. Press any key to begin."
    };

    /*define instructions block*/
    var instructions_block = {
        type:"text",
        text:"<p>In this experiment,a circle will appear in the center" +
            "of the screen.</p><p>If the circle is <strong>blue</strong>," +
            "press the letter F on the keyboard as fast as you can.</p>" +
            "<p>If the circle is <strong>orange</strong>, do not press " +
            "any key.</p>"+
            "<div class ='left center-conten'><img src='img/blue.png'></img>" +
            "<p class = 'small'><strong>Press the F key</strong></p></div>" +
            "div class='right center-content'><img src='img/orang.png'></img>" +
            "<p class='small'><strong>Do not press a key</strong></p></div>" +
            "<p>Press any key to begin.</p>"
        timing_post_trial: 2000     
    };

    var test_stimuli = [
        {
            stimulus: 'img/blue.png',
            data: { response: 'go'}
        },
        {
            stimulus: 'img/orange.png',
            data: { response: 'no-go'}
        }
    ];

    var all_trials = jsPsych.randomization.repeat(test_stimuli,10);


    var post_trial_gap = function() {
        return Math.floor( math.random() * 1500) + 750;
    }

    var test_block = {
        type: 'single-stim',
        choices: ['F'],
        timing_response: 1500,
        timing_post_trial: post_trial_gap,
        on_finish: function(data){
            var correct = false;
            if(data.response == 'go' && data.rt > -1){
                correct = true;
            } else if(data.response == 'no-go' && data.rt == -1){
                correct = true;
            }
            jsPsych.data.addDataToLastTrial({correct: correct});
        },            
        timeline: all_trials
    };

/*define debrief block*/
    function getSubjectData() {

        var trials = jsPsych.data.getTrialsofType('single-stim');

        var sum_rt = 0;
        var correct_trial_count = 0;
        var correct_rt_count = 0;
        for (var i = 0; i < trials.length; i++) {
            if (trials[i].correct == true) {
                correct_trial_count++;
                if(trials[i].rt > -1){
                    sum_rt += trials[i].rt;
                    correct_rt_count++;
                }
            }
        }
        return {
            rt: Math.floor(sum_rt/correct_rt_count),
            accuracy: Math.floor(correct_trial_count / trials.length * 100)
        }
    }

    var debrief_block = {
        type: "text",
        text: function() {
            var subject_data = getSubjectData();
            return"<p>You responded correctly on "+subject_data.accuracy+"% of "+
            "the trials.</p><p>Your average response time was <strong>"+
            subject_data.rt + "ms</strong>.  Press any key to complete the "+
            "experiment. Thank you!</p>";
        }
    };

    /*create experiment timeline array*/
    var timeline = [];
    timeline.push(welcome_block);
    timeline.push(instructions_block);
    timeline.push(test_block);
    timeline.pysh(debrief_block)

    /*start the experiment*/
    jsPsych.init({ 
        experiment_structure: experiment,
        on_finish: function() {
            jsPsych.data.displayData();
        }
    });
</script>
</html>

2 个答案:

答案 0 :(得分:0)

我使用此代码进行可访问性检查并在需要时请求它:

import AVFoundation
import Photos

func getCameraAccessibilityAndRequestIfNeeded(completion: (isAccesible: Bool)->Void) {
    let authorizationState = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)
    switch authorizationState {
    case .NotDetermined:
        AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (didAllow) in
            completion(isAccesible: didAllow)
        })
    case .Restricted:
        completion(isAccesible: false)
    case .Denied:
        completion(isAccesible: true)
    case .Authorized:
        completion(isAccesible: true)
 }
}

func getPhotoRollAccessibilityAndRequestIfNeeded(completion: (isAccesible: Bool)->Void) {
    PHPhotoLibrary.requestAuthorization { (status) in
        switch status {
        case .Authorized:
            completion(isAccesible: true)
        case .Restricted, .Denied , .NotDetermined:
            completion(isAccesible: false)
        }
    }
}

<强>用法:

self.getCameraAccessibilityAndRequestIfNeeded { (isAccesible) in
            if isAccesible {
                // Access confirmed
            } else {
                // No access
            }
        }

        self.getPhotoRollAccessibilityAndRequestIfNeeded { (isAccesible) in
            if isAccesible {
                // Access confirm
            } else {
                // No access
            }
        }

答案 1 :(得分:0)

最后答案非常简单:

我正在让按钮操作中的imagePicker = UIImagePickerController()将其移到类名下面,并且相机权限又回来了。