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

Add Windows support #530

Merged
merged 6 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 114 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

[![npm version](https://badge.fury.io/js/react-native-permissions.svg)](https://badge.fury.io/js/react-native-permissions)
[![npm](https://img.shields.io/npm/dt/react-native-permissions.svg)](https://www.npmjs.org/package/react-native-permissions)
![Platform - Android and iOS](https://img.shields.io/badge/platform-Android%20%7C%20iOS-yellow.svg)
![Platform - Android, iOS and Windows](https://img.shields.io/badge/platform-Android%20%7C%20iOS%20%7C%20Windows-yellow.svg)
![MIT](https://img.shields.io/dub/l/vibe-d.svg)
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)

A unified permissions API for React Native on iOS and Android.
A unified permissions API for React Native on iOS, Android and Windows.

For Windows only builds 18362 and later are supported.

## Support

| version | react-native version |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------- |
| 2.0.0+ | 0.60.2+ |
| version | react-native version |
| ------- | -------------------- |
| 2.0.0+ | 0.60.2+ |

## Setup

Expand Down Expand Up @@ -173,9 +175,13 @@ Add all wanted permissions to your app `android/app/src/main/AndroidManifest.xml
</manifest>
```

### Windows

Open the project solution file from the `windows` folder. In the app project open `Package.appxmanifest` file. From there you can select which capabilites you want your app to support.

## 🆘 Manual linking

Because this package targets React Native 0.60.0+, you probably won't need to link it manually. Otherwise if it's not the case, follow these additional instructions:
Because this package targets React Native 0.60.0+, you probably won't need to link it manually. Otherwise if it's not the case, follow these additional instructions. You also need to manual link the module on Windows when using React Native Windows prior to 0.63:

<details>
<summary><b>👀 See manual linking instructions</b></summary>
Expand Down Expand Up @@ -231,6 +237,23 @@ public class MainApplication extends Application implements ReactApplication {
}
```

### Windows

1. In `windows/myapp.sln` add the `RNCConfig` project to your solution:

- Open the solution in Visual Studio 2019
- Right-click Solution icon in Solution Explorer > Add > Existing Project
- Select `node_modules\react-native-permissions\windows\RNPermissions\RNPermissions.vcxproj`

2. In `windows/myapp/myapp.vcxproj` ad a reference to `RNPermissions` to your main application project. From Visual Studio 2019:

- Right-click main application project > Add > Reference...
- Check `RNPermissions` from Solution Projects.

3. In `pch.h` add `#include "winrt/RNPermissions.h"`.

4. In `app.cpp` add `PackageProviders().Append(winrt::RNPermissions::ReactPackageProvider());` before `InitializeComponent();`.

</details>

## Understanding permission flow
Expand Down Expand Up @@ -337,6 +360,56 @@ As permissions are not handled in the same way on iOS and Android, this library
└─────────────────┘ └─────────────────┘
```

### Windows flow

```
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ check(PERMISSIONS.WINDOWS.WEBCAM) ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
Is the feature available
on this device ?
│ ╔════╗
├───────────║ NO ║──────────────┐
│ ╚════╝ │
╔═════╗ ▼
║ YES ║ ┌─────────────────────┐
╚═════╝ │ RESULTS.UNAVAILABLE │
│ └─────────────────────┘
Is the permission
requestable ?
│ ╔════╗
├───────────║ NO ║──────────────┐
│ ╚════╝ │
╔═════╗ ▼
║ YES ║ ┌───────────────────┐
╚═════╝ │ RESULTS.BLOCKED / │
│ │ RESULTS.GRANTED │
▼ └───────────────────┘
┌────────────────┐
│ RESULTS.DENIED │
└────────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ request(PERMISSIONS.WINDOWS.WEBCAM) ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
Does the user accept
the request ?
│ ╔════╗
├───────────║ NO ║──────────────┐
│ ╚════╝ │
╔═════╗ ▼
║ YES ║ ┌─────────────────┐
╚═════╝ │ RESULTS.BLOCKED │
│ └─────────────────┘
┌─────────────────┐
│ RESULTS.GRANTED │
└─────────────────┘
```

## API

### Supported permissions
Expand Down Expand Up @@ -394,6 +467,38 @@ PERMISSIONS.IOS.REMINDERS;
PERMISSIONS.IOS.SIRI;
PERMISSIONS.IOS.SPEECH_RECOGNITION;
PERMISSIONS.IOS.STOREKIT;

// Windows permissions

PERMISSIONS.WINDOWS.APPOINTMENTS;
PERMISSIONS.WINDOWS.BLOCKED_CHAT_MESSAGES;
PERMISSIONS.WINDOWS.BLUETOOTH_GATT;
PERMISSIONS.WINDOWS.BLUETOOTH_RFCOMM;
PERMISSIONS.WINDOWS.CHAT;
PERMISSIONS.WINDOWS.CODE_GENERATION;
PERMISSIONS.WINDOWS.CONTACTS;
PERMISSIONS.WINDOWS.DOCUMENTS_LIBRARY;
PERMISSIONS.WINDOWS.ENTERPRISE_AUTHENTICATION;
PERMISSIONS.WINDOWS.HUMAN_INTERFACE_DEVICE;
PERMISSIONS.WINDOWS.INTERNET_CLIENT;
PERMISSIONS.WINDOWS.INTERNET_CLIENT_SERVER;
PERMISSIONS.WINDOWS.LOCATION;
PERMISSIONS.WINDOWS.MICROPHONE;
PERMISSIONS.WINDOWS.MUSIC_LIBRARY;
PERMISSIONS.WINDOWS.OBJECTS_3D;
PERMISSIONS.WINDOWS.PHONE_CALL;
PERMISSIONS.WINDOWS.PHOTO_LIBRARY;
PERMISSIONS.WINDOWS.POINT_OF_SERVICE;
PERMISSIONS.WINDOWS.PRIVATE_NETWORK_CLIENT_SERVER;
PERMISSIONS.WINDOWS.PROXIMITY;
PERMISSIONS.WINDOWS.RECORDED_CALLS_FOLDER;
PERMISSIONS.WINDOWS.REMOVABLE_STORAGE;
PERMISSIONS.WINDOWS.SHARED_USER_CERTIFICATES;
PERMISSIONS.WINDOWS.USB;
PERMISSIONS.WINDOWS.USER_ACCOUNT_INFORMATION;
PERMISSIONS.WINDOWS.VIDEOS_LIBRARY;
PERMISSIONS.WINDOWS.VOIP_CALL;
PERMISSIONS.WINDOWS.WEBCAM;
```

### Permissions statuses
Expand Down Expand Up @@ -519,6 +624,8 @@ checkNotifications().then(({status, settings}) => {

Request notifications permission status and get notifications settings values.

You cannot request notifications permissions on Windows. Disabling or enabling notifications can only be done through the App Settings.

```ts
// only used on iOS
type NotificationOption =
Expand Down Expand Up @@ -637,6 +744,7 @@ request(
Platform.select({
android: PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION,
ios: PERMISSIONS.IOS.LOCATION_WHEN_IN_USE,
windows: PERMISSIONS.WINDOWS.LOCATION,
}),
);
```
11 changes: 9 additions & 2 deletions example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ import theme from './theme';
const {SIRI, ...PERMISSIONS_IOS} = PERMISSIONS.IOS; // remove siri (certificate required)

const PLATFORM_PERMISSIONS = Platform.select<
typeof PERMISSIONS_IOS | typeof PERMISSIONS.ANDROID | {}
| typeof PERMISSIONS_IOS
| typeof PERMISSIONS.ANDROID
| typeof PERMISSIONS.WINDOWS
| {}
>({
ios: PERMISSIONS_IOS,
android: PERMISSIONS.ANDROID,
windows: PERMISSIONS.WINDOWS,
default: {},
});

Expand Down Expand Up @@ -46,7 +50,10 @@ const PermissionRow = ({
status: string;
onPress: () => void;
}) => (
<TouchableRipple onPress={onPress}>
<TouchableRipple
onPress={onPress}
accessible={true}
accessibilityLabel={`${name}:${status}`}>
<List.Item
right={() => <List.Icon color={colors[status]} icon={icons[status]} />}
title={name}
Expand Down
26 changes: 26 additions & 0 deletions example/__test__/Permissions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {driver, By2} from 'selenium-appium';
import {until} from 'selenium-webdriver';

const setup = require('../jest-setups/jest.setup');
jest.setTimeout(50000);

beforeAll(() => {
return driver.startWithCapabilities(setup.capabilites);
});

afterAll(() => {
return driver.quit();
});

describe('Test App', () => {
test('Permissions present', async () => {
await driver.wait(
until.elementLocated(By2.nativeName('CODE_GENERATION:granted')),
);
await driver.wait(
until.elementLocated(By2.nativeName('DOCUMENTS_LIBRARY:unavailable')),
);
await driver.wait(until.elementLocated(By2.nativeName('CONTACTS:denied')));
await driver.wait(until.elementLocated(By2.nativeName('LOCATION:blocked')));
});
});
13 changes: 13 additions & 0 deletions example/jest-setups/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable no-undef */
import {windowsAppDriverCapabilities} from 'selenium-appium';

switch (platform) {
case 'windows':
const webViewWindowsAppId = 'PermissionsExample_nsp2ha5jnb6xr!App';
module.exports = {
capabilites: windowsAppDriverCapabilities(webViewWindowsAppId),
};
break;
default:
throw 'Unknown platform: ' + platform;
}
2 changes: 2 additions & 0 deletions example/jest-setups/jest.setup.windows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable no-undef */
platform = 'windows';
17 changes: 17 additions & 0 deletions example/metro.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
const path = require('path');
const blacklist = require('metro-config/src/defaults/blacklist');

module.exports = {
resolver: {
blacklistRE: blacklist([
// This stops "react-native run-windows" from causing the metro server to crash if its already running
new RegExp(
`${path.resolve(__dirname, 'windows').replace(/[/\\]/g, '/')}.*`,
),
// This prevents "react-native run-windows" from hitting: EBUSY: resource busy or locked, open msbuild.ProjectImports.zip
new RegExp(
`${path
.resolve(__dirname, 'msbuild.ProjectImports.zip')
.replace(/[/\\]/g, '/')}.*`,
),
]),
},
transformer: {
getTransformOptions: async () => ({
transform: {
Expand Down
30 changes: 28 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,52 @@
"version": "0.0.1",
"private": true,
"scripts": {
"appium": "appium",
"clean-modules": "rm -rf ./node_modules/react-native-permissions/{example,node_modules}",
"clean": "rm -rf ./node_modules ./ios/Pods",
"postinstall": "yarn clean-modules && yarn pod-update",
"pod-update": "cd ./ios && pod update && cd ..",
"start": "react-native start",
"reinstall": "yarn clean && yarn install"
"reinstall": "yarn clean && yarn install",
"test:windows": "yarn jest --setupFiles=./jest-setups/jest.setup.windows.js"
},
"dependencies": {
"react": "16.11.0",
"react-native": "0.62.2",
"react-native-paper": "3.8.0",
"react-native-permissions": "../",
"react-native-vector-icons": "6.6.0"
"react-native-vector-icons": "6.6.0",
"react-native-windows": "^0.62.0-0"
},
"devDependencies": {
"@babel/core": "7.9.0",
"@babel/runtime": "7.9.2",
"@react-native-community/eslint-config": "^1.0.0",
"@types/jest": "^24.0.24",
"@types/react": "16.9.34",
"@types/react-native": "0.62.2",
"@types/react-test-renderer": "16.9.2",
"@typescript-eslint/parser": "^2.27.0",
"appium": "1.18.2",
"babel-jest": "^24.9.0",
"eslint": "^6.5.1",
"jest": "^24.9.0",
"metro-react-native-babel-preset": "0.59.0",
"prettier": "^2.0.4",
"react-test-renderer": "16.11.0",
"selenium-appium": "0.0.15",
"selenium-webdriver": "4.0.0-alpha.7",
"typescript": "3.8.3"
},
"jest": {
"preset": "react-native",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
}
}
Loading