swift开发之 -- 微信原生分享
swift集成微信原生分享,并没有用友盟的第三方sdk,直接对接的原生,相比较OC版的话,swift来说代码量很少,代码如下:
1,导入WechatOpenSDK,推荐使用cocoapods集成的话,非常便捷,具体操作流程就不多说了,可查看我的关于cocoapods的博客,如下图:
注:CD到当前项目路径,然后使用pod search WechatOpenSDK,可以查找最新版本;
执行vim Podfile命令,按 i 键,进入编辑模式,在文件中加入 pod 'WechatOpenSDK'
最后执行 pod install,或者 pod update,如需更新本地仓库执行 pod repo update命令
按esc键,:wq,退出即可
2,集成完后,需要创建桥接文件,然后配置下桥接文件的路径,可配置一个绝对路径:
从工程的路径开始,前面使用$(SRCROOT)代替,这样的话,就不用每次变更项目路径就得配置桥接文件路径了。
3,添加白名单:
如上图红框里面一样,添加“LSApplicationQueriesSchemes”,添加 wechat,weixin,weixinULAPI
4,在桥接文件里面导入头文件
5,添加URL Types:
6,在AppDelegate文件里面,添加如下代码:
universal link,这个通用链接的话,可以参考微信官方文档,这里就不在详细叙述了:
https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Access_Guide/iOS.html
7,回调方法,代码如下:
1 //iOS 9.0以前使用 2 func application(_ application: UIApplication, handleOpen url : URL) -> Bool{ 3 let handleUrlStr = url.absoluteString 4 if let handleUrl = URL(string: handleUrlStr) { 5 return WXApi.handleOpen(handleUrl, delegate: self) 6 } 7 return false 8 } 9 10 //ios 9.0及以后推荐使用 11 func application(_ app: UIApplication, open url: URL, options : [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool{ 12 let handleUrlStr = url.absoluteString 13 if let handleUrl = URL(string: handleUrlStr) { 14 return WXApi.handleOpen(handleUrl, delegate: self) 15 } 16 return false 17 } 18 19 //必须实现 20 func application(_ application: UIApplication, open url: URL, sourceApplication:String?, annotation:Any) -> Bool{ 21 let handleUrlStr = url.absoluteString 22 if let handleUrl = URL(string: handleUrlStr) { 23 return WXApi.handleOpen(handleUrl, delegate: self) 24 } 25 return false 26 } 27 28 func application(_ application: UIApplication, continue userActivity: NSUserActivity,restorationHandler:@escaping([UIUserActivityRestoring]?) -> Void) -> Bool{ 29 return WXApi.handleOpenUniversalLink(userActivity, delegate: self) 30 31 }
8,具体调用,代码如下:
声明一个枚举:
1 enum LDShareType { 2 case Session, Timeline, Favorite/*会话, 朋友圈, 收藏*/ 3 }
分享文本:
1 func shareText(_ text: String, to scene: LDShareType) { 2 3 let req = SendMessageToWXReq() 4 req.text = text 5 req.bText = true 6 7 switch scene { 8 case .Session: 9 req.scene = Int32(WXSceneSession.rawValue) 10 case .Timeline: 11 req.scene = Int32(WXSceneTimeline.rawValue) 12 case .Favorite: 13 req.scene = Int32(WXSceneFavorite.rawValue) 14 } 15 16 WXApi.send(req) 17 }
分享图片:
1 func shareImage(_ data: Data, thumbImage: UIImage, title: String, description: String, to scene: LDShareType) { 2 3 let message = WXMediaMessage() 4 message.setThumbImage(thumbImage) 5 message.title = title 6 message.description = description 7 8 let obj = WXImageObject() 9 obj.imageData = data 10 message.mediaObject = obj 11 12 let req = SendMessageToWXReq() 13 req.bText = false 14 req.message = message 15 16 switch scene { 17 case .Session: 18 req.scene = Int32(WXSceneSession.rawValue) 19 case .Timeline: 20 req.scene = Int32(WXSceneTimeline.rawValue) 21 case .Favorite: 22 req.scene = Int32(WXSceneFavorite.rawValue) 23 } 24 25 WXApi.send(req) 26 }
分享音乐:
1 func shareMusic(to scene: LDShareType) { 2 let message = WXMediaMessage() 3 message.title = "音乐标题" 4 message.description = "音乐描述" 5 message.setThumbImage(UIImage()) 6 7 let obj = WXMusicObject() 8 obj.musicUrl = "音乐链接" 9 obj.musicLowBandUrl = obj.musicUrl 10 11 obj.musicDataUrl = "音乐数据链接地址" 12 obj.musicLowBandDataUrl = obj.musicDataUrl 13 message.mediaObject = obj 14 15 let req = SendMessageToWXReq() 16 req.bText = false 17 req.message = message 18 switch scene { 19 case .Session: 20 req.scene = Int32(WXSceneSession.rawValue) 21 case .Timeline: 22 req.scene = Int32(WXSceneTimeline.rawValue) 23 case .Favorite: 24 req.scene = Int32(WXSceneFavorite.rawValue) 25 } 26 27 WXApi.send(req) 28 }
分享视频:
1 func shareVideo(to scene: LDShareType) { 2 3 let message = WXMediaMessage() 4 message.title = "视频标题" 5 message.description = "视频描述" 6 message.setThumbImage(UIImage()) 7 8 let obj = WXVideoObject() 9 obj.videoUrl = "视频链接地址" 10 obj.videoLowBandUrl = "低分辨率视频地址" 11 12 let req = SendMessageToWXReq() 13 req.bText = false 14 req.message = message 15 16 switch scene { 17 case .Session: 18 req.scene = Int32(WXSceneSession.rawValue) 19 case .Timeline: 20 req.scene = Int32(WXSceneTimeline.rawValue) 21 case .Favorite: 22 req.scene = Int32(WXSceneFavorite.rawValue) 23 } 24 25 WXApi.send(req) 26 }
分享URL:
1 func shareURL(to scene: LDShareType) { 2 3 let message = WXMediaMessage() 4 message.title = "title" 5 message.description = "description" 6 message.setThumbImage(UIImage()) 7 8 let obj = WXWebpageObject() 9 obj.webpageUrl = "http://www.baidu.com" 10 message.mediaObject = obj 11 12 let req = SendMessageToWXReq() 13 req.bText = false 14 req.message = message 15 16 switch scene { 17 case .Session: 18 req.scene = Int32(WXSceneSession.rawValue) 19 case .Timeline: 20 req.scene = Int32(WXSceneTimeline.rawValue) 21 case .Favorite: 22 req.scene = Int32(WXSceneFavorite.rawValue) 23 } 24 25 WXApi.send(req) 26 }
结果回调方法:
1 func onResp(_ resp: BaseResp!) { 2 if resp is SendAuthResp { 3 // 微信登录 4 5 } else if resp is SendMessageToWXResp { 6 let send = resp as? SendMessageToWXResp 7 if let sm = send { 8 if sm.errCode == 0 { 9 print("分享成功") 10 } else { 11 print("分享失败") 12 } 13 } 14 } 15 16 }
以上代码亲测过,仅做记录,刚写swift代码,感谢下面简书两位博主的博客,微信的sdk写的也还行。
本文来自博客园,作者:稻草人11223,转载请注明原文链接:https://www.cnblogs.com/hero11223/p/15428846.html