123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- //
- // UMManager.swift
- // RainbowPlanet
- //
- // Created by 南鑫林 on 2019/3/7.
- // Copyright © 2019 南鑫林. All rights reserved.
- //
- import UIKit
- import SwiftyJSON
- //MARK: - UMeng
- let kUMengAppKey = "5d4d38ce0cafb2131c00041c"
- let kUMengAppSecret = "mllaspth0pdgnhdm02uvkppby3isxxex"
- //彩虹星球
- //let kUMengAppKey = "5c984a7f0cafb2332300000f"
- //let kUMengAppSecret = "lpx7kstlnlrxmimo5gfyzvtscrtguhw8"
- //MARK: - QQ跟安卓用同一个
- let kQQAppKey = "101742987"
- let kQQAppSecret = "664edcf45543f1958b46f914c03b3ed0"
- //彩虹星球
- //let kQQAppKey = "101565722"
- //let kQQAppSecret = "dfe6f96762ed0dbc3ad52dc06d0eda9b"
- //MARK: - 微信
- let kWeiXinAppKey = "wx0aed0c936870ad29"
- let kWeiXinAppSecret = "6be875a7f04d19bdaeec4c0858886ac6"
- //彩虹星球
- //let kWeiXinAppKey = "wx163e76382d53654b"
- //let kWeiXinAppSecret = "1a915228304ea2e45eda9d2af5a64b84"
- //MARK: - 微博
- let kWeiboAppKey = "1960193914"
- let kWeiboAppSecret = "ad525c2e9eb07bb0bc43f998295cfe51"
- //彩虹星球
- //let kWeiboAppKey = "4123861024"
- //let kWeiboAppSecret = "79d36bfe6b75638ece14d5d852e785c4"
- public enum ShareType {
- case text
- case image
- case webPage
- }
- public var _entity : UMessageRegisterEntity?
- public class UMManager: NSObject {
-
- static let shared : UMManager = UMManager()
- /// 友盟初始化
- public func initUM(launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Void {
- //公共
- common()
- //推送
- push(launchOptions: launchOptions)
- //分享
- share()
- }
- }
- // MARK: 公共
- public extension UMManager {
- ///公共
- func common() -> Void {
- //将自动采集页面信息
- MobClick.setAutoPageEnabled(true)
- //开发者需要显式的调用此函数,日志系统才能工作
- UMCommonLogManager.setUp()
- //打开加密传输
- UMConfigure.setEncryptEnabled(true)
- //设置打开日志
- UMConfigure.setLogEnabled(true)
- //设置Key
- UMConfigure.initWithAppkey(kUMengAppKey, channel: "App Store")
-
- //开启Crash收集
- MobClick.setCrashReportEnabled(true)
- //默认为普通应用场景,目前还支持游戏统计场景
- MobClick.setScenarioType(eScenarioType.E_UM_NORMAL)
- //获得集成测试需要device_id
- let deice_id = UMConfigure.deviceIDForIntegration()
- if deice_id != nil {
- NXLLog("服务器端成功返回deviceID:\(deice_id!)");
- }else {
- NXLLog("服务器端还没有返回deviceID");
- }
- }
- }
- // MARK: 推送
- public extension UMManager {
- ///推送
- func push(launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Void {
- UMessage.openDebugMode(true)
- UMessage.setBadgeClear(true)//设置是否允许SDK自动清空角标
- UMessage.setWebViewClassString("UMWebViewController")
- // UMessage.addLaunch() //开屏图
- _entity = UMessageRegisterEntity.init()
- //type是对推送的几个参数的选择,可以选择一个或者多个。默认是三个全部打开,即:声音,弹窗,角标
- _entity?.types = Int(UInt8(UMessageAuthorizationOptions.badge.rawValue)|UInt8(UMessageAuthorizationOptions.alert.rawValue)|UInt8(UMessageAuthorizationOptions.sound.rawValue))
- if #available(iOS 10.0, *) {
- let action1_ios10 = UNNotificationAction(identifier: "action1_identifier", title: "打开应用", options: UNNotificationActionOptions.foreground)
- let action2_ios10 = UNNotificationAction(identifier: "action2_identifier", title: "忽略", options: UNNotificationActionOptions.foreground)
- let category1_ios10 = UNNotificationCategory(identifier: "category1", actions: [action1_ios10,action2_ios10], intentIdentifiers: [], options: UNNotificationCategoryOptions.customDismissAction)
- //UNNotificationCategoryOptionNone
- //UNNotificationCategoryOptionCustomDismissAction 清除通知被触发会走通知的代理方法
- //UNNotificationCategoryOptionAllowInCarPlay 适用于行车模式
- let categories = NSSet(objects: category1_ios10)
- _entity?.categories = (categories as! Set<AnyHashable>)
- UNUserNotificationCenter.current().delegate = self
- } else {
- let action1 = UIMutableUserNotificationAction.init()
- action1.identifier = "action1_identifier"
- action1.title = "打开应用"
- action1.activationMode = UIUserNotificationActivationMode.foreground;//当点击的时候启动程序
- let action2 = UIMutableUserNotificationAction.init()
- action2.identifier = "action2_identifier"
- action2.title = "忽略"
- action2.activationMode = UIUserNotificationActivationMode.background;//当点击的时候不启动程序,在后台处理
- action2.isAuthenticationRequired = true;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
- action2.isDestructive = true;
- let actionCategory1 = UIMutableUserNotificationCategory.init()
- actionCategory1.identifier = "category1"//这组动作的唯一标示
- actionCategory1.setActions([action1,action2], for: UIUserNotificationActionContext.default)
- let categories = NSSet(objects: actionCategory1)
- _entity?.categories = (categories as! Set<AnyHashable>)
- }
- //注册通知
- UMessage.registerForRemoteNotifications(launchOptions: launchOptions, entity: _entity) { (granted, error) in
- if granted {
- }else {
- }
- }
-
- // 启动接受通知跳转到指定页面
- receiveRemoteNotification(launchOptions: launchOptions)
- }
-
- /// 添加别名
- func addAlias() {
- UMessage.addAlias(UserModel.shared().getModel()?.mobile ?? "", type: "CHXQ_MOBILE", response: { (data, error) in })
- }
-
- /// 重置别名
- func setAlias() {
- UMessage.setAlias(UserModel.shared().getModel()?.mobile ?? "", type: "CHXQ_MOBILE", response: { (data, error) in })
- }
-
- /// 移除别名
- func removeAlias() {
- UMessage.removeAlias(UserModel.shared().getModel()?.mobile ?? "", type: "CHXQ_MOBILE", response: { (data, error) in })
- }
-
- }
- @available(iOS 10.0, *)
- // MARK: - UNUserNotificationCenterDelegate
- extension UMManager:UNUserNotificationCenterDelegate {
- public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
- let userInfo = notification.request.content.userInfo
-
- if (notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))! {
- UMessage.setAutoAlert(false)
- //应用处于前台时的远程推送接受
- // let json = JSON(userInfo)
- // RouterManager.shared.UMPush(pushModel: PushModel(JSONString: json.description) ?? PushModel())
- //必须加这句代码
- UMessage.didReceiveRemoteNotification(userInfo)
-
- }else {
- //应用处于前台时的本地推送接受
- }
- completionHandler(UNNotificationPresentationOptions(rawValue: UNNotificationPresentationOptions.sound.rawValue|UNNotificationPresentationOptions.alert.rawValue|UNNotificationPresentationOptions.badge.rawValue))
- }
-
- public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
- let userInfo = response.notification.request.content.userInfo
- if (response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))! {
- UMessage.setAutoAlert(false)
- //应用处于后台时的远程推送接受
- let json = JSON(userInfo)
- RouterManager.shared.UMPush(pushModel: PushModel(JSONString: json.description) ?? PushModel())
- //必须加这句代码
- UMessage.didReceiveRemoteNotification(userInfo)
-
- }else {
- //应用处于后台时的本地推送接受
- }
- }
-
-
- /// 启动跳转到指定页面
- func receiveRemoteNotification(launchOptions:[UIApplication.LaunchOptionsKey: Any]?) {
- let userInfo = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable : Any]
- if userInfo != nil {
- UMessage.setAutoAlert(false)
- let json = JSON(userInfo as Any)
- RouterManager.shared.UMPush(pushModel: PushModel(JSONString: json.description) ?? PushModel())
- //必须加这句代码
- UMessage.didReceiveRemoteNotification(userInfo)
- }
- }
- }
- // MARK: 分享
- public extension UMManager {
- /// 分享设置
- func share() -> Void {
- /*
- 设置微信的appKey和appSecret
- [微信平台从U-Share 4/5升级说明]http://dev.umeng.com/social/ios/%E8%BF%9B%E9%98%B6%E6%96%87%E6%A1%A3#1_1
- */
- UMSocialManager.default().setPlaform(UMSocialPlatformType.wechatSession, appKey: kWeiXinAppKey, appSecret: kWeiXinAppSecret, redirectURL: nil)
- /* 设置分享到QQ互联的appID
- * U-Share SDK为了兼容大部分平台命名,统一用appKey和appSecret进行参数设置,而QQ平台仅需将appID作为U-Share的appKey参数传进即可。
- 100424468.no permission of union id
- [QQ/QZone平台集成说明]http://dev.umeng.com/social/ios/%E8%BF%9B%E9%98%B6%E6%96%87%E6%A1%A3#1_3
- */
- UMSocialManager.default().setPlaform(UMSocialPlatformType.QQ, appKey: kQQAppKey, appSecret: kQQAppSecret, redirectURL: nil)
- /*
- 设置新浪的appKey和appSecret
- [新浪微博集成说明]http://dev.umeng.com/social/ios/%E8%BF%9B%E9%98%B6%E6%96%87%E6%A1%A3#1_2
- */
- UMSocialManager.default().setPlaform(UMSocialPlatformType.sina, appKey: kWeiboAppKey, appSecret: kWeiboAppSecret, redirectURL: "https://sns.whalecloud.com/sina2/callback")
- /*
- * 移除相应平台的分享,如微信收藏
- */
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.alipaySession)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.yixinSession)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.yixinTimeLine)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.laiWangSession)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.sms)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.email)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.renren)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.facebook)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.twitter)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.douban)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.kakaoTalk)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.pinterest)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.line)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.linkedin )
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.flickr)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.tumblr)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.instagram)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.whatsapp)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.dingDing)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.youDaoNote)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.everNote)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.googlePlus)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.pocket)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.dropBox)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.vKontakte)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.faceBookMessenger)
- UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.tim)
- }
- /// 分享面板
- ///
- /// - Parameters:
- /// - shareType: 平台
- /// - viewController: 控制器
- /// - text: 分享文本
- /// - thumbImage: 缩略图
- /// - shareImage: 分享图
- /// - title: 标题
- /// - descr: 内容描述
- /// - webpageUrl: 链接地址
- func UMSocialUI(shareType:ShareType, viewController:UIViewController,text:String,thumbImage:Any,shareImage:Any,title:String,descr:String,webpageUrl:String) -> Void {
- UMSocialUIManager.showShareMenuViewInWindow(platformSelectionBlock: {[weak self] platformType, userInfo in
- switch shareType {
- case .text:
- self?.shareText(to: platformType, viewController: viewController,text: text,completion: nil)
- break
- case .image:
- self?.shareImage(to: platformType, viewController: viewController,thumbImage: thumbImage,shareImage: shareImage,completion: nil)
- break
- case .webPage:
- self?.shareWebPage(to: platformType, viewController: viewController,title: title,descr: descr, thumbImage: thumbImage,webpageUrl: webpageUrl, completion: nil)
- break
- }
- })
- }
- /// 分享文本
- ///
- /// - Parameters:
- /// - platformType: 平台
- /// - viewController: 控制器
- /// - text: 分享文本
- func shareText(to platformType: UMSocialPlatformType,viewController:UIViewController,text:String,completion: (() -> Void)?) {
- //创建分享消息对象
- let messageObject = UMSocialMessageObject()
- //设置文本
- messageObject.text = text
- //调用分享接口
- isPlatformInstall(platformType: platformType, messageObject: messageObject, viewController: viewController, completion: completion)
- }
- /// 分享图片
- ///
- /// - Parameters:
- /// - platformType: 平台
- /// - viewController: 控制器
- /// - thumbImage: 缩略图
- /// - shareImage: 分享图
- func shareImage(to platformType: UMSocialPlatformType,viewController:UIViewController,thumbImage:Any,shareImage:Any,completion: (() -> Void)?) {
- //创建分享消息对象
- let messageObject = UMSocialMessageObject()
- //创建图片内容对象
- let shareObject = UMShareImageObject()
- //如果有缩略图,则设置缩略图
- shareObject.thumbImage = thumbImage
- shareObject.shareImage = shareImage
- //分享消息对象设置分享内容对象
- messageObject.shareObject = shareObject
- //调用分享接口
-
- isPlatformInstall(platformType: platformType, messageObject: messageObject, viewController: viewController, completion: completion)
- }
- /// 分享网页
- ///
- /// - Parameters:
- /// - platformType: 平台
- /// - viewController: 控制器
- /// - title: 标题
- /// - descr: 内容描述
- /// - thumbImage: 缩略图
- /// - webpageUrl: 链接地址
- func shareWebPage(to platformType: UMSocialPlatformType,viewController:UIViewController,title:String,descr:String,thumbImage:Any,webpageUrl:String,completion: (() -> Void)?) {
- //创建分享消息对象
- let messageObject = UMSocialMessageObject()
- //创建网页内容对象
- let shareObject = UMShareWebpageObject.shareObject(withTitle: title, descr: descr, thumImage: thumbImage)
- //设置网页地址
- shareObject!.webpageUrl = webpageUrl
- //分享消息对象设置分享内容对象
- messageObject.shareObject = shareObject
-
- isPlatformInstall(platformType: platformType, messageObject: messageObject, viewController: viewController, completion: completion)
- }
-
- private func isPlatformInstall(platformType:UMSocialPlatformType,messageObject:UMSocialMessageObject,viewController:Any,completion: (() -> Void)?) {
-
- if platformType == .sina {
- //调用分享接口
- share(platformType: platformType, messageObject: messageObject, viewController: viewController, completion: completion)
- } else if platformType == .wechatSession || platformType == .wechatTimeLine {
- if (UMSocialManager.default()?.isInstall(platformType))! {
- //调用分享接口
- share(platformType: platformType, messageObject: messageObject, viewController: viewController, completion: completion)
- }else {
- SwiftProgressHUD.shared().showText("微信未安装,请您先安装")
- }
- } else if platformType == .QQ {
- if (UMSocialManager.default()?.isInstall(platformType))! {
- share(platformType: platformType, messageObject: messageObject, viewController: viewController, completion: completion)
- }else {
- SwiftProgressHUD.shared().showText("QQ未安装,请您先安装")
- }
- }
-
- }
-
- private func share(platformType:UMSocialPlatformType,messageObject:UMSocialMessageObject,viewController:Any,completion: (() -> Void)?) {
- //调用分享接口
- UMSocialManager.default().share(to: platformType, messageObject: messageObject, currentViewController: viewController) { data, error in
- if error != nil {
- SwiftProgressHUD.shared().showText("分享失败")
- } else {
- SwiftProgressHUD.shared().showText("分享成功")
- completion?()
- }
- }
- }
- }
- // MARK: 第三方登录
- extension UMManager {
- /// 第三方登录
- ///
- /// - Parameter platformType: 平台
- func loginGetUserInfo(platformType: UMSocialPlatformType,currentViewController:UIViewController,callBack: @escaping (UMLoginModel) -> (Void)) {
-
- if !AlamofireReachabilityManager.shared.isNetworkConnect() {
- return
- }
- // let isInstall = UMSocialManager.default()?.isInstall(platformType)
- // if isInstall! { //验证
- // getUserInfo(platformType: platformType, callBack: callBack)
- // }else {
- // if platformType == .wechatSession {
- // SwiftProgressHUD.shared().showText("微信未安装\n请您安装微信程序")
- // }else{
- // getUserInfo(platformType: platformType, callBack: callBack)
- // }
- // }
- getUserInfo(platformType: platformType,currentViewController:currentViewController, callBack: callBack)
- }
- }
- func getUserInfo(platformType: UMSocialPlatformType,currentViewController:UIViewController,callBack: @escaping (UMLoginModel) -> (Void)) {
-
- UMSocialManager.default().getUserInfo(with: platformType, currentViewController: currentViewController) { result, error in
-
- if error != nil {
- SwiftProgressHUD.shared().showText("授权失败,请重新登录")
- } else {
-
- let resp = result as? UMSocialUserInfoResponse
-
- let UserModel = UMLoginModel()
- // 第三方登录数据(为空表示平台未提供)
- // 授权数据
- if let anUid = resp?.uid {
- NXLLog(" uid: \(anUid)")
- UserModel.union_id = anUid
- }
- if let anOpenid = resp?.openid {
- NXLLog(" openid: \(anOpenid)")
- UserModel.open_id = anOpenid
- }
-
- if let anUnionId = resp?.unionId {
- NXLLog(" unionId: \(anUnionId)")
- UserModel.union_id = anUnionId
- }
-
- // 用户数据
- if let aName = resp?.name {
- NXLLog(" name: \(aName)")
- UserModel.username = aName
- }
- if let anIconurl = resp?.iconurl {
- NXLLog(" iconurl: \(anIconurl)")
- UserModel.avatar = anIconurl
- }
- if let aGender = resp?.unionGender {
- NXLLog(" gender: \(aGender)")
- if aGender == "男" {
- UserModel.gender = 1
- }else if aGender == "女" {
- UserModel.gender = 2
- }
- }
- callBack(UserModel)
- }
- }
- }
|