Skip to content

Commit

Permalink
chore: improve the deeplink handling for MetaMask SDK on iOS (#1002)
Browse files Browse the repository at this point in the history
* chore: improve the deeplink handling for MetaMask SDK on iOS

* chore: wip

* chore: cleaning
  • Loading branch information
omridan159 authored Aug 28, 2024
1 parent 5af7cbf commit 6b7f45e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
34 changes: 34 additions & 0 deletions packages/examples/reactNativeSdkDemo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,37 @@ yarn ios:device
```sh
yarn android
```


### Running the `sdk-react-native` Package Locally and Testing

To run the `sdk-react-native` package locally and test it within the example dApp, follow these steps:

1. **Copy the Package Path:**
- Right-click on the `sdk-react-native` package folder.
- Click on "Copy Path" to copy the folder path.

2. **Update the Example dApp:**
- Open the `package.json` file of your example dApp.
- Replace the existing reference to `sdk-react-native` with the copied path.
- The entry in your `package.json` should look something like this:
```json
"@metamask/sdk-react-native": "/Users/{YOUR_MAC_USER_NAME}/Projects/metamask-sdk/packages/sdk-react-native"
```

3. **Build and Link the Package:**
- Open a terminal and navigate to the `sdk-react-native` package folder.
- Run the following command to build the package and link it to the example dApp:
```bash
yarn build && cd .. && cd examples/reactNativeSdkDemo && rm -rf .yarn && rm -rf node_modules && yarn && cd ios && pod install && cd ..
```

4. **Consume the Local Package:**
- Your example dApp is now consuming the `sdk-react-native` package from the local folder.
- After making any changes to the `sdk-react-native` package, repeat the above steps to see the updates reflected in the example dApp.

5. **Build and Test:**
- With the local package linked, you can now build the example dApp and start testing.

---
By following these steps, you ensure that the example dApp is using the latest local version of the `sdk-react-native` package, allowing for seamless development and testing.
35 changes: 30 additions & 5 deletions packages/sdk-react-native/src/NativePackageMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,36 @@ export const terminate = async (): Promise<void> => {
export function setupDeeplinkHandling() {
if (Platform.OS === 'ios') {
const handleOpenURL = (event: any) => {
const url = new URL(event.url);
// Only listen for metamask urls
if (url.host === 'mmsdk') {
// Handle the URL event here
MetaMaskReactNativeSdk.handleDeepLink(event.url);
const { url } = event;

try {
if (!url || typeof url !== 'string') {
return;
}

const urlParts = url.split('://');

if (urlParts.length < 2) {
return;
}

const hostParts = urlParts[1].split('?');
if (hostParts.length === 0 || !hostParts[0]) {
return;
}

const host = hostParts[0];

// Only listen for metamask urls
if (host === 'mmsdk') {
// Handle the URL event here
MetaMaskReactNativeSdk.handleDeepLink(url);
}
} catch (error) {
console.error(
`MetaMaskReactNativeSdk.handleOpenURL() => Error handling URL ${url} `,
error,
);
}
};

Expand Down

0 comments on commit 6b7f45e

Please sign in to comment.