Weixin Sharing and Favorites allow third-party apps to enable users to share any text, image, music, video, webpage, and Mini Program in Weixin friend chats and on Moments or add them to Favorites from apps.
The Weixin Sharing and Favorites features are open to all developers. Developers can obtain the Weixin Sharing and Favorites permissions after applying for a mobile app and getting the approval under the Weixin Open Platform account.
Weixin Sharing and Favorites support six types of messages including text, image, music, video, webpage, and Mini Program (overseas apps support sharing webpages and Mini Programs). After integrating the Weixin SDK into an app, developers may call APIs to implement this feature. The following are examples of sharing text, image, music, video, webpage, and Mini Program.
**WXMediaMessage Description**
Field | Type | Description | Note |
---|---|---|---|
title | NSString | Message title | The maximum length is 512 bytes. |
description | NSString | Description | The maximum length is 1 KB. |
thumbData | NSData | Binary data of thumbnail | The maximum size is 32 KB. |
mediaObject | NSObject | Multimedia data object | Objects include WXImageObject, WXMusicObject, WXVideoObject, and WXWebpageObject. |
SendMessageToWXReq (request class of SendMessageToWX)
The target scene shared or added to favorites is implemented by modifying the scene field of SendMessageToWXReq.
Field | Type | Description | Note |
---|---|---|---|
text | NSString | Text content of a sent message | |
bText | BOOL | Whether text message is sent | Types of sent messages, including text messages and multimedia messages. Only one type can be selected. Text and multimedia messages cannot be sent at the same time. |
message | WXMediaMessage | Multimedia content of a sent message | |
scene | int | Target scene to be sent | Share to Chat: WXSceneSession Share on Moments: WXSceneTimeline Shared to Favorites: WXSceneFavorite |
1. Example of Text Sharing
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
req.bText = YES;
req.text = @"Shared content";
req.scene = WXSceneSession;
[WXApi sendReq:req];
2. Example of Image Sharing
WXImageObject in a multimedia message
Field | Type | Description | Note |
---|---|---|---|
imageData | NSData | Binary data of image | The maximum size is 10MB. |
UIImage *image = [UIImage imageNamed:@"res2.png"];
imageData = UIImageJPEGRepresentation(image, 0.7);
WXImageObject *imageObject = [WXImageObject object];
imageObject.imageData = imageData;
WXMediaMessage *message = [WXMediaMessage message];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"res5"
ofType:@"jpg"];
message.thumbData = [NSData dataWithContentsOfFile:filePath];
message.mediaObject = imageObject;
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = WXSceneTimeline;
[WXApi sendReq:req];
3. Example of Music Sharing
WXMusicObject in a multimedia message
| Field | Type | Description | Note | | --- | --- | --- | | musicUrl | NSString | URL address of audio webpage | The maximum length is 10 KB. | | musicLowBandUrl | NSString | URL address of audio webpage in the low bandwidth environment | The maximum length is 10 KB. | | musicDataUrl | NSString | URL address of audio data | The maximum length is 10 KB. | | musicLowBandDataUrl | NSString | URL address of audio data in the low bandwidth environment | The maximum length is 10 KB. |
``` WXMusicObject *musicObject = [WXMusicObject object]; musicObject.musicUrl = @"Music url"; musicObject.musicLowBandUrl = musicObject.musicUrl; musicObject.musicDataUrl = @"Music data url"; musicObject.musicLowBandDataUrl = musicObject.musicDataUrl;
WXMediaMessage *message = [WXMediaMessage message]; message.title = @"Music title"; message.description = @"Music description"; [message setThumbImage:[UIImage imageNamed:@"Thumbnail.jpg"]]; message.mediaObject = musicObject; SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init]; req.bText = NO; req.message = message; req.scene = WXSceneSession; [WXApi sendReq:req];
Note: For music shared to Weixin, directly tap the shared content in a friend chat or Moments to redirect to a third-party app. Click the music sharing content at the top of the chat list to redirect to Weixin's native music player to play the music.
<br>
**4. Example of Video Sharing**
WXVideoObject in a multimedia message
| Field | Type | Description | Note |
| :---------------- | :------- | :-------------- | :---------- |
| videoUrl | NSString | Video URL | The maximum length is 10 KB. |
| videoLowBandUrl | NSString | Video URL of audio data in the low bandwidth environment | The maximum length is 10 KB. |
**Note: Either videoUrl or videoLowBandUrl should be specified.**
<br>
WXVideoObject *videoObject = [WXVideoObject object]; videoObject.videoUrl = @"Video url"; videoObject.videoLowBandUrl = @"Low-resolution video url"; WXMediaMessage *message = [WXMediaMessage message]; message.title = @"Title"; message.description = @"Description"; [message setThumbImage:[UIImage imageNamed:@"Thumbnail.jpg"]]; message.mediaObject = videoObject; SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init]; req.bText = NO; req.message = message; req.scene = WXSceneSession; [WXApi sendReq:req];
<br>
**5. Example of Webpage Sharing**
WXWebpageObject in a multimedia message
| Field | Type | Description | Note |
| :----------- | :------- | :----- | :---------- |
| webpageUrl | NSString | HTML URL | The maximum length is 10 KB. |
<br>
WXWebpageObject *webpageObject = [WXWebpageObject object]; webpageObject.webpageUrl = @"https://open.weixin.qq.com"; WXMediaMessage *message = [WXMediaMessage message]; message.title = @"Title"; message.description = @"Description"; [message setThumbImage:[UIImage imageNamed:@"Thumbnail.jpg"]]; message.mediaObject = webpageObject; SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init]; req.bText = NO; req.message = message; req.scene = WXSceneSession; [WXApi sendReq:req];
<br>
**6. Example of Mini Program Sharing**
WXMiniProgramObject in a multimedia message
| Field | Type | Description | Note |
| :---------------- | :---------------- | :------------------ | :--------------------------------------- |
| webpageUrl | NSString | Webpage URL compatible with older versions of Weixin | The maximum length is 10 KB. |
| userName | NSString | Mini Program's userName | This value can be obtained by logging in to the Mini Program admin console and selecting **Settings** > **Basic Settings** > **Account Information**. |
| path | NSString | Mini Program's page path | |
| hdImageData | NSData | Binary data of the preview of the new version of the Mini Program, supported as of Weixin 6.5.9 | The maximum size is 128 KB. The recommended length-width ratio of custom images is 5:4. |
| withShareTicket | BOOL | Indicates whether to share with shareTicket. | Generally, developers want users to get more information, such as group identifier, when they open a shared Mini Program. By setting withSharseTicket to true, when the sharing card is opened by other users in a group chat, a shareTicket can be obtained to get more shared information. For details, see <a href="https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share.html" target="_blank">How to Get More Information from a Shared Mini Program</a>. This field is supported as of Weixin 6.5.13. |
| miniprogramType | WXMiniProgramType | Mini Program type, which is release version by default. Sharing developer version and test version of the Mini Program is allowed as of Developer DevTools 1.8.1 | Release version: WXMiniProgramTypeRelease<br>Beta version: WXMiniProgramTypeTest<br>Test version: WXMiniProgramTypePreview |
<br>
WXMiniProgramObject *object = [WXMiniProgramObject object]; object.webpageUrl = webpageUrl; object.userName = userName; object.path = path; object.hdImageData = hdImageData; object.withShareTicket = withShareTicket; object.miniProgramType = programType; WXMediaMessage *message = [WXMediaMessage message]; message.title = @"Mini Program title"; message.description = @"Mini Program description"; message.thumbData = nil; // Compatible with images (less than 32 KB) of the old version of the node. The new version is preferred. // Use the hdImageData property of WXMiniProgramObject. message.mediaObject = object; SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init]; req.bText = NO; req.message = message; req.scene = WXSceneSession; // Only chats are supported. [WXApi sendReq:req];
Notes:
1. The app initiating a share operation and the Mini Program shall belong to the same Weixin Open Platform account.
2. Sharing Mini Program messages in a chat is supported. Sharing on Moments is not allowed.
3. If messages are received in the Weixin app below 6.5.6 or on iPad, Mini Program sharing will be automatically converted to webpage sharing. Developers must fill in the webpage URL field to ensure the URL can opened in an older version of Weixin.