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

1,实现的功能
(1)可以发送各种类型的消息给好友,也可以分享到朋友圈
(2)发送的内容类型包括:纯文本,图片,链接,音乐,视频,gif表情,非gif表情,文件
2,效果图如下
  
  
3,注意事项:
(1)该样例必须连接手机进行真机调试
(2)还需要到微信开发平台注册应用id,下面代码里会用到(如果不注册,随便起个id来调试也没什么问题,就是收到消息下方会显示“未审核应用”)
4,详细步骤
(1)首先把微信SDK资源库拖入到项目中来(整个Resources文件夹)
(2)引入WXApi.h这个头文件
(3)编写代码

--- 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()
        ext.webpageUrl = "http://hangge.com"
        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"
        ext.musicDataUrl = "http://stream20.qqmusic.qq.com/32464723.mp3"
        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()
        ext.videoUrl = "http://v.youku.com/v_show/id_XNTUxNDY1NDY4.html"
        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 - 发送消息(文本,图片,文件等)给微信好友或分享到朋友圈的更多相关文章

  1. UC和QQ两个主流浏览器 * 点击触发微信分享到朋友圈或发送给朋友的功能(转载)

    转载(声明:仅供学习使用) /** * 此插件主要作用是在UC和QQ两个主流浏览器 * 上面触发微信分享到朋友圈或发送给朋友的功能 * 代码编写过程中 参考: * http://mjs.sinaimg ...

  2. Layman 分享到朋友圈或发送给朋友

    *主要是介绍如何在网页中实现发送给朋友和分享到朋友圈时内容参数自定义的功能 微信JS接口 1.微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包; 通过使用微信JS-SDK, ...

  3. 如何让微信里的html应用弹出“点击右上角分享到朋友圈”的图片

    一个分享按钮,一个隐藏的图片(这个图片绝对定位在右上角)然后就是点击显示,点击隐藏了... <a href="javascript:;" onclick="docu ...

  4. JAVA调用微信接口实现页面分享功能(分享到朋友圈显示图片,分享给朋友)

    钉钉提供的内网穿透之HTTP穿透:https://www.cnblogs.com/pxblog/p/13862376.html 网页分享到微信中如何显示标题图,如果自定义标题图,描述,显示效果如下 官 ...

  5. Java企业微信开发_05_消息推送之发送消息(主动)

    一.本节要点 1.发送消息与被动回复消息 (1)流程不同:发送消息是第三方服务器主动通知微信服务器向用户发消息.而被动回复消息是 用户发送消息之后,微信服务器将消息传递给 第三方服务器,第三方服务器接 ...

  6. Java企业微信开发_04_消息推送之发送消息(主动)

    源码请见: Java企业微信开发_00_源码及资源汇总贴 一.本节要点 1.发送消息与被动回复消息 (1)流程不同:发送消息是第三方服务器主动通知微信服务器向用户发消息.而被动回复消息是 用户发送消息 ...

  7. 在zabbix中实现发送带有图片的邮件和微信告警

    1 python实现在4.2版本zabbix发送带有图片的报警邮件 我们通常收到的报警,都是文字,是把动作中的消息内容当成了正文参数传给脚本,然后邮件或者微信进行接收,往往只能看到当前值,无法直观的获 ...

  8. PHP实现RTX发送消息提醒

    RTX是腾讯公司推出的企业级即时通信平台,大多数公司都在使用它,但是我们很多时候需要将自己系统或者产品的一些通知实时推送给RTX,这就需要用到RTX的服务端SDK,建议先去看看RTX的SDK开发文档( ...

  9. .netcore--Controller后台实现企业微信发送消息

    一.获得企业微信管理端权限,登录企业企业微信管理端界面,并创建应用,如下图中的[网站消息推送] 二.参见企业微信API文献,根据corpid=ID&corpsecret=SECRET(其中企业 ...

随机推荐

  1. A - Oil Deposits(搜索)

    搜索都不熟练,所以把以前写的一道搜索复习下,然后下一步整理搜索和图论和不互质的中国剩余定理的题 Description GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp ...

  2. MySQL 出现 The table is full 的解决方法

    原文链接: MySQL 出现 The table is full 的解决方法 浅谈MySql的存储引擎(表类型) MySQL 出现 The table is full 只有一个原因,对应的表数据容量达 ...

  3. UVALive 6467 Strahler Order 拓扑排序

    这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ ...

  4. 基于visual Studio2013解决算法导论之048红黑树

     题目 红黑树 解决代码及点评 // 红黑树.cpp : 定义控制台应用程序的入口点. // #include <stdio.h> #include <stdlib.h> ...

  5. 基于visual Studio2013解决C语言竞赛题之0422牛顿迭代法

      题目

  6. C++ pair 使用方法

    类模板:template <class T1, class T2> struct pair 參数:T1是第一个值的数据类型,T2是第二个值的数据类型. 功能:pair将一对值组合成一个值, ...

  7. Codeforces Round #198 (Div. 2) C. Tourist Problem

    C. Tourist Problem time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. Java设计模式菜鸟系列(九)外观模式建模与实现

    转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39805735 外观模式(Facade):是为了解决类与类之间的依赖关系的,像spring一 ...

  9. libvirt(virsh命令总结)

    virsh回车进入交互式界面: version pwd hostname 显示本节点主机名 nodeinfo  显示节点信息 list --all 显示所有云主机 7种状态: running  运行中 ...

  10. C# 点击窗口任意位置拖动

    代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...