Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PermissionUtil #6

Open
wuyunqiang opened this issue Aug 16, 2017 · 1 comment
Open

PermissionUtil #6

wuyunqiang opened this issue Aug 16, 2017 · 1 comment
Labels

Comments

@wuyunqiang
Copy link
Owner

wuyunqiang commented Aug 16, 2017

使用 PermissionUtil.checkPermission(success,fail,permission = [])
/**
 * react-native-permissions 1.0.6
 * Created by wuyunqiang 
 */
import Permissions from 'react-native-permissions'
class PermissionUtil{
    checkPermission = (success,fail,permission = []) => {
        let self = this;
        let flag = true;
        let per = [];
        if(permission.length<=0){
            return;
        }
        Permissions.checkMultiple(permission)
            .then(status => {//直接修改设置 检测不到授权结果
                console.log('status',status);
                Object.keys(status).map((k,v)=>{
                    if(status[k]=="authorized"){
                    }else{
                        per.push(k);
                        flag=false;
                    }
                });
                if(flag){
                    success();
                }else{
                    if(per.length>0){
                        self.request(success,fail,per,0);
                    }
                }
            })
    };

    request = (success,fail,per,i)=>{
        if(i<per.length){
            console.log('per',per);
            this.requestPermission(success,fail,per,i);
        }else{
            success();
        }
    };

    requestPermission = (success,fail,per,i) => {
        let self = this;
        console.log('per[i]',per[i]);
        Permissions.request(per[i])
            .then(res => {
                console.log('res',res);
                if (res != 'authorized') {
                    if(fail){
                        fail();
                        return;
                    }
                    switch (per[i]){
                        case "camera":
                            Alert.alert(
                                '无法使用!',
                                "请授予应用使用相机权限",
                                [
                                    {text: '取消', style: 'cancel'},
                                    // {text: '设置权限', onPress: Permissions.openSettings},
                                ]
                            );
                            break;
                        case "photo":
                            Alert.alert(
                                '无法访问!',
                                "请授予应用访问存储sd卡权限",
                                [
                                    {text: '取消', style: 'cancel'},
                                ]
                            );
                            break;
                        case "location":
                            Alert.alert(
                                '无法访问!',
                                "请授予应用访问位置信息权限",
                                [
                                    {text: '取消', style: 'cancel'},
                                ]
                            );
                            break;
                        case "microphone":
                            Alert.alert(
                                '无法访问!',
                                "请授予应用录音权限",
                                [
                                    {text: '取消', style: 'cancel'},
                                ]
                            );
                            break;
                        case "contacts":
                            Alert.alert(
                                '无法访问!',
                                "请授予应用获取联系人权限",
                                [
                                    {text: '取消', style: 'cancel'},
                                ]
                            );
                            break;
                        default:
                            Alert.alert(
                                '无法访问!',
                                "请授予应用权限",
                                [
                                    {text: '取消', style: 'cancel'},
                                ]
                            );
                            break;
                    }
                } else {
                    self.request(success,fail,per,++i);
                }
            }).catch(e => console.log(e))
    };
}
export default new PermissionUtil();  
@wuyunqiang
Copy link
Owner Author

wuyunqiang commented Jan 26, 2018

Tip:
1:动态权限是Android6.0以上(API 23||23+)才有的如果发现每次请求权限返回的都是true,将/app/build.gradle compileSdkVersion,buildToolsVersion,targetSdkVersion都调整到23及以上版本.
2:每次请求都是false,原因可能是没有在AndroidManifest.xml 里面注册权限。
3:如果已经同意一次权限请求下次在使用该权限时不会提示,只有拒绝后才会每次都弹出提示框。
android 原生权限工具:
#54

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant