4

I noticed that my little vanilla JS script to get people to connect to my "dapp" works fine on desktop with metamask, but doesn't work with metamask mobile app.

It gives me an error as if the "Provider" was not set.

const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const connectbtn = document.querySelector('.connectbtn');
connectbtn.addEventListener('click',enabler);

async function enabler(){
    
        try {
  await ethereum.request({
    method: 'wallet_switchEthereumChain',
    params: [{ chainId: '0x38' }],
  });
} catch (switchError) {
  // This error code indicates that the chain has not been added to MetaMask.
  if (error.code === 4902) {
    try {
      await ethereum.request({
        method: 'wallet_addEthereumChain',
        params: [{ chainId: '0x38', rpcUrl: 'https://bsc-dataseed.binance.org/' /* ... */ }],
      });
    } catch (addError) {
      // handle "add" error
    }
  }
  // handle other "switch" errors
}
 doing();
 
};

async function doing() {
    await provider.send("eth_requestAccounts", []);

    
    
    let addressconnected = document.querySelector(".addressConnected") ;
    addressconnected.innerHTML =`Connected`;
    

    // console.log("Account:", await signer.getAddress());
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Tryneed
  • 65
  • 3

1 Answers1

0

Browser is not injecting the Window.ethereum object in android. First force stop your Metamask then allow access for this app and try it would work fine.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Albert Logic Einstein Aug 08 '23 at 06:28