功能介绍: 自己简单写的分享工具类,主要用于向 微信、微博、QQ 分享多媒体消息(多媒体消息是项目中常用的分享内容)。
考虑到在项目可能会有多个地方调用,因此对该类功能代码进行了整合,方便开发中调用。
在此分享到微博希望对大家有所帮助。
配图:
微信、新浪微博、QQ的官方SDK需要大家自己手动导入,导入方法及配置详见官方文档!
代码:(短信功能详见地址:
http://www.jb51.cc/article/p-nsmpfkiw-gh.html
)
class Sharetool: NSObject { //MARK: 网页的URL地址 class func getWebpageUrl() -> String { let webpageUrl = "xxx" return webpageUrl } //MARK: 分享到微信好友 /** 分享到微信好友 * Title: 标题 * description: 描述内容 * thumbImagename: 缩略图名称(本地) */ class func sharetoWXScenesession(Title: String?,description: String?,thumbImagename: String) { if (WXAPI.isWXAppInstalled()) { var sendMessagetoWXRequest = SendMessagetoWXReq() sendMessagetoWXRequest.bText = false sendMessagetoWXRequest.scene = 0 var mediaMessage = WXMediaMessage() var webpageObject = WXWebpageObject() webpageObject.webpageUrl = self.getWebpageUrl() mediaMessage.mediaObject = webpageObject if Title != nil { mediaMessage.Title = Title } if description != nil { mediaMessage.description = description } mediaMessage.setThumbImage(UIImage(named: thumbImagename)) sendMessagetoWXRequest.message = mediaMessage WXAPI.sendReq(sendMessagetoWXRequest) } else { MPAlertVIEw.showAlertVIEw("请安装微信客户端") } } //MARK: 分享到微信朋友圈 /** 分享到微信朋友圈 * Title: 标题,可为nil * description: 描述内容,可为nil * thumbImagename: 缩略图名称(本地) */ class func sharetoWXSceneTimeline(Title: String?,thumbImagename: String) { if (WXAPI.isWXAppInstalled()) { var sendMessagetoWXRequest = SendMessagetoWXReq() sendMessagetoWXRequest.bText = false sendMessagetoWXRequest.scene = 1 var mediaMessage = WXMediaMessage() var webpageObject = WXWebpageObject() webpageObject.webpageUrl = self.getWebpageUrl() mediaMessage.mediaObject = webpageObject if Title != nil { mediaMessage.Title = Title! } if description != nil { mediaMessage.description = description! } mediaMessage.setThumbImage(UIImage(named: thumbImagename)) sendMessagetoWXRequest.message = mediaMessage WXAPI.sendReq(sendMessagetoWXRequest) } else { MPAlertVIEw.showAlertVIEw("请安装微信客户端") } } //MARK: 分享到新浪微博 /** 分享到新浪微博 * Title: 多媒体内容标题 (不能为空且长度小于1k) * description: 多媒体内容描述 (多媒体内容描述,可为nil) * text: 消息的文本内容(长度小于140个汉字,可为nil) * thumbImagename: 缩略图名称(本地) */ class func sharetoWeibo(Title: String,text: String?,thumbImagename: String) { if (WeiboSDK.isWeiboAppInstalled()) { var webpageObject = WBWebpageObject() webpageObject.webpageUrl = self.getWebpageUrl() webpageObject.objectID = kWeiboKey webpageObject.thumbnailData = self.getimageData(thumbImagename) webpageObject.Title = Title if description != nil { webpageObject.description = description! } var message = WBMessageObject() message.mediaObject = webpageObject if text != nil { message.text = text! } var sendMessagetoWeiboRequest = WBSendMessagetoWeiboRequest() sendMessagetoWeiboRequest.message = message WeiboSDK.sendRequest(sendMessagetoWeiboRequest) } else { MPAlertVIEw.showAlertVIEw("请安装新浪微博客户端") } } //MARK: 分享到QQ好友 /** 分享到QQ好友 * Title 分享内容的标题 * description 分享内容的描述,可为nil * thumbImagename: 缩略图名称(本地) */ class func sharetoQQFrIEnds(Title: String,thumbImagename: String) { if (QQAPIInterface.isQQInstalled()) { var oauth = TencentOAuth(appID: kQQAppID,andDelegate: nil) var newsObj = QQAPINewsObject.objectWithURL(NSURL(string: self.getWebpageUrl()),Title: Title,description: description,prevIEwImageData: self.getimageData(thumbImagename)) as! QQAPIObject var request = SendMessagetoQQReq(content: newsObj) var resultCode = QQAPIInterface.sendReq(request) } else { MPAlertVIEw.showAlertVIEw("请安装腾讯QQ客户端") } } //MARK: 复制邀请码 class func copyInvitecode() { var pasteboard = UIPasteboard.generalPasteboard() pasteboard.string = NSUserDefaults.standardUserDefaults().stringForKey(kInviteCode) MPAlertVIEw.showAlertVIEw("已复制邀请码") } private class func getimageData(localimagename: String) -> NSData { var imageData = UIImagePNGRepresentation(UIImage(named: localimagename)) if (imageData == nil) { imageData = UIImageJPEGRepresentation(UIImage(named: localimagename),1) } return imageData }}总结
以上是内存溢出为你收集整理的Swift 分享多媒体消息到 微信/新浪微博/QQ全部内容,希望文章能够帮你解决Swift 分享多媒体消息到 微信/新浪微博/QQ所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)