I created a site using web3.ys with some features, without react and node. How can I make it so that when entering from a mobile device and when clicking on the button, the user is transferred to the mobile metamask and immediately opens the browser with my site?
1 Answer
Here is what I use:
export function connectMetaMask(chainId) {
if (!window.ethereum && isMobile()) connectMetamaskMobile();
else return metaMaskConnector.activate(chainId);
}
export const connectMetamaskMobile = () => {
const dappUrl = window.location.href.split("//")[1].split("/")[0];
const metamaskAppDeepLink = "https://metamask.app.link/dapp/" + dappUrl;
window.open(metamaskAppDeepLink, "_self");
};
This is described in the MetaMask Docs.
I'm using this together with web3-react, however you only need the connectMetaMaskMobile
part. Check this out if you want more info on detecting mobile browsers.
-
-
The code is not very complex, add in the function
connectMetaMaskMobile
somewhere and when a user clicks on a button, execute that function..– phazeJun 9, 2022 at 11:10 -
<button onclick="functionToExecute(connectMetaMaskMobile)">Click</button> Right?– CerberusJun 9, 2022 at 11:16
-
more like: <button onclick="connectMetaMaskMobile()">Click</button>. When using normal html with js, you also take out the 'export' keyword.– phazeJun 9, 2022 at 11:18
-