之前写了OC版的友盟分享,现在来份Swift的(注:Siwft的是有坑的)
pod导入
pod 'UMCCommon' #友盟基础库
pod 'UMCSecurityPlugins' #友盟安全组件
pod 'UMCShare/UI' #UI模块(分享面板,建议添加)
pod 'UMCShare/Social/ReducedWeChat' #集成微信(精简版0.2M)
pod 'UMCShare/Social/ReducedQQ' #集成QQ/QZone/TIM(精简版0.5M)
桥接文件(注:现在注册友盟改变了)
#import <UMCommon/UMCommon.h> // 导入这个是重点 不然没法注册
#import <UMShare/UMShare.h>
#import <UShareUI/UShareUI.h>
其他准备条件与OC版一样的
那我们就直接开始吧,这里直接对AppDelegate进行扩展
let UMAppkey = "友盟Appkey"
/// 微信appkey
let WeChatAppkey = "微信Appkey"
let WeChatAppSecret = "微信Appkey"
/// QQappkey
let QQAppkey = "QQAppID"
let QQAppSecret = "QQAppkey"
extension AppDelegate {
func setUpUM(){
UMSocialManager.default().openLog(true)
UMConfigure.initWithAppkey(UMAppkey, channel: "App Store")
if !WeChatAppkey.isEmpty {
UMSocialManager.default().setPlaform(UMSocialPlatformType.wechatSession, appKey: WeChatAppkey, appSecret: WeChatAppSecret, redirectURL: nil)
}
if !QQAppkey.isEmpty {
UMSocialManager.default().setPlaform(UMSocialPlatformType.QQ, appKey: QQAppkey, appSecret: QQAppSecret, redirectURL: nil)
}
}
}
回调方法是一样的这里就不写了
直接开始分享内容吧,还是一样的创建一个分享模型
class HWShareItem : NSObject {
/// 分享URL
var url:String?
/// 分享标题
var title:String?
/// 分享内容
var content:String?
/// 分享图片 (与分享图URL二选一)
var icon:UIImage?
/// 分享图片URL(字符串)
var iconUrlString:String?
/// 分享模型构造方法 title:分享标题 content:分享内容 url:分享URL icon:分享图片 iconUrlString:分享图片URL(字符串)
convenience init(title:String,content:String,url:String,icon:UIImage? ,iconUrlString:String?){
self.init()
self.title = title
self.content = content
self.url = url
self.icon = icon
self.iconUrlString = iconUrlString
}
}
这里需要注意
array.addObjects(from: [UMSocialPlatformType.wechatSession , UMSocialPlatformType.wechatTimeLine])
通过这样去添加你会发现你的分享面板啥都没有,之前开始弄的时候我也郁闷了一天,后面改成了对应的角标了,问题解决 也可以通过UMSocialPlatformType.init(rawValue: 1)
这样来添加
class HWShare: NSObject {
private static let instance: HWShare = HWShare()
class func sharedShareManage() -> HWShare {
return instance
}
func showShareUI(item:HWShareItem,_ action:((Bool)->())? = nil) {
let array = NSMutableArray.init()
if !WeChatAppkey.isEmpty {
array.addObjects(from: [UMSocialPlatformType.wechatSession.rawValue , UMSocialPlatformType.wechatTimeLine.rawValue])
}
if !QQAppkey.isEmpty {
array.addObjects(from: [UMSocialPlatformType.QQ.rawValue, UMSocialPlatformType.qzone.rawValue])
}
UMSocialUIManager.setPreDefinePlatforms(array as! [NSInteger])
UMSocialUIManager.showShareMenuViewInWindow { (platformType, userInfo) in
self.shareWebPageToPlatformType(platformType, item, action)
}
}
/// 分享
func shareWebPageToPlatformType(_ platformType:UMSocialPlatformType,_ item:HWShareItem, _ action:((Bool)->())? = nil){
//创建分享消息对象
let messageObject = UMSocialMessageObject.init()
var thumImage:UIImage = hw_Image(named: "分享图标")
if item.icon != nil {
thumImage = item.icon ?? hw_Image(named: "分享图标")
} else if (item.iconUrlString?.count ?? 0) > 0 {
let imageView = UIImageView.init()
imageView.hw_setImage(item.iconUrlString, nil)
thumImage = imageView.image ?? hw_Image(named: "分享图标")
}
//创建网页内容对象
let shareObject = UMShareWebpageObject.shareObject(withTitle: item.title ?? "分享标题", descr: item.content ?? "分享内容", thumImage: thumImage)
//设置网页地址
shareObject?.webpageUrl = item.url
//分享消息对象设置分享内容对象
messageObject.shareObject = shareObject
//调用分享接口
UMSocialManager.default().share(to: platformType, messageObject: messageObject, currentViewController: UIApplication.shared.keyWindow?.rootViewController) { (data, error) in
if ((error) != nil) {
action?(false)
}else{
action?(true)
}
}
}
使用: item本地图片和图片URL二选一就好(最好用本地图片,因为网络图片下载需要时间)
// 创建分享模型
let item = HWShareItem.init(title: "你的标题", content: "来自环球渔网分享", url: "你的URL", icon: UIImage(), iconUrlString: nil)
// 调用分享面板
HWShare.shared().showShareUI(item: item) { (tag) in
print("\(tag == true ? "分享成功" : "分享失败")")
}
下面是登录
@IBAction func otherLoginButton(_ btn: UIButton) {
if btn == qqButton {
HWPrint("qq登录")
UMSocialManager.default().getUserInfo(with: UMSocialPlatformType.QQ, currentViewController: nil) { [weak self] (result, error) in
if error == nil {
let resp = result as! UMSocialUserInfoResponse
HWPrint(resp.uid)
获取uid处理你的登录逻辑
} else { // 授权失败
HWPrint(error)
}
}
} else {
HWPrint("微信登录")
UMSocialManager.default().getUserInfo(with: UMSocialPlatformType.wechatSession, currentViewController: nil) { [weak self] (result, error) in
if error == nil {
let resp = result as! UMSocialUserInfoResponse
HWPrint(resp.uid)
获取uid处理你的登录逻辑
} else { // 授权失败
HWPrint(error)
}
}
}
}
其他
/// 判断设备是否安装微信
if UIApplication.shared.canOpenURL(URL(string: "weixin://")!) == false {
HWHUDManage.sharedHUD.hw_showtitleHUD(name: "设备未安装微信")
return
}
/// 判断设备是否按QQ
if UIApplication.shared.canOpenURL(URL(string: "mqq://")!) == false {
HWHUDManage.sharedHUD.hw_showtitleHUD(name: "设备未安装QQ")
return
}