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

What is the correct way to check whether the browser supports Web NFC API or not? #434

Closed
FluorescentHallucinogen opened this issue Nov 5, 2019 · 26 comments · Fixed by #435
Assignees

Comments

@FluorescentHallucinogen
Copy link
Contributor

What is the correct way to check whether the browser supports Web NFC API or not?

In the previous version of API, I've used the following:

if ('nfc' in navigator) {
  // use Web NFC API
}

Is

if ('NDEFReader' in window && 'NDEFWriter' in window) {
  // use Web NFC API
}

the correct way?

It would also really nice to have a property (or something) to check whether the device has NFC chip or not.

@FluorescentHallucinogen
Copy link
Contributor Author

Are there NFC chips that support only reading, but not writing NFC tags? If yes, how to detect this?

@beaufortfrancois
Copy link
Collaborator

Indeed!

if ('NDEFReader' in window) { /* ... Scan NDEF Tags */ }
if ('NDEFWriter' in window) { /* ... Write NDEF Tags */ }

You can also use Permissions API

try {
  const status = await navigator.permissions.query({ name: 'nfc'});
}
catch(e) {
  // No Web NFC support
}

@beaufortfrancois
Copy link
Collaborator

To check whether the device has NFC chip or not, you can call scan() or push()and it will reject with an appropriate error if that's the case.

@beaufortfrancois
Copy link
Collaborator

Are there NFC chips that support only reading, but not writing NFC tags? If yes, how to detect this?

I don't think so.

@FluorescentHallucinogen
Copy link
Contributor Author

@beaufortfrancois What about adding this to the spec?

@beaufortfrancois
Copy link
Collaborator

Which parts?

Good luck for your talk by the way ;)

@FluorescentHallucinogen
Copy link
Contributor Author

@beaufortfrancois

Good luck for your talk by the way ;)

Thanks! This is workshop, not talk. :)

Which parts?

About browser feature detection: #434 (comment).

@beaufortfrancois
Copy link
Collaborator

I'll do this.
Feel free to file more issues as you play with Web NFC.
Note that current Chromium implementation doesn't show prompts yet, but will really soon.

@beaufortfrancois beaufortfrancois self-assigned this Nov 5, 2019
@FluorescentHallucinogen
Copy link
Contributor Author

Note that current Chromium implementation doesn't show prompts yet, but will really soon.

I know. :) Thanks!

@beaufortfrancois
Copy link
Collaborator

beaufortfrancois commented Nov 5, 2019

@FluorescentHallucinogen
Copy link
Contributor Author

@beaufortfrancois Just curious, what if I call navigator.permissions.query({ name: 'nfc'}), but NFC is disabled in the OS settings? Does your CL cover this use case similar to the following (example for Web Geolocation API)?

1
2
3
4

@beaufortfrancois
Copy link
Collaborator

navigator.permissions.query({ name: 'nfc'}) will resolve with a status of "denied".

navigator.permissions.query({ name: "nfc" }).then(permissionStatus => {
  console.log(`NFC user permission: ${permissionStatus.state}`);
  permissionStatus.onchange = _ => {
    console.log(`NFC user permission changed: ${permissionStatus.state}`);
  };
});

@beaufortfrancois
Copy link
Collaborator

https://w3c.github.io/web-nfc/#feature-support has been added to the spec.

@FluorescentHallucinogen
Copy link
Contributor Author

@beaufortfrancois

navigator.permissions.query({ name: 'nfc'}) will resolve with a status of "denied".

Silently? It would be great to let the user know what went wrong and provide the ability to enable NFC in the OS settings (see screenshots in #434 (comment)).

P.S. Sorry for offtopic.

@beaufortfrancois
Copy link
Collaborator

navigator.permissions.query will always be silent. What we could use is navigator.permissions.request, however it is not functional at the moment in Chromium.

Having something similar to Geolocation makes sense to me.
I'll ping appropriate folks. Thanks!

@FluorescentHallucinogen
Copy link
Contributor Author

@beaufortfrancois

Just to clarify, will there be a separate permission prompt for writing NFC (alongside permission prompt for reading NFC) or one single permission prompt for the whole Web NFC API (both reading and writing)?

Use case: what if I want to allow the web app to only read NFC tags but not write?

I'm not familiar with the process of merge code in Chromium. I see that the NFC permission change was merged, but I've just tested in Chrome Canary for Android with chrome://flags/#enable-webnfc flag enabled today and for some reason don't see the permission prompt. Why? Any ETA?

@beaufortfrancois
Copy link
Collaborator

Just to clarify, will there be a separate permission prompt for writing NFC (alongside permission prompt for reading NFC) or one single permission prompt for the whole Web NFC API (both reading and writing)?

In Chromium, there will be one single permission prompt for using the Web NFC API.

I'm not familiar with the process of merge code in Chromium. I see that the NFC permission change was merged, but I've just tested in Chrome Canary for Android with chrome://flags/#enable-webnfc flag enabled today and for some reason don't see the permission prompt. Why? Any ETA?

I'm currently working on making sure tests pass for https://chromium-review.googlesource.com/c/chromium/src/+/1895345. Once it's solved, it will be pushed to Chromium. I hope this will be done this week or next week.

@FluorescentHallucinogen
Copy link
Contributor Author

@beaufortfrancois

In Chromium, there will be one single permission prompt for using the Web NFC API.

Any plans to separate permission prompts for reading and writing?

From the name of https://chromium-review.googlesource.com/c/chromium/src/+/1895345[WebNFC] Add permission prompt for NDEFWriter.push it's not clear is this for writing only?

What if I try to read NFC tags (if user didn't previously allow Web NFC), will the permission prompt be shown?

@beaufortfrancois
Copy link
Collaborator

@beaufortfrancois

In Chromium, there will be one single permission prompt for using the Web NFC API.

Any plans to separate permission prompts for reading and writing?

Nope. Not in Chromium.

From the name of https://chromium-review.googlesource.com/c/chromium/src/+/1895345[WebNFC] Add permission prompt for NDEFWriter.push it's not clear is this for writing only?

What if I try to read NFC tags (if user didn't previously allow Web NFC), will the permission prompt be shown?

Yup. Once user approves Web NFC, prompt won't be shown anymore.

@beaufortfrancois
Copy link
Collaborator

For info, I'm currently adding "second" prompt to Chrome to turn on NFC if needed. See https://chromium-review.googlesource.com/c/chromium/src/+/1940241

image

@beaufortfrancois
Copy link
Collaborator

@FluorescentHallucinogen All bits have finally landed in Chrome Canary for Android. You can give it a try at https://permission.site

@FluorescentHallucinogen
Copy link
Contributor Author

@beaufortfrancois That's awesome! Thanks for your work!

One detail: what about changing the word "phone" in texts to more generic "device"? What if my device is e.g. tablet, not phone?

@beaufortfrancois
Copy link
Collaborator

@FluorescentHallucinogen I'll share this feedback with the team. Thanks!

@FluorescentHallucinogen
Copy link
Contributor Author

@beaufortfrancois Any plans on Origin Trial for Web NFC API?

@riju
Copy link
Collaborator

riju commented Dec 26, 2019

@FluorescentHallucinogen : We are planning for M81, so around end of Jan '20.

@beaufortfrancois
Copy link
Collaborator

To know more about the Origin Trial for Web NFC, I'd recommend having a look at https://web.dev/nfc/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants