{"action":"created","issue":{"url":"https://api.github.com/repos/yonahforst/react-native-permissions/issues/228","repository_url":"https://api.github.com/repos/yonahforst/react-native-permissions","labels_url":"https://api.github.com/repos/yonahforst/react-native-permissions/issues/228/labels{/name}","comments_url":"https://api.github.com/repos/yonahforst/react-native-permissions/issues/228/comments","events_url":"https://api.github.com/repos/yonahforst/react-native-permissions/issues/228/events","html_url":"https://github.com/yonahforst/react-native-permissions/pull/228","id":297070637,"number":228,"title":"Fix of bugs related to permission location type \"always/whenInUse\"","user":{"login":"andwaal","id":2964903,"avatar_url":"https://avatars3.githubusercontent.com/u/2964903?v=4","gravatar_id":"","url":"https://api.github.com/users/andwaal","html_url":"https://github.com/andwaal","followers_url":"https://api.github.com/users/andwaal/followers","following_url":"https://api.github.com/users/andwaal/following{/other_user}","gists_url":"https://api.github.com/users/andwaal/gists{/gist_id}","starred_url":"https://api.github.com/users/andwaal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andwaal/subscriptions","organizations_url":"https://api.github.com/users/andwaal/orgs","repos_url":"https://api.github.com/users/andwaal/repos","events_url":"https://api.github.com/users/andwaal/events{/privacy}","received_events_url":"https://api.github.com/users/andwaal/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-02-14T12:00:29Z","updated_at":"2018-02-14T13:06:29Z","closed_at":null,"author_association":"NONE","pull_request":{"url":"https://api.github.com/repos/yonahforst/react-native-permissions/pulls/228","html_url":"https://github.com/yonahforst/react-native-permissions/pull/228","diff_url":"https://github.com/yonahforst/react-native-permissions/pull/228.diff","patch_url":"https://github.com/yonahforst/react-native-permissions/pull/228.patch"},"body":"This PR fixes multiple bugs I encountered related to location permission and type _always_.\r\n\r\nMy initial problem was that after first requesting permission _whenInUse_ I did not manage to escalate the permission to _always_, even though this should be possible.\r\n\r\n\r\nThis was caused by the _request()_ method starting with a call to:\r\n```javascript\r\nNSString *status = [RNPLocation getStatusForType:nil];\r\n```\r\nSince we already had _kCLAuthorizationStatusAuthorizedWhenInUse_ rights from the first permission request, we would never enter:\r\n```javascript\r\nNSString *status = [RNPLocation getStatusForType:nil];\r\nif (status == RNPStatusUndetermined) {\r\n  self.completionHandler = completionHandler;\r\n```\r\nSo the request for permission type _always_ would never be triggered.\r\n\r\n\r\nSo what I did:\r\n\r\n\r\n* Since we never will get _RNPStatusUndetermined_ when we try to ecalate permission from _whenInUse_ to _always_, I changed to logic so that the only status that causes us not to ask for permission is _RNPStatusAuthorized_.\r\n\r\n\r\n* Next I added a init() method to setup or _locationManger_.  According to my observations AND https://stackoverflow.com/questions/30106341/swift-locationmanager-didchangeauthorizationstatus-always-called/30107511  the delegate is called once when the _CLLocationManager_ is initialized. \r\nThis caused all kinds of wierd behavior when trying to escalate permission, since the _CLLocationManager_ was initialized for each request, every permission request caused two _didChangeAuthorizationStatus callbacks.\r\nIn the case of trying to _escalate_ permission we would already have the permission _kCLAuthorizationStatusAuthorizedWhenInUse_ from the first request, causing us to enter: \r\n    ```javascript\r\n    (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus: CLAuthorizationStatus)status {\r\n      if (status != kCLAuthorizationStatusNotDetermined) {\r\n    ````\r\n    which then would trigger the callback to RN. This would happen just after initializing the _locationManger_ in the request method, before we actually had requested the _escalated_ permission. \r\nThis would then return _Denied_ to the client.\r\nBy holding a variable who checks if its initial callback, we could remove the check for _kCLAuthorizationStatusNotDetermined_ in the delegate method and always treat it properly.\r\n\r\n* Fixed #168 by keeping a copy of type in _lastTypeRequested_. By doing this we know the requested type, and may use it in _didChangeAuthorizationStatus_.\r\n\r\n* Using the status returned in the _didChangeAuthorizationStatus_ instead of fetching it from _CLLocationManager_. This seems to remove the need for the _dispatch_after(dispatch_time)_ hack.\r\n\r\n* Updated the example project to work with permission type _always_.\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/yonahforst/react-native-permissions/issues/comments/365601533","html_url":"https://github.com/yonahforst/react-native-permissions/pull/228#issuecomment-365601533","issue_url":"https://api.github.com/repos/yonahforst/react-native-permissions/issues/228","id":365601533,"user":{"login":"andwaal","id":2964903,"avatar_url":"https://avatars3.githubusercontent.com/u/2964903?v=4","gravatar_id":"","url":"https://api.github.com/users/andwaal","html_url":"https://github.com/andwaal","followers_url":"https://api.github.com/users/andwaal/followers","following_url":"https://api.github.com/users/andwaal/following{/other_user}","gists_url":"https://api.github.com/users/andwaal/gists{/gist_id}","starred_url":"https://api.github.com/users/andwaal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andwaal/subscriptions","organizations_url":"https://api.github.com/users/andwaal/orgs","repos_url":"https://api.github.com/users/andwaal/repos","events_url":"https://api.github.com/users/andwaal/events{/privacy}","received_events_url":"https://api.github.com/users/andwaal/received_events","type":"User","site_admin":false},"created_at":"2018-02-14T13:06:29Z","updated_at":"2018-02-14T13:06:29Z","author_association":"NONE","body":"After more testing I found that my implementation in this PR is flawed and should probably not be merged yet.\r\nIt seems like the initial trigger of _didChangeAuthorizationStatus_ only happens when your app has never requested permission before.\r\n\r\nThis causes my implementation not to work if someone where to:\r\n1. Start app - request _inUsePermission_\r\n2. Terminate app\r\n3. Open app - request _always_ rights.  -> Here my solution would case the callback never to be called.\r\n\r\nI will try to figure out a solution for this."}}
{"actor":{"display_login":"andwaal"}}