如何导入权限模块对本机项目做出反应?

时间:2017-08-01 09:14:41

标签: reactjs typescript react-native permissions

我是本地和打字稿的新人。我想为我的本机项目创建权限模块,我在Yonah's projectFacebook's permissions project下面分隔了两个链接

Additionaly

  

npm install,npm start,yarn run build --watch and react-native run-android

命令运行顺利,但权限模块不使用它,我不提出这个模块添加我的项目 我在下面尝试链接;

const permission = require('react-native-permissions')
import React from 'react'
import {
StyleSheet,
TouchableHighlight,
Text,
View,
Alert,
AppState,
Platform,
PermissionsAndroid,
} from 'react-native'

interface Props {
names: string[]
}

interface PermissionsState {
status: PermissionStatus
}

type PermissionStatus = 'granted' | 'denied' | 'never_ask_again' | ''

export class permissions extends React.Component<Props, PermissionsState>
{
constructor(props: Props, context: any)
// tslint:disable-next-line:brace-style
{
    super(props, context)

    this.state = {
        status: '',
    }
}

public render()
// tslint:disable-next-line:brace-style
{
    return (<View> a </View>)
}
 }

 async function requestCameraPermission() {
try {
    const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.CAMERA,

    )
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        // console.log("kamerayı kullanabilirsiniz")
    } else {
        // console.log('kamerayı kullanamazsınız')
    }
  } catch (err) {
    // console.warn(err)
  }
  }

  export default permission

1 个答案:

答案 0 :(得分:0)

requestCameraPermission函数不是调用。您可以尝试在

中调用函数
 constructor(props: Props, context: any)
 // tslint:disable-next-line:brace-style
 {
    super(props, context)

    this.requestCameraPermission= this.requestCameraPermission.bind(this);

    this.state = {
       status: '',
    }
 }

 componentWillMount () {
   this.requestCameraPermission();
  .....
 }

对于多个请求,请参阅此示例

export async function requestForPermission() {
try{

const granted = await PermissionsAndroid.requestMultiple(
  [
    PermissionsAndroid.PERMISSIONS.CAMERA,
    PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,

    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
    PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,

    PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
    PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
  ],

)

........

} catch (err) {
  console.warn(err)
}
}

@ ++