转载链接: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/

由于最近工作的需要用到微信分享,于是我就找开源组件库(react-native-wechat),下面是具体的使用方法。

一、进入到项目的目录中执行如下命令:

  1. $ npm install react-native-wechat --save

二、IOS:打开你的Xcode工程

2.1 从node_modules/react-native-wechat/ios中链接RCTWeChat到库文件中,不要忘记将它添加到"Buid Phases"中(如下图所示。注:具体的可以参考facebook官网的方法http://facebook.github.io/react-native/docs/linking-libraries-ios.html)

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

2.3添加app id (Targets->info->URL types)注:URL Schemes填写的是APP ID 需要去微信开发者平台注册。

2.4为了适配IOS9需要在LSApplicationQueriesSchemes (Target-info-Custom IOS Target Properties)添加微信为白名单。我们需要修改info.plist文件,

然后添加如下的代码:

  1. <key>LSApplicationQueriesSchemes</key>
  2. <array>
  3. <!-- 微信 URL Scheme 白名单-->
  4. <string>wechat</string>
  5. <string>weixin</string>
  6. <!-- 新浪微博 URL Scheme 白名单-->
  7. <string>sinaweibohd</string>
  8. <string>sinaweibo</string>
  9. <string>sinaweibosso</string>
  10. <string>weibosdk</string>
  11. <string>weibosdk2.5</string>
  12. <!-- QQ、Qzone URL Scheme 白名单-->
  13. <string>mqqapi</string>
  14. <string>mqq</string>
  15. <string>mqqOpensdkSSoLogin</string>
  16. <string>mqqconnect</string>
  17. <string>mqqopensdkdataline</string>
  18. <string>mqqopensdkgrouptribeshare</string>
  19. <string>mqqopensdkfriend</string>
  20. <string>mqqopensdkapi</string>
  21. <string>mqqopensdkapiV2</string>
  22. <string>mqqopensdkapiV3</string>
  23. <string>mqzoneopensdk</string>
  24. <string>wtloginmqq</string>
  25. <string>wtloginmqq2</string>
  26. <string>mqqwpa</string>
  27. <string>mqzone</string>
  28. <string>mqzonev2</string>
  29. <string>mqzoneshare</string>
  30. <string>wtloginqzone</string>
  31. <string>mqzonewx</string>
  32. <string>mqzoneopensdkapiV2</string>
  33. <string>mqzoneopensdkapi19</string>
  34. <string>mqzoneopensdkapi</string>
  35. <string>mqzoneopensdk</string>
  36. <!-- 支付宝 URL Scheme 白名单-->
  37. <string>alipay</string>
  38. <string>alipayshare</string>
  39. </array>

2.5因为需要使用LinkingIOS,还需要在AppDelegate.m中添加如下代码:

  1. #import "../Libraries/LinkingIOS/RCTLinkingManager.h"
  2.  
  3. @implementation AppDelegate
  4.  
  5. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
  6.  
  7. return [RCTLinkingManager application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
  8.  
  9. }

三、API文档

  1. 3.1 registerApp(appid)
  1. {string} appid 你的appid
  1. return {promise} 返回一个promise对象
  1. 3.2 registerAppWithDescription(appid,appdesc) ->仅支持IOS
  1. {string} appid (同上)
  1. {String} appdesc 你的app的说明
  1. return {promise}
  1. 3.3 isWXAppInstalled() 检查微信是否初始化
  1. 3.4 isWXAppSupportApi()检查微信是否支持url
  1. 3.5 getApiVersion() 获得微信SDK的版本
  1. 3.6 openWXApp() 打开微信app
  1. 3.7 sendAuthRequest([scope[,state]]) 发送默认请求,scope:登录时所申请的权限默认为get_simple_userinfo.需要
  1. 多个权限的时候以逗号分隔开。

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'组件

  1. import * as WeChat from 'react-native-wechat';
  2. import fs from 'react-native-fs';
  3. var resolveAssetSource = require('resolveAssetSource'); // along with Image component
  4. // Code example to share text message:
  5. try {
  6. var result = await WeChat.shareToTimeline({type: 'text', description: 'I\'m Wechat, :)'});
  7. console.log('share text message to time line successful', result);
  8. }
  9. catch (e) {
  10. console.log('share text message to time line failed', e);
  11. }
  12.  
  13. // Code example to share image url:
  14. // Share raw http(s) image from web will always fail with unknown reason, please use image file or image resource instead
  15. try {
  16. var result = await WeChat.shareToTimeline({
  17. type: 'imageUrl',
  18. title: 'web image',
  19. description: 'share web image to time line',
  20. mediaTagName: 'email signature',
  21. messageAction: undefined,
  22. messageExt: undefined,
  23. imageUrl: 'http://www.ncloud.hk/email-signature-262x100.png'
  24. });
  25. console.log('share image url to time line successful', result);
  26. }
  27. catch (e) {
  28. console.log('share image url to time line failed', e);
  29. }
  30.  
  31. // Code example to share image file:
  32. try {
  33. var rootPath = fs.DocumentDirectoryPath;
  34. var savePath = rootPath + '/email-signature-262x100.png'; // like /var/mobile/Containers/Data/Application/B1308E13-35F1-41AB-A20D-3117BE8EE8FE/Documents/email-signature-262x100.png
  35.  
  36. await fs.downloadFile('http://www.ncloud.hk/email-signature-262x100.png', savePath);
  37.  
  38. var result = await WeChat.shareToTimeline({
  39. type: 'imageFile',
  40. title: 'image file download from network',
  41. description: 'share image file to time line',
  42. mediaTagName: 'email signature',
  43. messageAction: undefined,
  44. messageExt: undefined,
  45. imageUrl: savePath
  46. });
  47.  
  48. console.log('share image file to time line successful', result);
  49. }
  50. catch (e) {
  51. console.log('share image file to time line failed', e);
  52. }
  53.  
  54. // Code example to share image resource:
  55. try {
  56. var imageResource = require('./email-signature-262x100.png');
  57. var result = await WeChat.shareToTimeline({
  58. type: 'imageResource',
  59. title: 'resource image',
  60. description: 'share resource image to time line',
  61. mediaTagName: 'email signature',
  62. messageAction: undefined,
  63. messageExt: undefined,
  64. imageUrl: resolveAssetSource(imageResource).uri
  65. });
  66. console.log('share resource image to time line successful', result);
  67. }
  68. catch (e) {
  69. console.log('share resource image to time line failed', e);
  70. }

3.9 shareToSession(data) 用法和shareToTimeline用法相似,发送信息给一个朋友或者群组

3.10 addListener(eventType,listener[,context]) 当事件触发时,会调用一个监听器,返回一个对象

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

3.12  removeAllListener()   删除所有的注册监听器

react-native开源组件react-native-wechat学习的更多相关文章

  1. 利用 React 高阶组件实现一个面包屑导航

    什么是 React 高阶组件 React 高阶组件就是以高阶函数的方式包裹需要修饰的 React 组件,并返回处理完成后的 React 组件.React 高阶组件在 React 生态中使用的非常频繁, ...

  2. 微信开源组件WCDB漫谈及Demo

    代码地址如下:http://www.demodashi.com/demo/12422.html 前言 移动端的数据库选型一直是一个难题,直到前段时间看到了WeMobileDev(微信前端团队)放出了第 ...

  3. React Native指南汇集了各类react-native学习资源、开源App和组件

    来自:https://github.com/ele828/react-native-guide React Native指南汇集了各类react-native学习资源.开源App和组件 React-N ...

  4. beeshell —— 开源的 React Native 组件库

    介绍 beeshell 是一个 React Native 应用的基础组件库,基于 0.53.3 版本,提供一整套开箱即用的高质量组件,包含 JavaScript(以下简称 JS)组件和复合组件(包含 ...

  5. react native 的图表开源组件react-native-chart-android

    react-native-chart-android是一个图表开源组件,使用方法可以去这里 由于需要在数据上加上触摸事件,而github上没有说明看源码找了半天才找到下面的解决方法,特此记录一下: 在 ...

  6. React Native 之 组件化开发

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  7. 【React Native开发】React Native配置执行官方样例-刚開始学习的人的福音(8)

    ),React Native技术交流4群(458982758),请不要反复加群! 欢迎各位大牛,React Native技术爱好者加入交流!同一时候博客左側欢迎微信扫描关注订阅号,移动技术干货,精彩文 ...

  8. React Native 开源项目汇总

    最近闲来无事,学习了React Native开发Android APP,自我感觉RN APP的效果和Native APP比还是蛮不错,以下是找到的一些优秀源码,仅供学习参考... React Nati ...

  9. React Native的组件ListView

    React Native的组件ListView类似于iOS中的UITableView和UICollectionView,也就是说React Native的组件ListView既可以实现UITableV ...

  10. React Native交互组件之Touchable

    React Native交互组件之Touchable:只要在组件外面包一个Touchable组件就可以实现点击交互. TouchableHighlight:高亮触摸 当点击时,组件的透明度会改变,可以 ...

随机推荐

  1. centos6.5环境通达OA数据库mysql5.0.67升级至mysql5.5.48方案

    centos6.5环境通达OA数据库mysql5.0.67升级至mysql5.5.42方案 整体方案: 环境准备,在备用服务器安装mysql5.5数据库 1.停用生产环境的应用访问 直接修改web的访 ...

  2. lvs持久连接及防火墙标记实现多端口绑定服务

    lvs持久连接及防火墙标记实现多端口绑定服务 LVS持久连接: PCC:将来自于同一个客户端发往VIP的所有请求统统定向至同一个RS: PPC:将来自于一个客户端发往某VIP的某端口的所有请求统统定向 ...

  3. Expm 4_2 有向无环图中的最短路径问题

    [问题描述] 建立一个从源点S到终点E的有向无环图,设计一个动态规划算法求出从S到E的最短路径值,并输出相应的最短路径. 解: package org.xiu68.exp.exp4; import j ...

  4. 如何利用github打造个人博客专属域名(文字版本)

    1. 前言 此篇文章仅限于记录,不适合作为教程使用. 2. 步骤 2.1 先决条件 有github账号,有个人域名(可在万网购买),电脑本地安装有git环境 2.2 在github新建仓库.例如我的g ...

  5. 破解idea

    2019最新注册码 地址:  http://idea.lanyus.com/ https://blog.csdn.net/best_luxi/article/details/81479820

  6. poj2018 二分+线性dp好题

    /* 遇到求最值,且答案显然具有单调性,即可用二分答案进行判定 那么本题要求最大的平均数,就可以转换成是否存在一个平均数为mid的段 */ #include<iostream> #incl ...

  7. String中的toCharArray:将此字符串转换为新的字符数组,并统计次数

    package stringyiwen; public class StringTestToCharArray { public static void main(String[] args) { S ...

  8. JQuery编写自己的插件(七)

    一:jQuery插件的编写基础1.插件的种类编写插件的目的是给一系列已经方法或函数做一个封装,以便在其他地方重复使用,方便后期维护和提高开发效率.常见的种类有以下三种:封装对象方法的插件

  9. 【C++ Primer | 19】运行类型识别

    运行类型识别 一.使用RTTI dynamic_cast运算符的调用形式如下所示: dynamic_cast<type*>(e) //e是指针 dynamic_cast<type&a ...

  10. ORA-12638: 身份证明检索失败 的解决办法

    今天在使用应用程序连接Oracle时碰到了 “ORA-12638: 身份证明检索失败” 错误, 解决方法:这是因为Oracle-client端的高级安全性验证导致,解决办法如下: 开始 -> 程 ...