友盟UShare分享+复制链接 Swift Version

有几个注意事项:
1、直接用Cocoapod导入吧,能简单pod为何还要手工导入呢,装X?
2、桥接OC还有引入 UMSocialSwiftInterface.swift 文件
3、AppKey换成自己在各大第三方平台上申请的AppKey
4、分享的图片Logo要为Https的
5、URL Types 记得上
6、info.plist的LSApplicationQueriesSchemes也要记得
7、直接上码了

cocopods

# 友盟分享
# U-Share SDK UI模块(分享面板,建议添加)
pod 'UMengUShare/UI'
# 集成微信(精简版0.2M)
pod 'UMengUShare/Social/ReducedWeChat'
# 集成QQ(精简版0.5M)
pod 'UMengUShare/Social/ReducedQQ'
# 集成新浪微博(精简版1M)
pod 'UMengUShare/Social/ReducedSina'

info.plist

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>wechat</string>
        <string>weixin</string>
        <string>sinaweibohd</string>
        <string>sinaweibo</string>
        <string>sinaweibosso</string>
        <string>weibosdk</string>
        <string>weibosdk2.5</string>
        <string>mqqapi</string>
        <string>mqq</string>
        <string>mqqOpensdkSSoLogin</string>
        <string>mqqconnect</string>
        <string>mqqopensdkdataline</string>
        <string>mqqopensdkgrouptribeshare</string>
        <string>mqqopensdkfriend</string>
        <string>mqqopensdkapi</string>
        <string>mqqopensdkapiV2</string>
        <string>mqqopensdkapiV3</string>
        <string>mqqopensdkapiV4</string>
        <string>mqzoneopensdk</string>
        <string>wtloginmqq</string>
        <string>wtloginmqq2</string>
        <string>mqqwpa</string>
        <string>mqzone</string>
        <string>mqzonev2</string>
        <string>mqzoneshare</string>
        <string>wtloginqzone</string>
        <string>mqzonewx</string>
        <string>mqzoneopensdkapiV2</string>
        <string>mqzoneopensdkapi19</string>
        <string>mqzoneopensdkapi</string>
        <string>mqqbrowser</string>
        <string>mttbrowser</string>
    </array>

URL Types

weixin wxdc1e388c3822c80b // 微信
tencent1105821097 // QQ
QQ41E97DA9 // QQ对话
wb3921700954 // 微博

OC桥接

#import <UMSocialCore/UMSocialCore.h>
#import <UShareUI/UShareUI.h>

UMSocialSwiftInterface.swift

//
//  UMSocialSwiftInterface.swift
//  UMSocialDemo
//

import Foundation
class UMSocialSwiftInterface: NSObject {
    
    //swift的分享
    static func share(plattype:UMSocialPlatformType,
                      messageObject:UMSocialMessageObject,
                      viewController:UIViewController?,
                      completion: @escaping (_ data:Any?,_ error:Error?) -> Void) -> Void{
        UMSocialManager.default().share(to: plattype, messageObject: messageObject, currentViewController: viewController) { (shareResponse, error) in
            completion(shareResponse, error);
        }
    }
    
    //授权
    static func auth(plattype:UMSocialPlatformType,
                      viewController:UIViewController?,
                      completion: @escaping (_ data:Any?,_ error:Error?) -> Void) -> Void{
        UMSocialManager.default().auth(with: plattype, currentViewController: viewController) { (shareResponse, error) in
                completion(shareResponse, error);
        }
    }
    
    //取消授权
    static func cancelAuth(plattype:UMSocialPlatformType,
                     completion: @escaping (_ data:Any?,_ error:Error?) -> Void) -> Void{
        UMSocialManager.default().cancelAuth(with: plattype) { (shareResponse, error) in
            completion(shareResponse, error);
        }
    }
    
    //获得用户资料
    static func getUserInfo(plattype:UMSocialPlatformType,
                     viewController:UIViewController?,
                     completion: @escaping (_ data:Any?,_ error:Error?) -> Void) -> Void{
        UMSocialManager.default().getUserInfo(with: plattype, currentViewController: viewController) { (shareResponse, error) in
            completion(shareResponse, error);
        }
    }
    
    //弹出分享面板
    static func showShareMenuViewInWindowWithPlatformSelectionBlock(selectionBlock:@escaping (_ platformType:UMSocialPlatformType, _ userinfo:Any?) -> Void) -> Void {
        UMSocialUIManager.showShareMenuViewInWindow { (platformType, userinfo) in
            selectionBlock(platformType,userinfo);
        }
    }
    
    //设置预定义平台
    //备注:preDefinePlatforms为[NSNumber]的类型
    static func setPreDefinePlatforms(_ preDefinePlatforms: [Any]!) -> Void {
          //调用setPreDefinePlatforms的示例
//        UMSocialUIManager.setPreDefinePlatforms(
//            [NSNumber(integerLiteral:UMSocialPlatformType.wechatSession.rawValue)
//            ,NSNumber(integerLiteral:UMSocialPlatformType.QQ.rawValue)
//            ,NSNumber(integerLiteral:UMSocialPlatformType.sina.rawValue)
//            ]
//        )
        UMSocialUIManager.setPreDefinePlatforms(preDefinePlatforms)
    }
}

AppDelegate 记得要把 AppKey换成自己的

 func initUMSocial(){
        
        UMSocialManager.default().openLog(true)
        UMSocialGlobal.shareInstance().isClearCacheWhenGetUserInfo = false
        UMSocialManager.default().umSocialAppkey = "593e209b7f2c7458e4001ad3"
         // FIXME: URL跳转到 a.app.qq.com on 并 把 appKey 改成上线的APP的  2017.06.12
         /* 微信聊天   */
         UMSocialManager.default().setPlaform(UMSocialPlatformType.wechatSession, appKey: "wxdc1e388c3822c80b", appSecret: "3baf1193c85774b3fd9d18447d76cab0", redirectURL: "http://www.sina.com.cn")
         /* 微信朋友圈*/
        UMSocialManager.default().setPlaform(UMSocialPlatformType.wechatTimeLine, appKey: "wxfd23fac852a54c97", appSecret: "d4624c36b6795d1d99dcf0547af5443d", redirectURL: "http://www.sina.com.cn")
        /* QQ聊天页面*/
         UMSocialManager.default().setPlaform(UMSocialPlatformType.QQ, appKey: "1105821097", appSecret: nil, redirectURL: "http://www.sina.com.cn")
        /* 新浪*/
         UMSocialManager.default().setPlaform(.sina, appKey: "3921700954", appSecret: "04b48b094faeb16683c32669824ebdad", redirectURL: "http://www.sina.com.cn")
    }

把代码放在你想调用的地方ViewController

 // FIXME: -  UM友盟 分享
    open func getShare(){
        
        print ("分享")
        //创建分享消息对象
        UMSocialSwiftInterface.setPreDefinePlatforms([
            (UMSocialPlatformType.wechatSession.rawValue),
            (UMSocialPlatformType.wechatTimeLine.rawValue),
            (UMSocialPlatformType.QQ.rawValue),
            (UMSocialPlatformType.sina.rawValue)
            ])
        
        UMSocialUIManager.removeAllCustomPlatformWithoutFilted()
        UMSocialShareUIConfig.shareInstance().sharePageGroupViewConfig.sharePageGroupViewPostionType = UMSocialSharePageGroupViewPositionType.middle
        UMSocialShareUIConfig.shareInstance().sharePageScrollViewConfig.shareScrollViewPageItemStyleType = UMSocialPlatformItemViewBackgroudType.none
        
        UMSocialSwiftInterface.showShareMenuViewInWindowWithPlatformSelectionBlock { (platformType, userInfo) in
            self.runShareWithType(type: platformType)
        }
        
    }
    
    open func runShareWithType(type:UMSocialPlatformType) -> Void {
        // print("runShareWithType-----\(type)")
        
        
        // FIXME: 图片必须为 https
        let UMS_THUMB_IMAGE = "https://www.elinknet.cn/img/logo.png"
        let UMS_Title = "我是标题"
        let UMS_Web_Desc = "我是描述!"
        let UMS_WebLink = "http://我是APP的下载地址"
        let messageObject = UMSocialMessageObject()
        let shareObject = UMShareWebpageObject.shareObject(withTitle: UMS_Title, descr: UMS_Web_Desc, thumImage: UMS_THUMB_IMAGE)
        //设置网页地址
        shareObject?.webpageUrl = UMS_WebLink
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject
        
        UMSocialSwiftInterface.share(plattype: type, messageObject: messageObject, viewController: self) { (data, error) in
            if (error != nil) {
                SVProgressHUD.showError(withStatus: "请先安装相应APP、再进行分享")
                print(error ?? "未知错误!")
                
            }else{
                
            }
        }
        
    }

2017.10.1日补充自定义icon

  // FIXME: -  UM友盟 分享
   @objc class func shareToAnother(){
        //创建分享消息对象
       //var snsPlatform = UMSocialPlatformConfig.platformName(with: self)
        UMSocialSwiftInterface.setPreDefinePlatforms([
            (UMSocialPlatformType.wechatSession.rawValue),
            (UMSocialPlatformType.wechatTimeLine.rawValue),
            (UMSocialPlatformType.QQ.rawValue),
            (UMSocialPlatformType.sina.rawValue),
            (UMSocialPlatformType.linkUrl.rawValue)
            ])
        UMSocialUIManager.removeAllCustomPlatformWithoutFilted()
        // 分享的POP位置
        UMSocialShareUIConfig.shareInstance().shareTitleViewConfig.isShow = true
        UMSocialShareUIConfig.shareInstance().shareTitleViewConfig.shareTitleViewTitleString = "分享给更多人"
        UMSocialShareUIConfig.shareInstance().sharePageGroupViewConfig.sharePageGroupViewPostionType = UMSocialSharePageGroupViewPositionType.bottom
        UMSocialShareUIConfig.shareInstance().sharePageScrollViewConfig.shareScrollViewPageItemStyleType = UMSocialPlatformItemViewBackgroudType.none

        UMSocialUIManager.addCustomPlatformWithoutFilted(UMSocialPlatformType.linkUrl, withPlatformIcon: UIImage(named:"channel_discover_28x28_"), withPlatformName: "复制链接")
    
        UMSocialSwiftInterface.showShareMenuViewInWindowWithPlatformSelectionBlock { (platformType, userInfo) in
            
            if platformType == UMSocialPlatformType.linkUrl {
                //设置网页地址
                let shareObject = UMShareWebpageObject.shareObject(withTitle: "网页标题", descr: "备注", thumImage: UIImage(named:"channel_discover_28x28_"))
                shareObject?.webpageUrl = "http://www.elinknet.cn"
                let pastboad = UIPasteboard.general
                pastboad.string = shareObject?.webpageUrl
                SVHUD.showSuccess("复制成功", 0.8)
                
            }else {
                self.runShareWithType(platformType)
            }
            
        }
        /*
        UMSocialUIManager.showShareMenuViewInWindow { (platformType, userinfo) in
            printLog(platformType)
            if platformType == UMSocialPlatformType.linkUrl {
                
            }else{
                self.runShareWithType(platformType)
            }
         }
        */
    }

// 关键语句 在UMSocialPlatformConfig.h的
typedef NS_ENUM(NSInteger,UMSocialPlatformType)
{}函数内增加   
UMSocialPlatformType_LinkUrl = 888, // URL自定义
真是搞不明白为何友盟要把这个代码和方法隐藏的那么深呢?

希望对各位有帮助!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,402评论 6 499
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,377评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,483评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,165评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,176评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,146评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,032评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,896评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,311评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,536评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,696评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,413评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,008评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,815评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,698评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,592评论 2 353

推荐阅读更多精彩内容