友盟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自定义
真是搞不明白为何友盟要把这个代码和方法隐藏的那么深呢?

希望对各位有帮助!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容