Swift 分享多媒体消息到 微信/新浪微博/QQ

前端之家收集整理的这篇文章主要介绍了Swift 分享多媒体消息到 微信/新浪微博/QQ前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

功能介绍: 自己简单写的分享工具类,主要用于向 微信、微博、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相关文章