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

iOS doesn't rerender component after grant permissions [BUG] #31

Open
WebTravel opened this issue Sep 5, 2021 · 0 comments
Open

iOS doesn't rerender component after grant permissions [BUG] #31

WebTravel opened this issue Sep 5, 2021 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@WebTravel
Copy link

WebTravel commented Sep 5, 2021

So, I noticed the difference behavior in chrome android and safari iOS browsers. I have the next code

export const QRCodeScanner = ({ onDecode, onClose, onDecodeError }) => {
    const status = useCameraPermission();

    const handleResult = useCallback(
        (result, error) => {
            result && onDecode(result.getText());
        },
        [onDecode, onDecodeError],
    );

    return (
        <div>
            {status === "granted" && (
                <QrReader
                    onResult={handleResult}
                    constraints={{
                        aspectRatio: 1,
                        facingMode: "environment",
                    }}
                    videoId="react-qr-reader"
                />
            )}
        </div>
    );
};

And it works for safari because QrReader component appears to page only AFTER user allows permission, pressing the button "Allow camera" in prompt window in safari IOS browser. And this works great! But the same code doesn't work in chrome in android browser, because the prompt window doesn't appear and user can't to allow permission. I suppose the prompt window in safari browser works outside the component while the prompt window in chrome android works inside it.

So, the next code will works in chrome android but doesn't work in safari IOS. After user allow use the camera and status updated doesn't happen anything.

export const QRCodeScanner = ({ onDecode, onClose, onDecodeError }) => {
    const status = useCameraPermission();

    const handleResult = useCallback(
        (result, error) => {
            result && onDecode(result.getText());
        },
        [onDecode, onDecodeError],
    );

    return (
        <div>
                <QrReader
                    onResult={handleResult}
                    constraints={{
                        aspectRatio: 1,
                        facingMode: "environment",
                    }}
                    videoId="react-qr-reader"
                />
        </div>
    );
};

And I reproduce the hook code. I wrote it, because I can check permissions before the QrReader will render to my page. And really needed functionality.

export const useCameraPermission = () => {
    const [status, setStatus] = useState<PermissionState>();

    const checkPermission = useCallback(() => {
        !window.navigator && setStatus("granted");
        window?.navigator?.permissions
            ? window.navigator.permissions
                  .query({ name: "camera" })
                  .then((status) => {
                      setStatus(status.state);
                  })
                  .catch((err) => err.name === "NotAllowedError" && setStatus("denied"))
            : window?.navigator?.mediaDevices
                  .getUserMedia({ video: true })
                  .then((stream) => {
                      stream.active && setStatus("granted");
                  })
                  .catch((err) => {
                      err.name === "NotAllowedError" && setStatus("denied");
                  });
    }, []);

    useEffect(checkPermission, [checkPermission]);

    return status;
};

Any workaround for solving this?

@JonatanSalas JonatanSalas self-assigned this Sep 10, 2021
@JonatanSalas JonatanSalas added the bug Something isn't working label Sep 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants