Swift4里使用友盟分享,自定义按钮

1.通过pod集成:

  #友盟相关
  pod 'UMCCommonLog' #调试插件,上线时去掉
  pod 'UMCSecurityPlugins' #安全插件
  pod 'UMCCommon' #通用库
  pod 'UMCAnalytics' #统计
  pod 'UMCPush' #推送
  pod 'UMCShare/UI' #分享的UI
  pod 'UMCShare/Social/ReducedWeChat' #微信
  pod 'UMCShare/Social/ReducedQQ' #qq
  pod 'UMCShare/Social/ReducedSina' #新浪微博

2.按照文档配置白名单和第三方Key

然后在桥接文件(Swift工程创建OC类时自动创建的文件,可以当做以前的PCH文件使用)里引入友盟:

#import 
#import 
#import 
#import 
#import 
#import 

3.代码里配置友盟和第三方Key:

func confitUShareSettings() {
        
        UMSocialGlobal.shareInstance().isUsingHttpsWhenShareContent = false
        UMSocialGlobal.shareInstance().isUsingWaterMark = false
        
        UMConfigure.initWithAppkey(kUMAppKey, channel: nil)
    }
    
    func configUSharePlatforms() {
        /* 设置微信的appKey和appSecret */
        
        UMSocialManager.default().setPlaform(.wechatSession, appKey: kWeiXinKey, appSecret: kWeiXinSecret, redirectURL: kCallBackURL)
        UMSocialManager.default().setPlaform(.wechatTimeLine, appKey: kWeiXinKey, appSecret: kWeiXinSecret, redirectURL: kCallBackURL)
        
        /*QQ*/
        UMSocialManager.default().setPlaform(.QQ, appKey: kQQKey, appSecret: nil, redirectURL: kCallBackURL)
        UMSocialManager.default().setPlaform(.qzone, appKey: kQQKey, appSecret: nil, redirectURL: kCallBackURL)
        
        /*新浪微博*/
        UMSocialManager.default().setPlaform(.sina, appKey: kSinaKey, appSecret: kQQAppSecret, redirectURL: kCallBackURL)
    }

4.可选,配置一个自定义的按钮:

let kPlatformForward = UMSocialPlatformType.init(rawValue: 1001)!
let kPlatformCopyUrl = UMSocialPlatformType.init(rawValue: 1002)!
let kPlatformSystemShare = UMSocialPlatformType.init(rawValue: 1003)!

    func addCustomPlatform() {
        
       UMSocialUIManager
        .addCustomPlatformWithoutFilted(
            kPlatformForward,
            withPlatformIcon: UIImage.init(named: "walan"),
            withPlatformName: "站内转发")
        UMSocialUIManager
            .addCustomPlatformWithoutFilted(
                kPlatformCopyUrl,
                withPlatformIcon: UIImage.init(named: "copy"),
                withPlatformName: "复制链接")
        UMSocialUIManager
            .addCustomPlatformWithoutFilted(
                kPlatformSystemShare,
                withPlatformIcon: UIImage.init(named: "system"),
                withPlatformName: "系统分享")
        
    }

5.预设置平台顺序:

        //如果没有自定义的按钮,可以去掉kPlatformForward这些

        UMSocialUIManager.setPreDefinePlatforms([
            NSNumber(integerLiteral: kPlatformForward.rawValue),
            NSNumber(integerLiteral: UMSocialPlatformType.wechatTimeLine.rawValue),
            NSNumber(integerLiteral: UMSocialPlatformType.wechatSession.rawValue),
            
            NSNumber(integerLiteral: UMSocialPlatformType.QQ.rawValue),
            NSNumber(integerLiteral: UMSocialPlatformType.qzone.rawValue),
            NSNumber(integerLiteral: UMSocialPlatformType.sina.rawValue),
            NSNumber(integerLiteral: kPlatformCopyUrl.rawValue),
            NSNumber(integerLiteral: kPlatformSystemShare.rawValue)
            
            ])

6.定义一个全局的分享函数:

func shareImageOrText(vc: UIViewController,
                      platformType: UMSocialPlatformType,
                      message: String?,
                      image: String?,
                      thumbImage: String?,
                      complate: ((Any?, NSError?) -> ())?) {
    //创建分享消息对象
    let messageObject = UMSocialMessageObject()
    //设置文本
    if let message = message {
        messageObject.text = message
    }
    
    if let image = image{
        //创建图片内容对象
        let shareObject = UMShareImageObject()
        shareObject.shareImage = image
        //如果有缩略图,则设置缩略图
        if let thumbImage = thumbImage {
            shareObject.thumbImage = UIImage(named: thumbImage)
        }
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject
    }
    
    
    UMSocialManager.default().share(to: platformType, messageObject: messageObject, currentViewController: vc) { (data, error) in
        if let complate = complate{
            complate(data, error as NSError?)
        }
        
    }

7.代理中实现回调:

    // 支持所有iOS系统
    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
            //6.3的新的API调用,是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
        let result: Bool = UMSocialManager.default().handleOpen(url, sourceApplication: sourceApplication, annotation: annotation)
        
        if !result {
            // 其他如支付等SDK的回调
        }
        return result
    }
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
            //6.3的新的API调用,是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
        let result: Bool = UMSocialManager.default().handleOpen(url, options: options)
        if !result {
            // 其他如支付等SDK的回调
        }
        return result
    }

PS: 以上可以写在AppDelegate的扩展AppDelegate+UM中

8.在需要的地方,调起分享面板:

        //kPlatformSystemShare等是自定义的,前面定义了才会有
        UMSocialUIManager.showShareMenuViewInWindow { (type, dic) in
            
            if type == kPlatformSystemShare{
                print("kPlatformSystemShare")
            }else if type == kPlatformCopyUrl{
                print("kPlatformCopyUrl")
            }else if type == kPlatformForward{
                print("kPlatformForward")
            }else{
                shareImageOrText(vc: self, platformType: type, message: "你猜", image: nil, thumbImage: nil, complate: { (data, error) in
                    
                })
            }
            

        }

你可能感兴趣的:(Swift4里使用友盟分享,自定义按钮)