https://github.com/evansuner/expo-wechat-lib
https://github.com/evansuner/expo-wechat-lib
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/evansuner/expo-wechat-lib
- Owner: evansuner
- Created: 2025-08-02T08:06:11.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-08-02T09:28:50.000Z (11 months ago)
- Last Synced: 2025-08-02T10:50:11.968Z (11 months ago)
- Language: Kotlin
- Size: 139 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Expo Native Wechat
A Expo Module for supporting Wechat APIs on Android and iOS
#### For Bare React Native Project
Please refer to [Native Wechat](https://github.com/Hector-Chong/native-wechat)
# Motivation
Most developers using React Native to build apps are not equipped with native developing skills, not to mention connecting their apps with WeChat.
Especially, WeChat OpenSDK is something pain in the ass because of [shitty docs](https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Access_Guide/iOS.html) where lots of APIs are not clear and intuitive. To help developers using React Native, I tried as much as possible to create this library. And I hope you benefit from this and be less frustrated with WeChat.
# Why
- Native WeChat is new, and many libraries like it are no longer maintained and keep up with the latest WeChat.
- Native WeChat supports both legacy and new React Native architecture.
- Native WeChat is a TurboModule that supports lazy loading, allowing for faster app startup.
- Supports TypeScript for better intellisense and easy-to-check typing errors.
- All Apis are promisified.
# Getting Started
## Installation
### Expo SDK 51 and below
```bash
npm install expo-native-wechat@0.1.x
# or
yarn add expo-native-wechat@0.1.x
```
### Expo SDK 52 and above
```bash
npm install expo-native-wechat@0.2.x
# or
yarn add expo-native-wechat@0.2.x
```
## Configuration
Open `app.json` and add the following information:
```js
{
// Add your WeChat app ID
"scheme": ["wx123456nxabcdefg"],
"ios": {
// Add your iOS universal link
// Example "applinks:(google.com)"
"associatedDomains": ["applinks:(Universal Link Here)"]
},
// Add Expo Native Wechat plugin
"plugins": ["expo-native-wechat"]
}
```
## Register SDK
Before invoking any APIs, registering Native WeChat by invoking `registerApp` is essential.
```typescript
import { registerApp } from "expo-native-wechat";
useEffect(() => {
return registerApp("wx964290141ebe9b7b");
}, []);
```
When invoking `registerApp`, there will be a listener to receive events from Wechat. `registerApp` returns a function to remove the listener, and no events will be received from Wechat.
## Invoking API
Now is the time to invoke API. For example, we will send an auth request to Wechat and receive the code. You may use the API named `sendAuthRequest` here.
```jsx
import { registerApp, sendAuthRequest } from "expo-native-wechat";
import { Button, Text } from "react-native";
import { verifyWechatCode } from "@/api/auth/wechat";
useEffect(() => {
registerApp("wx964290141ebe9b7b");
}, []);
const onButtonClicked = async () => {
const {
data: { code },
} = await sendAuthRequest();
await verifyWechatCode(code);
};
return (
Send Auth Request
);
```
Most APIs in Native WeChat are promisified. So is `sendAuthRequest` which will return a promise. When the user confirms the request on Wechat, the promise is resolved to be data with a code. All promisifed APIs return a generic type named `NativeWechatResponse`.
```typescript
export type NativeWechatResponse> = {
type: string;
errorCode: number;
errorStr: string | null;
data: T;
};
```
## WeChat App Pure Contract Signing
### appPureSignContract
```typescript
import { appPureSignContract } from 'expo-native-wechat';
const signContract = async () => {
try {
const result = await appPureSignContract({
preEntrustwebId: '5778aadY9nltAsZzXixCkFIGYnV2V'
});
console.log('Contract signing result:', result);
} catch (error) {
console.error('Contract signing failed:', error);
}
};
```
# Support
If you have trouble using this library, do not hesitate to open an issue. I am always here to help.