通过调用微信提供的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