友盟分享

分享集成


  1. Cocopods集成
pod ‘UMengUShare/UI’, '6.4.5'
# 集成微信(完整版14.4M)
pod ‘UMengUShare/Social/WeChat’, ‘6.4.5’

pod install

  1. 桥接文件导入
/*****************    UM       ************************/
#import 
#import 
#import 
// U-Share核心SDK
#import 
// U-Share分享面板SDK,未添加分享面板SDK可将此行去掉
#import 

  1. 在AppDelegate里面配置推送相关
func configUM() {
      UMSocialManager.default().openLog(true)
      UMSocialManager.default().umSocialAppkey = MB_UMAPPKey

      // Wechat
      UMSocialManager.default().setPlaform(.wechatSession, appKey: MB_WechatAppID, appSecret: MB_WechatAppSecret, redirectURL: MB_OfficialWebUrl)
  }
  1. 配置回调
/// 建议使用的系统openURL回调,且 新浪 平台仅支持以上回调。还有以下两种回调方式,如果开发者选取以下回调,也请补充相应的函数调用。
  func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
      let result = UMSocialManager.default().handleOpen(url, sourceApplication: sourceApplication, annotation: annotation)
      if result == false {
          // 其他如支付等SDK的回调
      }
      return result;
  }
  1. 配置URLSchemes
平台 配置 配置
微信 appid -
QQ QQ41ee1dd7 tencent1106124247
微博 wb3531109777 -
  1. 自定义分享页面
typealias CallBackBlock = () -> Void
class MMShareViewController: UIViewController {

    var titleStr: String?
    var bodyStr: String?
    var imageUrlStr: String?
    var shareUrlStr: String?
    var shareSuccessBlock:CallBackBlock?

    init() {
        super.init(nibName: nil, bundle: nil)
        modalPresentationStyle = .custom
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func touchesBegan(_ touches: Set, with event: UIEvent?) {
        dismiss(animated: true, completion: nil)
    }
}

extension MMShareViewController {
    fileprivate func setupUI() {

    }
}
extension MMShareViewController {
    @IBAction func shareButtonClick(_ sender: UIButton) {
        switch sender.tag {
        /// 微信分享
        case 0:
            let result = WXApi.isWXAppInstalled()
            share(result: result, title: "微信", platformType: .wechatSession)

        /// 朋友圈分享
        case 1:
            let result = WXApi.isWXAppInstalled()
            share(result: result, title: "微信", platformType: .wechatTimeLine)
        default:
            print("选择未配置")
        }
    }

    @IBAction func closeButtonClick() {
    }

    func share(result: Bool,title: String,platformType:UMSocialPlatformType){
        if result == false {
            self.hud_showTextTitle("您还未安装\(title)客户端")
            return
        }
        self.dismiss(animated: true, completion: nil)

        let messageObject = UMSocialMessageObject()
        let shareObject = UMShareWebpageObject()
        shareObject.title = titleStr ?? "我的语音"
        shareObject.thumbImage = imageUrlStr ?? UIImage(named: "Common_icon")
        shareObject.descr = bodyStr ?? "听听看"
        shareObject.webpageUrl = shareUrlStr ?? downUrl
        messageObject.shareObject = shareObject
        UMSocialManager.default().share(to: platformType, messageObject: messageObject, currentViewController: nil) { (data, error) in

            if (error == nil) {
                // 成功请求接口
                if self.shareSuccessBlock != nil { self.shareSuccessBlock!() }
            } else {
                self.hud_showTextTitle("分享失败")
            }
        }
    }
}

我不愿成为你退而求其次的人。
以上内容纯属个人笔记,不作其他用途

你可能感兴趣的:(友盟分享)