react-native開源組件react-native-wechat學習

轉載連接:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/react-native-open-source-components-react-native-wechat-learning/html

因爲最近工做的須要用到微信分享,因而我就找開源組件庫(react-native-wechat),下面是具體的使用方法。node

1、進入到項目的目錄中執行以下命令:react

$ npm install react-native-wechat --save

2、IOS:打開你的Xcode工程ios

2.1 從node_modules/react-native-wechat/ios中連接RCTWeChat到庫文件中,不要忘記將它添加到"Buid Phases"中(以下圖所示。注:具體的能夠參考facebook官網的方法http://facebook.github.io/react-native/docs/linking-libraries-ios.html)c++

 

 

2.2 添加一下庫文件(Build Phase->Link Binary With Libraries):SystemConfiguration.framework,CoreTelephony.framework ,libsqlite3.0,libc++,libz  git

2.3添加app id (Targets->info->URL types)注:URL Schemes填寫的是APP ID 須要去微信開發者平臺註冊。github

2.4爲了適配IOS9須要在LSApplicationQueriesSchemes (Target-info-Custom IOS Target Properties)添加微信爲白名單。咱們須要修改info.plist文件,web

而後添加以下的代碼:sql

<key>LSApplicationQueriesSchemes</key>
 <array>
    <!-- 微信 URL Scheme 白名單-->
    <string>wechat</string>
    <string>weixin</string>
    <!-- 新浪微博 URL Scheme 白名單-->
    <string>sinaweibohd</string>
    <string>sinaweibo</string>
    <string>sinaweibosso</string>
    <string>weibosdk</string>
    <string>weibosdk2.5</string>
    <!-- QQ、Qzone URL Scheme 白名單-->
    <string>mqqapi</string>
    <string>mqq</string>
    <string>mqqOpensdkSSoLogin</string>
    <string>mqqconnect</string>
    <string>mqqopensdkdataline</string>
    <string>mqqopensdkgrouptribeshare</string>
    <string>mqqopensdkfriend</string>
    <string>mqqopensdkapi</string>
    <string>mqqopensdkapiV2</string>
    <string>mqqopensdkapiV3</string>
    <string>mqzoneopensdk</string>
    <string>wtloginmqq</string>
    <string>wtloginmqq2</string>
    <string>mqqwpa</string>
    <string>mqzone</string>
    <string>mqzonev2</string>
    <string>mqzoneshare</string>
    <string>wtloginqzone</string>
    <string>mqzonewx</string>
    <string>mqzoneopensdkapiV2</string>
    <string>mqzoneopensdkapi19</string>
    <string>mqzoneopensdkapi</string>
    <string>mqzoneopensdk</string>
    <!-- 支付寶  URL Scheme 白名單-->
    <string>alipay</string>
    <string>alipayshare</string>
</array>

2.5由於須要使用LinkingIOS,還須要在AppDelegate.m中添加以下代碼:npm

#import "../Libraries/LinkingIOS/RCTLinkingManager.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

  return [RCTLinkingManager application:application openURL:url sourceApplication:sourceApplication annotation:annotation];

}

3、API文檔

3.1 registerApp(appid)
	{string} appid  你的appid
	return {promise} 返回一個promise對象
3.2 registerAppWithDescription(appid,appdesc) ->僅支持IOS
	{string} appid  (同上)
	{String} appdesc 你的app的說明
	return {promise}
3.3 isWXAppInstalled() 檢查微信是否初始化
3.4 isWXAppSupportApi()檢查微信是否支持url
3.5 getApiVersion()    得到微信SDK的版本
3.6 openWXApp()        打開微信app
3.7 sendAuthRequest([scope[,state]]) 發送默認請求,scope:登陸時所申請的權限默認爲get_simple_userinfo.須要
多個權限的時候以逗號分隔開。

3.8  shareToTimeline(data) 分享信息到朋友圈

      {Object } data 包含如下發送信息

       {string} thumbImage  能夠是一個uri或者資源id

       {string} type  信息的類型,能夠是 {news|text|imageUrl|imageFile|imageResource|video|audio|file}

       {string} webpageUrl  若是類型是news,分享一個網頁連接

       {string}  imageUrl      若是類型是image,提供一個遠程的圖片連接

       {string}  videoUrl       若是類型是video,提供一個視頻

       {string} musicUrl        若是是audio,提供一個音樂

       {string} filePath          提供本地文件

如下例子須要'react-native-wechat'和'react-native-fs'組件

import * as WeChat from 'react-native-wechat';
import fs from 'react-native-fs';
var resolveAssetSource = require('resolveAssetSource'); // along with Image component
// Code example to share text message:
try {
    var result = await  WeChat.shareToTimeline({type: 'text', description: 'I\'m Wechat, :)'});
    console.log('share text message to time line successful', result);
}
catch (e) {
    console.log('share text message to time line failed', e);
}

// Code example to share image url:
// Share raw http(s) image from web will always fail with unknown reason, please use image file or image resource instead
try {
    var result = await WeChat.shareToTimeline({
        type: 'imageUrl',
        title: 'web image',
        description: 'share web image to time line',
        mediaTagName: 'email signature',
        messageAction: undefined,
        messageExt: undefined,
        imageUrl: 'http://www.ncloud.hk/email-signature-262x100.png'
    });
    console.log('share image url to time line successful', result);
}
catch (e) {
    console.log('share image url to time line failed', e);
}

// Code example to share image file:
try {
    var rootPath = fs.DocumentDirectoryPath;
    var savePath = rootPath + '/email-signature-262x100.png'; // like /var/mobile/Containers/Data/Application/B1308E13-35F1-41AB-A20D-3117BE8EE8FE/Documents/email-signature-262x100.png

    await fs.downloadFile('http://www.ncloud.hk/email-signature-262x100.png', savePath);

    var result = await WeChat.shareToTimeline({
        type: 'imageFile',
        title: 'image file download from network',
        description: 'share image file to time line',
        mediaTagName: 'email signature',
        messageAction: undefined,
        messageExt: undefined,
        imageUrl: savePath
    });

    console.log('share image file to time line successful', result);
}
catch (e) {
    console.log('share image file to time line failed', e);
}

// Code example to share image resource:
try {
    var imageResource = require('./email-signature-262x100.png');
    var result = await WeChat.shareToTimeline({
        type: 'imageResource',
        title: 'resource image',
        description: 'share resource image to time line',
        mediaTagName: 'email signature',
        messageAction: undefined,
        messageExt: undefined,
        imageUrl: resolveAssetSource(imageResource).uri
    });
    console.log('share resource image to time line successful', result);
}
catch (e) {
    console.log('share resource image to time line failed', e);
}



  3.9 shareToSession(data) 用法和shareToTimeline用法類似,發送信息給一個朋友或者羣組

  3.10 addListener(eventType,listener[,context]) 當事件觸發時,會調用一個監聽器,返回一個對象

  3.11  once(eventType,listener[,context]) 用法和addListener類似

  3.12  removeAllListener()   刪除全部的註冊監聽器  

相關文章
相關標籤/搜索