nativescript-wechat-share
为 Android 和 iOS 提供微信分享功能的 NativeScript 模块
npm i --save nativescript-wechat-share

nativescript-wechat-share

目前仅支持 iOS。Android 不支持,因为微信 SDK 需要主项目的同一包下定义一个 activity,而 NativeScript 目前不支持此功能(v1.4)。

如何使用

添加插件

通过 tns plugin add nativescript-wechat-share

Xcode 中的配置

在 Xcode 中打开 iOS 项目并执行以下配置

  1. 通过 Target->构建阶段->链接二进制与库添加 WeChat SDK 依赖的库

    • CoreTelephony.framework
    • libc++.tbd
    • libsqlite3.0.tbd
    • libz.tbd
    • SystemConfiguration.framework
  2. 为微信添加 URL Scheme,以便您的应用可以与微信应用通信

    • 转到 Target->Info->URL 类型
    • 点击加号按钮,添加新的方案:identifier = weixin,URL Schemes = APP-ID
    • APP-ID 是您在微信开放平台仪表板注册应用后从微信获取的 APPID:https://open.weixin.qq.com/
  3. 将以下内容添加到您的 Info.plist 中,您可以在 Xcode 的项目导航器中找到该文件->项目名称->项目名称->支持文件->项目名称-Info.plist

LSApplicationQueriesSchemes weixin

NSAppTransportSecurity NSAllowsArbitraryLoads ``` - LSApplicationQueriesSchemes 设置,以便您的应用可以检查微信是否已安装。 - NSAppTransportSecurity 设置为 NSAllowsArbitraryLoads,这样 iOS 不会强制您使用 https。 - 如果您想使用 NSAppTransportSecurity 默认的高安全设置,您可以使用 NSExceptionDomains 代替 NSAllowsArbitraryLoads,并使用 pingma.qq.com 作为白名单域名。

分享前准备代码

  1. 自定义 UIApplicationDelegate 的 handlOpenUrl 和 openUrl 方法的逻辑,使其由 WXApi.handleOpenURLDelegate 处理。因此您将有一个类似 app.ios.ts 的 app

var application = require("application"); var wechatPlugin = require("nativescript-wechat-share"); application.mainModule = "main-page"; application.cssFile = "./app.css";

class MyDelegate extends UIResponder implements UIApplicationDelegate { public static ObjCProtocols = [UIApplicationDelegate];

applicationHandleOpenURL(application: UIApplication, url: NSURL): boolean { return WXApi.handleOpenURLDelegate(url, wechatPlugin.myWXApiDelegate); } applicationOpenURLSourceApplicationAnnotation(application: UIApplication, url: NSURL, sourceApplication: NSString, annotation: id): boolean { return WXApi.handleOpenURLDelegate(url, wechatPlugin.myWXApiDelegate); } }

application.ios.delegate = MyDelegate; application.start(); ```

  1. 注册回调函数以处理微信应用返回的响应代码,如下所示

var wechatPlugin = require("nativescript-wechat-share"); wechatPlugin.registerOnRespCallback(function(code){ if (code == wechatPlugin.RespCodeEnum.Success) { alert("url 分享成功"); } else { alert("分享 url 失败,错误码: " + code); } }); ```

  1. 在真正进行分享之前注册您的应用(只需一次)

var wechatPlugin = require("nativescript-wechat-share"); wechatPlugin.registerApp("您的-App-ID"); ```

进行分享

最后,在需要时可以进行分享

var wechatPlugin = require("nativescript-wechat-share");
// share an URL to the timeline
wechatPlugin.shareUrl("the title", "the description", thumbImage, "the url", wechatPlugin.ShareToEnum.Timeline);
// or send to a friend
// wechatPlugin.shareUrl("the title", "the description", thumbImage, "the url", wechatPlugin.ShareToEnum.Chat);
// or save to favorite
// wechatPlugin.shareUrl("the title", "the description", thumbImage, "the url", wechatPlugin.ShareToEnum.Favorite);
// or share text
// wechatPlugin.shareText("the text", wechatPlugin.ShareToEnum.Timeline);
// or share image
// wechatPlugin.shareImage(image, thumbImage, wechatPlugin.ShareToEnum.Timeline);