1

I built a website and set up the web3 to interactive with my smart contract. Everything is fine on desktop but if i use Metamask browser visiting the website on mobile then i can't connect it to Metamask. Here's my web3 provider setting enter image description here

I think there's something wrong on "web3.currentProvider" so that the function below "requestAccounts()" doesn't work.

What should i do to fix this problem?

1 Answer 1

1

You shouldn't be using web3.currentProvider anymore. This is the old way. Do this instead:

if (window.ethereum) {
    web3 = new Web3(window.ethereum);
} else {
    // Code for HttpProvider remains unchanged
}
await window.ethereum.send('eth_requestAccounts');

See How to Connect Web3.js to MetaMask in 2021

2
  • Ty.It works but still wondering why the old way is only disabled on mobile. Commented Oct 2, 2021 at 23:09
  • From the documentation: "MetaMask still injects a dummy object at window.web3, in order to issue warnings when websites attempt to access window.web3". Since developers are using the desktop version to develop, they kept window.web3 to show the warning and redirect to the documentation. On mobile we don't really look at the browser's console so they removed it entirely. More info: docs.metamask.io/guide/provider-migration.html
    – KonyTech
    Commented Oct 3, 2021 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.