通过调用微信提供的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. c++,给常成员变量赋值

    C++中,常成员变量只能在构造函数赋值,且只能通过参数列表的形式赋值,且必须在构造函数赋值. (拥有常成员变量的类的构造函数必须对所有成员变量赋值.) #include <iostream> ...

  2. iOS Development: Proper Use of initWithNibName:bundle: Affects UITableViewController

    Address:http://www.outofcore.com/2011/07/ios-development-proper-use-of-initwithnibnamebundle-affects ...

  3. python3.4.3将汉字转换为大写拼音首字母

    from pypinyin import pinyin a=pinyin(u'杨强',type=FIRST_LETTER)    --->此时返回一个列表并赋给a(元素也是列表) b=[]  - ...

  4. 1, sync_with_stdio(), tie()的应用

    一.sync_with_stdio() 这个函数是一个“是否兼容stdio”的开关,C++为了兼容C,保证程序在使用了std::printf和std::cout的时候不发生混乱,将输出流绑在了一起. ...

  5. linux C函数之access函数的用法

    1.函数功能: 检查调用进程是否可以对指定的文件执行某种操作. 2.函数原型: 1)函数头文件 #include <stdio.h> #include <unistd.h> 2 ...

  6. android的fragments管理

    FragmentManager 为了管理Activity中的fragments,需要使用FragmentManager. 为了得到它,需要调用Activity中的getFragmentManager( ...

  7. BZOJ 1110: [POI2007]砝码Odw( 贪心 )

    ORZjcvb... #include<bits/stdc++.h> using namespace std; ; int N, M, item[maxn], V[maxn]; vecto ...

  8. 一天一个类,一点也不累 之 LinkedList

    我们的口号是,一天一个类,一点也不累 .. 今天要讲的是---LinkedList 首先,还是看看他的组织结构 Class LinkedList<E> java.lang.Object j ...

  9. java--创建多线程两种方法的比较

    [通过继承Thread] 一个Thread对象只能创建一个线程,即使它调用多次的.start()也会只运行一个的线程. [看下面的代码 & 输出结果] package Test; class ...

  10. Windows Phone 8初学者开发—第22部分:用演示图板创建卷盘的动画

    原文 Windows Phone 8初学者开发—第22部分:用演示图板创建卷盘的动画 第22部分: 用演示图板创建卷盘的动画 原文地址:http://channel9.msdn.com/Series/ ...