2017-02-24 139 views
3

我得到這個錯誤:爲什麼PermissionAndroid不起作用?

Reference Error: can't find variable PermissionsAndroid

這是我的代碼:

的AndroidManifest.xml:

<uses-permission android:name="android.permission.CAMERA" /> 

index.android.js:

constructor(props) { 
    super(props); 
    this.state = { 
    message: 'Hi world' 
    }; 
    this.requestCameraPermission(); 
} 


async requestCameraPermission() { 
    try { 
    const granted = await PermissionsAndroid.requestPermission(
     PermissionsAndroid.PERMISSIONS.CAMERA, 
     { 
     'title': 'Cool Photo App Camera Permission', 
     'message': 'Cool Photo App needs access to your camera ' + 
        'so you can take awesome pictures.' 
     } 
    ) 
    if (granted === PermissionsAndroid.RESULTS.GRANTED) { 
     console.log("You can use the camera") 
    } else { 
     console.log("Camera permission denied") 
    } 
    } catch (err) { 
    console.warn(err) 
    } 
} 

回答

5

也許你忘了在使用它之前導入組件:

import { PermissionsAndroid } from 'react-native';