1)注册账号
2)到https://open.weixin.qq.com资源中心下载sdk包
3)把下载的整个文件夹都拖到项目里
4)在Build Settings / Other Linker Flags: -Objc -all_load
5)创建bridge.h, 加入:
#import “WechatAuthSDK.h”
#import “WXApi.h”
#import “WXApiObject.h”
6)在Build Settings / Swift Compiler / Objective-C Bridging Header: {ProjectName}/bridge.h
前面要加项目名,不然会提示找不到bridge.h文件
7)在Linked Frameworks and Libraries 加入微信sdk依赖库:
CoreTelephony.framwork
SystemConfiguration.framework
libc++.tbd
libsqlite3.0.tbd
libz.tbd
libWeChatSDK.a (sdk拖入会自动添加)
8)在info / URL types增加一条记录
identifier:weixin
URLSchemes:是微信appid,测试用wxe569f3b01ff5573(来源hangge.com)
9)在info.plist增加
App Transport Security打开Arbitray Allow Loads
LSApplicationQueriesSchemes(Array):weixin
10)测试代码:转载hangge.com
–》AppDelegate.swift <–
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, WXApiDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// 注册app
WXApi.registerApp("wxe569f3b201ff5573")
return true
}
//重写openURL
func application(_ app: UIApplication, open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return WXApi.handleOpen(url, delegate: self)
}
//微信分享完毕后的回调(只有使用真实的AppID才能收到响应)
func onResp(_ resp: BaseResp!) {
if resp.isKind(of: SendMessageToWXResp.self) {//确保是对我们分享操作的回调
if resp.errCode == WXSuccess.rawValue{//分享成功
print("分享成功")
}else if resp.errCode == WXErrCodeCommon.rawValue {//普通错误类型
print("分享失败:普通错误类型")
}else if resp.errCode == WXErrCodeUserCancel.rawValue {//用户点击取消并返回
print("分享失败:用户点击取消并返回")
}else if resp.errCode == WXErrCodeSentFail.rawValue {//发送失败
print("分享失败:发送失败")
}else if resp.errCode == WXErrCodeAuthDeny.rawValue {//授权失败
print("分享失败:授权失败")
}else if resp.errCode == WXErrCodeUnsupport.rawValue {//微信不支持
print("分享失败:微信不支持")
}
}
}
func applicationWillResignActive(_ application: UIApplication) {
}
func applicationDidEnterBackground(_ application: UIApplication) {
}
func applicationWillEnterForeground(_ application: UIApplication) {
}
func applicationDidBecomeActive(_ application: UIApplication) {
}
func applicationWillTerminate(_ application: UIApplication) {
}
}
–>ViewController.swift<–
import UIKit
class ViewController: UIViewController {
//发送给好友还是朋友圈(默认好友)
var _scene = Int32(WXSceneSession.rawValue)
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//切换发送给好友还是朋友圈
@IBAction func changeScene(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
_scene = Int32(WXSceneSession.rawValue)
}else{
_scene = Int32(WXSceneTimeline.rawValue)
}
}
//发送纯文本
@IBAction func sendTextContent(_ sender: AnyObject) {
let req = SendMessageToWXReq()
req.bText = true
req.text = "hangge.com 做最好的开发者知识平台。"
req.scene = _scene
WXApi.send(req)
}
//发送图片
@IBAction func sendImageContent(_ sender: AnyObject) {
let message = WXMediaMessage()
//发送的图片
let filePath = Bundle.main.path(forResource: "image", ofType: "jpg")
let image = UIImage(contentsOfFile:filePath!)
let imageObject = WXImageObject()
imageObject.imageData = UIImagePNGRepresentation(image!)
message.mediaObject = imageObject
//图片缩略图
let width = 240.0 as CGFloat
let height = width*image!.size.height/image!.size.width
UIGraphicsBeginImageContext(CGSize(width: width, height: height))
image!.draw(in: CGRect(x: 0, y: 0, width: width, height: height))
message.setThumbImage(UIGraphicsGetImageFromCurrentImageContext())
UIGraphicsEndImageContext()
let req = SendMessageToWXReq()
req.bText = false
req.message = message
req.scene = _scene
WXApi.send(req)
}
//发送链接
@IBAction func sendLinkContent(_ sender: AnyObject) {
let message = WXMediaMessage()
message.title = "欢迎访问 hangge.com"
message.description = "做最好的开发者知识平台。分享各种编程开发经验。"
message.setThumbImage(UIImage(named:"apple.png"))
let ext = WXWebpageObject()
ext.webpageUrl = "http://hangge.com"
message.mediaObject = ext
let req = SendMessageToWXReq()
req.bText = false
req.message = message
req.scene = _scene
WXApi.send(req)
}
//发送音乐
@IBAction func sendMusicContent(_ sender: AnyObject) {
let message = WXMediaMessage()
message.title = "一无所有"
message.description = "崔健"
message.setThumbImage(UIImage(named:"apple.png"))
let ext = WXMusicObject()
ext.musicUrl = "http://y.qq.com/portal/song/103347_num.html"
ext.musicDataUrl = "http://stream20.qqmusic.qq.com/32464723.mp3"
message.mediaObject = ext
let req = SendMessageToWXReq()
req.bText = false
req.message = message
req.scene = _scene
WXApi.send(req)
}
//发送视频
@IBAction func sendVideoContent(_ sender: AnyObject) {
let message = WXMediaMessage()
message.title = "乔布斯访谈"
message.description = "饿着肚皮,傻逼着。"
message.setThumbImage(UIImage(named:"apple.png"))
let ext = WXVideoObject()
ext.videoUrl = "http://v.youku.com/v_show/id_XNTUxNDY1NDY4.html"
message.mediaObject = ext
let req = SendMessageToWXReq()
req.bText = false
req.message = message
req.scene = _scene
WXApi.send(req)
}
//发送非gif格式的表情
@IBAction func sendNonGifContent(_ sender: AnyObject) {
let message = WXMediaMessage()
message.setThumbImage(UIImage(named:"res5thumb.png"))
let ext = WXEmoticonObject()
let filePath = Bundle.main.path(forResource: "res5", ofType: "jpg")
let url = URL(fileURLWithPath: filePath!)
ext.emoticonData = try! Data(contentsOf: url)
message.mediaObject = ext
let req = SendMessageToWXReq()
req.bText = false
req.message = message
req.scene = _scene
WXApi.send(req)
}
//发送gif格式的表情
@IBAction func sendGifContent(_ sender: AnyObject) {
let message = WXMediaMessage()
message.setThumbImage(UIImage(named:"res6thumb.png"))
let ext = WXEmoticonObject()
let filePath = Bundle.main.path(forResource: "res6", ofType: "gif")
let url = URL(fileURLWithPath: filePath!)
ext.emoticonData = try! Data(contentsOf: url)
message.mediaObject = ext
let req = SendMessageToWXReq()
req.bText = false
req.message = message
req.scene = _scene
WXApi.send(req)
}
//发送文件
@IBAction func sendFileContent(_ sender: AnyObject) {
let message = WXMediaMessage()
message.title = "ML.pdf"
message.description = "Pro CoreData"
message.setThumbImage(UIImage(named:"apple.png"))
let ext = WXFileObject()
ext.fileExtension = "pdf"
let filePath = Bundle.main.path(forResource: "ML", ofType: "pdf")
let url = URL(fileURLWithPath: filePath!)
ext.fileData = try! Data(contentsOf: url)
message.mediaObject = ext
let req = SendMessageToWXReq()
req.bText = false
req.message = message
req.scene = _scene
WXApi.send(req)
}
}