Skip to content

兼容iOS

佘小恒 edited this page Oct 17, 2016 · 5 revisions

因为与iOS端提供的API不太一致,在此提供一套解决方案,作为参考:

wechat.js

import { Platform,NativeAppEventEmitter } from 'react-native';
import WeChatIOS from 'react-native-wechat-ios';
import WeChatAndroid from 'react-native-wechat-android';

let shareOptions;

let android = {
    registerApp(appId,callback){
        WeChatAndroid.registerApp(appId,callback);
    },
    openWXApp(callback){
        WeChatAndroid.openWXApp(callback);
    },
    share(options,callback){
        shareOptions = {
            title: options.title,
            desc: options.desc,
            thumbSize: 150,
            scene: options.scene,
            type: 3,
            webpageUrl: options.link,
            thumbImage: options.thumbImage,
        };
        WeChatAndroid.sendReq(shareOptions,callback);
    },
    addAuthListener(callback) {
        NativeAppEventEmitter.addListener('finishedAuth', callback);
    },
    removeAuthListeners(){
        NativeAppEventEmitter.removeAllListeners('finishedAuth');
    },
    addShareListener(callback) {
        NativeAppEventEmitter.addListener('finishedShare', callback);
    },
    removeShareListeners(){
        NativeAppEventEmitter.removeAllListeners('finishedShare');
    },
    addPayListener(callback) {
        NativeAppEventEmitter.addListener('finishedPay', callback);
    },
    removePayListeners(){
        NativeAppEventEmitter.removeAllListeners('finishedPay');
    }
}

let iOS = {
    registerApp(appId,callback){
        WeChatIOS.registerApp(appId,callback);
    },
    openWXApp(callback){
        WeChatIOS.openWXApp(callback);
    },
    share(options,callback){
        WeChatIOS.sendLinkURL(options,callback);
    },
    addAuthListener(callback) {
        NativeAppEventEmitter.addListener('didRecvAuthResponse', callback);
    },
    removeAuthListeners(){
        NativeAppEventEmitter.removeAllListeners('didRecvAuthResponse');
    },
    addShareListener(callback) {
        NativeAppEventEmitter.addListener('didRecvMessageResponse', callback);
    },
    removeShareListeners(){
        NativeAppEventEmitter.removeAllListeners('didRecvMessageResponse');
    },
    addPayListener(callback) {
        NativeAppEventEmitter.addListener('finishedPay', callback);
    },
    removePayListeners(){
        NativeAppEventEmitter.removeAllListeners('finishedPay');
    }
}

let wechat;

if (Platform.OS === 'ios') { 
    wechat = iOS;
} else {
    wechat = android;
}

export default wechat;

使用

import React, {Component} from 'react';

import {
    AppRegistry, 
    StyleSheet,
} from 'react-native';

import WeChat from './wechat';

WeChat.registerApp('',(err,res) => {
})

...
Clone this wiki locally