Skip to content

Commit 4718a65

Browse files
Fixed a bug with permissions
1 parent 200fe24 commit 4718a65

File tree

5 files changed

+12484
-6952
lines changed

5 files changed

+12484
-6952
lines changed

App.js

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
import React, { Component } from 'react';
2-
import {StyleSheet, PermissionsAndroid, Platform, ToastAndroid} from 'react-native';
2+
import {StyleSheet, Platform, ToastAndroid} from 'react-native';
33
import {
44
ViroImage,
55
ViroNode,
66
ViroARScene,
77
ViroText,
88
ViroConstants,
9-
ViroARSceneNavigator
9+
ViroARSceneNavigator,
10+
ViroFlexView
1011
} from 'react-viro';
1112
import Geolocation from '@react-native-community/geolocation';
1213
import CompassHeading from 'react-native-compass-heading';
13-
14+
import {requestMultiple, PERMISSIONS, RESULTS} from 'react-native-permissions';
1415

1516
const Toast = (message) => {
1617
ToastAndroid.showWithGravityAndOffset(
1718
message,
1819
ToastAndroid.LONG,
1920
ToastAndroid.BOTTOM,
20-
25,
21-
50
21+
25, 50
2222
);
2323
}
2424

25-
const MAPS_API_KEY = 'your-api-key'
25+
const MAPS_API_KEY = ''
2626
const PlacesAPIURL = (lat,lng) => `https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${lat},${lng}&radius=50&key=${MAPS_API_KEY}`;
2727

2828
const distanceBetweenPoints = (p1, p2) => {
@@ -46,6 +46,7 @@ class HelloWorldSceneAR extends Component {
4646
constructor(props) {
4747
super(props);
4848
this.state = {
49+
cameraReady: false,
4950
locationReady: false,
5051
location: undefined,
5152
nearbyPlaces: [],
@@ -62,22 +63,28 @@ class HelloWorldSceneAR extends Component {
6263
}
6364

6465
componentDidMount(){
65-
PermissionsAndroid.check('android.permission.ACCESS_FINE_LOCATION')
66-
.then((result) => {
67-
if(result){
68-
this.getCurrentLocation();
66+
const permissions = Platform.select({
67+
ios: [PERMISSIONS.IOS.CAMERA, PERMISSIONS.IOS.LOCATION_WHEN_IN_USE],
68+
android: [PERMISSIONS.ANDROID.CAMERA, PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION]
69+
});
70+
71+
requestMultiple(permissions).then((statuses) => {
72+
if(Platform.OS == 'ios'){
73+
console.log('Camera', statuses[PERMISSIONS.IOS.CAMERA]);
74+
console.log('Location', statuses[PERMISSIONS.IOS.LOCATION_WHEN_IN_USE]);
75+
this.setState({
76+
locationReady: statuses[PERMISSIONS.IOS.LOCATION_WHEN_IN_USE] === RESULTS.GRANTED,
77+
cameraReady: statuses[PERMISSIONS.IOS.CAMERA] === RESULTS.GRANTED
78+
}, this.getCurrentLocation);
6979
}
7080
else{
71-
PermissionsAndroid.request('android.permission.ACCESS_FINE_LOCATION')
72-
.then((granted) => {
73-
if(granted){
74-
this.getCurrentLocation();
75-
}
76-
})
81+
console.log('Camera', statuses[PERMISSIONS.ANDROID.CAMERA]);
82+
console.log('Location', statuses[PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION]);
83+
this.setState({
84+
locationReady: statuses[PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION] === RESULTS.GRANTED,
85+
cameraReady: statuses[PERMISSIONS.ANDROID.CAMERA] === RESULTS.GRANTED
86+
}, this.getCurrentLocation);
7787
}
78-
})
79-
.catch((error) => {
80-
8188
});
8289

8390
CompassHeading.start(3, (heading) => {
@@ -93,14 +100,15 @@ class HelloWorldSceneAR extends Component {
93100
}
94101

95102
getCurrentLocation = () => {
96-
const geoSuccess = (result) => {
97-
this.setState({
98-
location: result.coords,
99-
locationReady: true
100-
}, this.getNearbyPlaces);
101-
};
102-
103-
this.listener = Geolocation.watchPosition(geoSuccess, (error) => {}, {distanceFilter: 10});
103+
if(this.state.cameraReady && this.state.locationReady){
104+
const geoSuccess = (result) => {
105+
this.setState({
106+
location: result.coords
107+
}, this.getNearbyPlaces);
108+
};
109+
110+
this.listener = Geolocation.watchPosition(geoSuccess, (error) => {}, {distanceFilter: 10});
111+
}
104112
}
105113

106114
latLongToMerc = (latDeg, longDeg) => {
@@ -141,6 +149,7 @@ class HelloWorldSceneAR extends Component {
141149
fetch(URL)
142150
.then((response) => response.json())
143151
.then((responseJson) => {
152+
//console.log(responseJson)
144153
if(responseJson.status === 'OK'){
145154
const places = responseJson.results.map((rawPlace) => {
146155
return {
@@ -153,6 +162,9 @@ class HelloWorldSceneAR extends Component {
153162
});
154163
this.setState({nearbyPlaces: places});
155164
}
165+
else{
166+
console.warn(responseJson.status)
167+
}
156168
})
157169
.catch((error) => {
158170
console.error(error)
@@ -181,7 +193,7 @@ class HelloWorldSceneAR extends Component {
181193
render() {
182194
return (
183195
<ViroARScene onTrackingUpdated={this._onInitialized} >
184-
{(this.state.locationReady) && this.placeARObjects()}
196+
{(this.state.locationReady && this.state.cameraReady) && this.placeARObjects()}
185197
</ViroARScene>
186198
);
187199
}
@@ -212,6 +224,7 @@ export default class App extends React.Component{
212224
render(){
213225
return(
214226
<ViroARSceneNavigator
227+
worldAlignment={'GravityAndHeading'}
215228
autofocus={true}
216229
initialScene={{
217230
scene: HelloWorldSceneAR,

ios/Podfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ target 'myviroapp' do
88

99
use_react_native!(:path => config["reactNativePath"])
1010

11+
permissions_path = '../node_modules/react-native-permissions/ios'
12+
1113
pod 'ViroReact', :path => '../node_modules/react-viro/ios/'
1214
pod 'ViroKit_static_lib', :path => '../node_modules/react-viro/ios/dist/ViroRenderer/static_lib'
15+
pod 'Permission-Camera', :path => "#{permissions_path}/Camera"
16+
pod 'Permission-LocationWhenInUse', :path => "#{permissions_path}/LocationWhenInUse"
17+
pod 'Permission-LocationAccuracy', :path => "#{permissions_path}/LocationAccuracy"
1318

1419
target 'myviroappTests' do
1520
inherit! :complete

ios/myviroapp/Info.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
</dict>
3838
</dict>
3939
</dict>
40+
<key>NSCameraUsageDescription</key>
41+
<string></string>
4042
<key>NSLocationWhenInUseUsageDescription</key>
4143
<string></string>
4244
<key>UILaunchStoryboardName</key>

0 commit comments

Comments
 (0)