nativescript-wechat-share-plugin
gdtdpt | v0.0.1
在nativescript上实现的微信分享插件。
npm i --save nativescript-wechat-share-plugin

Nativescript-wechat-share-plugin

此插件在 NativeScript 平台上无法接收来自微信的任何消息,包括应用请求和分享结果。我在 NativeScript 平台上还没有找到添加自定义活动或在 iOS 上定义代理的方法。

安装

$ tns plugin add nativescript-wechat-share-plugin 

注意:在我测试的 iOS 平台上存在一个问题,即使静态库已经被删除,也无法将其链接两次。所以如果在运行时遇到像 'Can not find variable WXApiDelegate' 这样的错误,请按照以下步骤操作并再次测试。

$ tns platform remove ios
$ tns platform add ios
$ tns prepare ios

iOS 配置

将以下内容添加到 <YOUR_PROJECT_DIR>/app/App_Resources/iOS/Info.plist

<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>weixin</string>
<key>CFBundleURLSchemes</key>
<array>
<string><!--YOUR_WECHAT_APP_ID--></string>
</array>
</dict>
</array>

用法

此处 定义的 WechatSharingScene 对象。它有三个选项可以使用

WechatSharingScene.SESSION //share to someone or a group
WechatSharingScene.TIMELINE //share to Moments
WechatSharingScene.FAVORITE //add to self favorite
分享文本
import {WechatSharePlugin, WechatSharingScene} from 'nativescript-wechat-share-plugin';

let wechatSharePlugin = new WechatSharePlugin(/*YOUR_WECHAT_APP_ID*/);

let sharingOptions = {
scene: WechatSharingScene.TIMELINE,
text: "this is a test text."
};

wechatSharePlugin.share(sharingOptions);
分享 URL
import {WechatSharePlugin, WechatSharingScene, WechatSharingType} from 'nativescript-wechat-share-plugin';

let wechatSharePlugin = new WechatSharePlugin(/*YOUR_WECHAT_APP_ID*/);

let sharingOptions = {
scene: WechatSharingScene.TIMELINE,
messages: {
media: {
type: WechatSharingType.TYPE_SHARING_WEBPAGE,
webpageUrl: "https://open.weixin.qq.com/"
}
}
};
wechatSharePlugin.share(sharingOptions);
分享本地图片
import * as fs from 'file-system';
const appPath = fs.knownFolders.currentApp();
const applePath = fs.path.join(appPath.path, "img", "apple.jpg");

import {WechatSharePlugin, WechatSharingScene, WechatSharingType} from 'nativescript-wechat-share-plugin';

let wechatSharePlugin = new WechatSharePlugin(/*YOUR_WECHAT_APP_ID*/);

let sharingOptions = {
scene: WechatSharingScene.TIMELINE,
messages: {
title: "test title",
description: "test description",
media: {
type: WechatSharingType.TYPE_SHARING_IMAGE,
image: applePath
}
}
}
wechatSharePlugin.share(sharingOptions);
分享在线图片
import {WechatSharePlugin, WechatSharingScene, WechatSharingType} from 'nativescript-wechat-share-plugin';

let wechatSharePlugin = new WechatSharePlugin(/*YOUR_WECHAT_APP_ID*/);

let sharingOptions = {
scene: WechatSharingScene.TIMELINE,
messages: {
title: "shareOnlineImageToWechat title",
description: "shareOnlineImageToWechat description",
media: {
type: WechatSharingType.TYPE_SHARING_IMAGE,
image: "http://mmbiz.qpic.cn/mmbiz/PiajxSqBRaEIVJ6bW5EhIpIVZuxavukF9zUCzuoAKicofAtxibTBZOzsgP73GtO7jkkH2MQke21fOFC6Pnm0JvC6Q/0?wx_fmt=png/"
}
}
}
wechatSharePlugin.share(sharingOptions);
分享音乐
import {WechatSharePlugin, WechatSharingScene, WechatSharingType} from 'nativescript-wechat-share-plugin';

let wechatSharePlugin = new WechatSharePlugin(/*YOUR_WECHAT_APP_ID*/);

let sharingOptions = {
scene: WechatSharingScene.TIMELINE,
messages: {
title: "shareMusicToWechat title",
description: "shareMusicToWechat description",
media: {
type: WechatSharingType.TYPE_SHARING_MUSIC,
image: "http://staff2.ustc.edu.cn/~wdw/softdown/index.asp/0042515_05.ANDY.mp3"
}
}
}
wechatSharePlugin.share(sharingOptions);
分享视频
import {WechatSharePlugin, WechatSharingScene, WechatSharingType} from 'nativescript-wechat-share-plugin';

let wechatSharePlugin = new WechatSharePlugin(/*YOUR_WECHAT_APP_ID*/);

let sharingOptions = {
scene: WechatSharingScene.TIMELINE,
messages: {
title: "shareVideoToWechat title",
description: "shareVideoToWechat description",
media: {
type: WechatSharingType.TYPE_SHARING_VIDEO,
image: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
}
}
}
wechatSharePlugin.share(sharingOptions);