Swift - 发送消息(文本,图片,文件等)给微信好友或分享到朋友圈
通过调用微信提供的API接口,我们可以很方便的在应用中发送消息给微信好友,或者分享到朋友圈。在微信开发平台(https://open.weixin.qq.com)里,提供了详细的说明文档和样例。但由于提供的样例是使用Objective-C写的,所以这边我写了个Swift版的样例。








--- AppDelegate.swift ---
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
import UIKit @UIApplicationMain class AppDelegate : UIResponder , UIApplicationDelegate { var window: UIWindow ? func application(application: UIApplication , didFinishLaunchingWithOptions launchOptions: [ NSObject : AnyObject ]?) -> Bool { // 注册app(这里随便取个名字) WXApi .registerApp( "hangge_appid" ) return true } func applicationWillResignActive(application: UIApplication ) { } func applicationDidEnterBackground(application: UIApplication ) { } func applicationWillEnterForeground(application: UIApplication ) { } func applicationDidBecomeActive(application: UIApplication ) { } func applicationWillTerminate(application: UIApplication ) { } } |
--- ViewController.swift ---
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
import UIKit class ViewController : UIViewController { //发送给好友还是朋友圈(默认好友) var _scene = WXSceneSession .value override func viewDidLoad() { super .viewDidLoad() } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() } //切换发送给好友还是朋友圈 @IBAction func changeScene(sender: UISegmentedControl ) { if sender.selectedSegmentIndex == 0 { _scene = WXSceneSession .value } else { _scene = WXSceneTimeline .value } } //发送纯文本 @IBAction func sendTextContent(sender: AnyObject ) { var req = SendMessageToWXReq () req.bText = true req.text = "hangge.com 做最好的开发者知识平台。" req.scene = _scene WXApi .sendReq(req) } //发送图片 @IBAction func sendImageContent(sender: AnyObject ) { var message = WXMediaMessage () //发送的图片 var filePath = NSBundle .mainBundle().pathForResource( "image" , ofType: "jpg" ) var image = UIImage (contentsOfFile:filePath!) var imageObject = WXImageObject () imageObject.imageData = UIImagePNGRepresentation (image) message.mediaObject = imageObject //图片缩略图 var width = 240.0 as CGFloat var height = width*image!.size.height/image!.size.width UIGraphicsBeginImageContext ( CGSizeMake (width, height)) image!.drawInRect( CGRectMake (0, 0, width, height)) message.setThumbImage( UIGraphicsGetImageFromCurrentImageContext ()) UIGraphicsEndImageContext () var req = SendMessageToWXReq () req.bText = false req.message = message req.scene = _scene WXApi .sendReq(req) } //发送链接 @IBAction func sendLinkContent(sender: AnyObject ) { var message = WXMediaMessage () message.title = "欢迎访问 hangge.com" message.description = "做最好的开发者知识平台。分享各种编程开发经验。" message.setThumbImage( UIImage (named: "apple.png" )) var ext = WXWebpageObject () message.mediaObject = ext var req = SendMessageToWXReq () req.bText = false req.message = message req.scene = _scene WXApi .sendReq(req) } //发送音乐 @IBAction func sendMusicContent(sender: AnyObject ) { var message = WXMediaMessage () message.title = "一无所有" message.description = "崔健" message.setThumbImage( UIImage (named: "apple.png" )) var ext = WXMusicObject () ext.musicUrl = "http://y.qq.com/i/song.html#p=7B22736F6E675F4E616D65223A22E4B880E697A0E68980E69C89222C22736F6E675F5761704C69766555524C223A22687474703A2F2F74736D7573696334382E74632E71712E636F6D2F586B30305156342F4141414130414141414E5430577532394D7A59344D7A63774D4C6735586A4C517747335A50676F47443864704151526643473444442F4E653765776B617A733D2F31303130333334372E6D34613F7569643D3233343734363930373526616D703B63743D3026616D703B636869643D30222C22736F6E675F5769666955524C223A22687474703A2F2F73747265616D31342E71716D757369632E71712E636F6D2F33303130333334372E6D7033222C226E657454797065223A2277696669222C22736F6E675F416C62756D223A22E4B880E697A0E68980E69C89222C22736F6E675F4944223A3130333334372C22736F6E675F54797065223A312C22736F6E675F53696E676572223A22E5B494E581A5222C22736F6E675F576170446F776E4C6F616455524C223A22687474703A2F2F74736D757369633132382E74632E71712E636F6D2F586C464E4D313574414141416A41414141477A4C36445039536A457A525467304E7A38774E446E752B6473483833344843756B5041576B6D48316C4A434E626F4D34394E4E7A754450444A647A7A45304F513D3D2F33303130333334372E6D70333F7569643D3233343734363930373526616D703B63743D3026616D703B636869643D3026616D703B73747265616D5F706F733D35227D" message.mediaObject = ext var req = SendMessageToWXReq () req.bText = false req.message = message req.scene = _scene WXApi .sendReq(req) } //发送视频 @IBAction func sendVideoContent(sender: AnyObject ) { var message = WXMediaMessage () message.title = "乔布斯访谈" message.description = "饿着肚皮,傻逼着。" message.setThumbImage( UIImage (named: "apple.png" )) var ext = WXVideoObject () message.mediaObject = ext var req = SendMessageToWXReq () req.bText = false req.message = message req.scene = _scene WXApi .sendReq(req) } //发送非gif格式的表情 @IBAction func sendNonGifContent(sender: AnyObject ) { var message = WXMediaMessage () message.setThumbImage( UIImage (named: "res5thumb.png" )) var ext = WXEmoticonObject () var filePath = NSBundle .mainBundle().pathForResource( "res5" , ofType: "jpg" ) ext.emoticonData = NSData (contentsOfFile:filePath!) message.mediaObject = ext var req = SendMessageToWXReq () req.bText = false req.message = message req.scene = _scene WXApi .sendReq(req) } //发送gif格式的表情 @IBAction func sendGifContent(sender: AnyObject ) { var message = WXMediaMessage () message.setThumbImage( UIImage (named: "res6thumb.png" )) var ext = WXEmoticonObject () var filePath = NSBundle .mainBundle().pathForResource( "res6" , ofType: "gif" ) ext.emoticonData = NSData (contentsOfFile:filePath!) message.mediaObject = ext var req = SendMessageToWXReq () req.bText = false req.message = message req.scene = _scene WXApi .sendReq(req) } //发送文件 @IBAction func sendFileContent(sender: AnyObject ) { var message = WXMediaMessage () message.title = "ML.pdf" message.description = "Pro CoreData" message.setThumbImage( UIImage (named: "apple.png" )) var ext = WXFileObject () ext.fileExtension = "pdf" var filePath = NSBundle .mainBundle().pathForResource( "ML" , ofType: "pdf" ) ext.fileData = NSData (contentsOfFile:filePath!) message.mediaObject = ext var req = SendMessageToWXReq () req.bText = false req.message = message req.scene = _scene WXApi .sendReq(req) } } |
5,源码下载:WeiXinShare.zip
Swift - 发送消息(文本,图片,文件等)给微信好友或分享到朋友圈的更多相关文章
- UC和QQ两个主流浏览器 * 点击触发微信分享到朋友圈或发送给朋友的功能(转载)
转载(声明:仅供学习使用) /** * 此插件主要作用是在UC和QQ两个主流浏览器 * 上面触发微信分享到朋友圈或发送给朋友的功能 * 代码编写过程中 参考: * http://mjs.sinaimg ...
- Layman 分享到朋友圈或发送给朋友
*主要是介绍如何在网页中实现发送给朋友和分享到朋友圈时内容参数自定义的功能 微信JS接口 1.微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包; 通过使用微信JS-SDK, ...
- 如何让微信里的html应用弹出“点击右上角分享到朋友圈”的图片
一个分享按钮,一个隐藏的图片(这个图片绝对定位在右上角)然后就是点击显示,点击隐藏了... <a href="javascript:;" onclick="docu ...
- JAVA调用微信接口实现页面分享功能(分享到朋友圈显示图片,分享给朋友)
钉钉提供的内网穿透之HTTP穿透:https://www.cnblogs.com/pxblog/p/13862376.html 网页分享到微信中如何显示标题图,如果自定义标题图,描述,显示效果如下 官 ...
- Java企业微信开发_05_消息推送之发送消息(主动)
一.本节要点 1.发送消息与被动回复消息 (1)流程不同:发送消息是第三方服务器主动通知微信服务器向用户发消息.而被动回复消息是 用户发送消息之后,微信服务器将消息传递给 第三方服务器,第三方服务器接 ...
- Java企业微信开发_04_消息推送之发送消息(主动)
源码请见: Java企业微信开发_00_源码及资源汇总贴 一.本节要点 1.发送消息与被动回复消息 (1)流程不同:发送消息是第三方服务器主动通知微信服务器向用户发消息.而被动回复消息是 用户发送消息 ...
- 在zabbix中实现发送带有图片的邮件和微信告警
1 python实现在4.2版本zabbix发送带有图片的报警邮件 我们通常收到的报警,都是文字,是把动作中的消息内容当成了正文参数传给脚本,然后邮件或者微信进行接收,往往只能看到当前值,无法直观的获 ...
- PHP实现RTX发送消息提醒
RTX是腾讯公司推出的企业级即时通信平台,大多数公司都在使用它,但是我们很多时候需要将自己系统或者产品的一些通知实时推送给RTX,这就需要用到RTX的服务端SDK,建议先去看看RTX的SDK开发文档( ...
- .netcore--Controller后台实现企业微信发送消息
一.获得企业微信管理端权限,登录企业企业微信管理端界面,并创建应用,如下图中的[网站消息推送] 二.参见企业微信API文献,根据corpid=ID&corpsecret=SECRET(其中企业 ...
随机推荐
- DZNEmptyDataSet框架简介
给大家推荐一个设置页面加载失败时显示加载失败等的框架. 下载地址:DZNEmptyDataSet https://github.com/dzenbot/DZNEmptyDataSet 上效果首先在你的 ...
- tomcat环境变量的配置(网上摘,全部验证通过)
tomcat环境变量的配置 1.===> 进入bin目录下,双击startup.bat看是否报错.一般肯定会报. 2.===> 右键我的电脑===>高级===>环境变量 ...
- 【转】lnmp_auto:自动化安装lnmp环境脚本
原文链接: lnmp_auto:自动化安装lnmp环境脚本 这哥们整理的这篇博文很好 转载分享 博文转载如下: 源代码在github上:https://github.com/jianfengye/ ...
- USACO Ski Course Design 暴力
从Min到Max范围内暴力一下即可. /* ID: wushuai2 PROG: skidesign LANG: C++ */ //#pragma comment(linker, "/STA ...
- 什么时候需要交换Top Level ?
什么时候需要交换Top Level ? 上一篇中提到,如果采用仿真的时候,运用门级仿真就需要进行顶层交换,RTL仿真不需要,那么什么时候需要呢? QuartusII 向下包含,在Project Nav ...
- Android string.xml error: Apostrophe not preceded by \
Android string.xml error: Apostrophe not preceded by \ 遇到了这个错误,编译无法通过 error: Apostrophe not preceded ...
- 基于Sql Server 2008的分布式数据库的实践(四)
原文 基于Sql Server 2008的分布式数据库的实践(四) 数据库设计 1.E-R图 2.数据库创建 Win 7 1 create database V3 Win 2003 1 create ...
- Android学习笔记:如何高效显示图片,避免内存溢出 和 ImageView无法显示大尺寸的图片
因为手机的内存资源是有限的,每个app可使用的内存是受限的.而现在采用高分辨率拍的照片往往很大.如果加载时不注意方法,很有可能会引起java.lang.OutofMemoryError: bitmap ...
- python爬虫实战2百度贴吧爬html
转自:http://blog.csdn.net/wxg694175346/article/details/8927832 import string, urllib2 #定义百度函数 def baid ...
- 设计模式 ( 十三 ) 命令模式Command(对象行为型)
设计模式 ( 十三 ) 命令模式Command(对象行为型) 1.概述 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需 ...