- 版本:0.1.1
- GitHub: https://github.com/linzhiqi/nativescript-wechat-share
- NPM: https://npmjs.net.cn/package/nativescript-wechat-share
- 下载
- 昨天:0
- 上周:0
- 上个月:0
nativescript-wechat-share
目前仅支持 iOS。Android 不支持,因为微信 SDK 需要主项目的同一包下定义一个 activity,而 NativeScript 目前不支持此功能(v1.4)。
如何使用
添加插件
通过 tns plugin add nativescript-wechat-share
。
Xcode 中的配置
在 Xcode 中打开 iOS 项目并执行以下配置
-
通过 Target->构建阶段->链接二进制与库添加 WeChat SDK 依赖的库
- CoreTelephony.framework
- libc++.tbd
- libsqlite3.0.tbd
- libz.tbd
- SystemConfiguration.framework
-
为微信添加 URL Scheme,以便您的应用可以与微信应用通信
- 转到 Target->Info->URL 类型
- 点击加号按钮,添加新的方案:identifier = weixin,URL Schemes = APP-ID
- APP-ID 是您在微信开放平台仪表板注册应用后从微信获取的 APPID:https://open.weixin.qq.com/
-
将以下内容添加到您的 Info.plist 中,您可以在 Xcode 的项目导航器中找到该文件->项目名称->项目名称->支持文件->项目名称-Info.plist
分享前准备代码
- 自定义 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(); ```
- 注册回调函数以处理微信应用返回的响应代码,如下所示
var wechatPlugin = require("nativescript-wechat-share"); wechatPlugin.registerOnRespCallback(function(code){ if (code == wechatPlugin.RespCodeEnum.Success) { alert("url 分享成功"); } else { alert("分享 url 失败,错误码: " + code); } }); ```
- 在真正进行分享之前注册您的应用(只需一次)
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);