UMManager.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. //
  2. // UMManager.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/3/7.
  6. // Copyright © 2019 南鑫林. All rights reserved.
  7. //
  8. import UIKit
  9. import SwiftyJSON
  10. //MARK: - UMeng
  11. let kUMengAppKey = "5d4d38ce0cafb2131c00041c"
  12. let kUMengAppSecret = "mllaspth0pdgnhdm02uvkppby3isxxex"
  13. //彩虹星球
  14. //let kUMengAppKey = "5c984a7f0cafb2332300000f"
  15. //let kUMengAppSecret = "lpx7kstlnlrxmimo5gfyzvtscrtguhw8"
  16. //MARK: - QQ跟安卓用同一个
  17. let kQQAppKey = "101742987"
  18. let kQQAppSecret = "664edcf45543f1958b46f914c03b3ed0"
  19. //彩虹星球
  20. //let kQQAppKey = "101565722"
  21. //let kQQAppSecret = "dfe6f96762ed0dbc3ad52dc06d0eda9b"
  22. //MARK: - 微信
  23. let kWeiXinAppKey = "wx0aed0c936870ad29"
  24. let kWeiXinAppSecret = "6be875a7f04d19bdaeec4c0858886ac6"
  25. //彩虹星球
  26. //let kWeiXinAppKey = "wx163e76382d53654b"
  27. //let kWeiXinAppSecret = "1a915228304ea2e45eda9d2af5a64b84"
  28. //MARK: - 微博
  29. let kWeiboAppKey = "1960193914"
  30. let kWeiboAppSecret = "ad525c2e9eb07bb0bc43f998295cfe51"
  31. //彩虹星球
  32. //let kWeiboAppKey = "4123861024"
  33. //let kWeiboAppSecret = "79d36bfe6b75638ece14d5d852e785c4"
  34. public enum ShareType {
  35. case text
  36. case image
  37. case webPage
  38. }
  39. public var _entity : UMessageRegisterEntity?
  40. public class UMManager: NSObject {
  41. static let shared : UMManager = UMManager()
  42. /// 友盟初始化
  43. public func initUM(launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Void {
  44. //公共
  45. common()
  46. //推送
  47. push(launchOptions: launchOptions)
  48. //分享
  49. share()
  50. }
  51. }
  52. // MARK: 公共
  53. public extension UMManager {
  54. ///公共
  55. func common() -> Void {
  56. //将自动采集页面信息
  57. MobClick.setAutoPageEnabled(true)
  58. //开发者需要显式的调用此函数,日志系统才能工作
  59. UMCommonLogManager.setUp()
  60. //打开加密传输
  61. UMConfigure.setEncryptEnabled(true)
  62. //设置打开日志
  63. UMConfigure.setLogEnabled(true)
  64. //设置Key
  65. UMConfigure.initWithAppkey(kUMengAppKey, channel: "App Store")
  66. //开启Crash收集
  67. MobClick.setCrashReportEnabled(true)
  68. //默认为普通应用场景,目前还支持游戏统计场景
  69. MobClick.setScenarioType(eScenarioType.E_UM_NORMAL)
  70. //获得集成测试需要device_id
  71. let deice_id = UMConfigure.deviceIDForIntegration()
  72. if deice_id != nil {
  73. NXLLog("服务器端成功返回deviceID:\(deice_id!)");
  74. }else {
  75. NXLLog("服务器端还没有返回deviceID");
  76. }
  77. }
  78. }
  79. // MARK: 推送
  80. public extension UMManager {
  81. ///推送
  82. func push(launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Void {
  83. UMessage.openDebugMode(true)
  84. UMessage.setBadgeClear(true)//设置是否允许SDK自动清空角标
  85. UMessage.setWebViewClassString("UMWebViewController")
  86. // UMessage.addLaunch() //开屏图
  87. _entity = UMessageRegisterEntity.init()
  88. //type是对推送的几个参数的选择,可以选择一个或者多个。默认是三个全部打开,即:声音,弹窗,角标
  89. _entity?.types = Int(UInt8(UMessageAuthorizationOptions.badge.rawValue)|UInt8(UMessageAuthorizationOptions.alert.rawValue)|UInt8(UMessageAuthorizationOptions.sound.rawValue))
  90. if #available(iOS 10.0, *) {
  91. let action1_ios10 = UNNotificationAction(identifier: "action1_identifier", title: "打开应用", options: UNNotificationActionOptions.foreground)
  92. let action2_ios10 = UNNotificationAction(identifier: "action2_identifier", title: "忽略", options: UNNotificationActionOptions.foreground)
  93. let category1_ios10 = UNNotificationCategory(identifier: "category1", actions: [action1_ios10,action2_ios10], intentIdentifiers: [], options: UNNotificationCategoryOptions.customDismissAction)
  94. //UNNotificationCategoryOptionNone
  95. //UNNotificationCategoryOptionCustomDismissAction 清除通知被触发会走通知的代理方法
  96. //UNNotificationCategoryOptionAllowInCarPlay 适用于行车模式
  97. let categories = NSSet(objects: category1_ios10)
  98. _entity?.categories = (categories as! Set<AnyHashable>)
  99. UNUserNotificationCenter.current().delegate = self
  100. } else {
  101. let action1 = UIMutableUserNotificationAction.init()
  102. action1.identifier = "action1_identifier"
  103. action1.title = "打开应用"
  104. action1.activationMode = UIUserNotificationActivationMode.foreground;//当点击的时候启动程序
  105. let action2 = UIMutableUserNotificationAction.init()
  106. action2.identifier = "action2_identifier"
  107. action2.title = "忽略"
  108. action2.activationMode = UIUserNotificationActivationMode.background;//当点击的时候不启动程序,在后台处理
  109. action2.isAuthenticationRequired = true;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
  110. action2.isDestructive = true;
  111. let actionCategory1 = UIMutableUserNotificationCategory.init()
  112. actionCategory1.identifier = "category1"//这组动作的唯一标示
  113. actionCategory1.setActions([action1,action2], for: UIUserNotificationActionContext.default)
  114. let categories = NSSet(objects: actionCategory1)
  115. _entity?.categories = (categories as! Set<AnyHashable>)
  116. }
  117. //注册通知
  118. UMessage.registerForRemoteNotifications(launchOptions: launchOptions, entity: _entity) { (granted, error) in
  119. if granted {
  120. }else {
  121. }
  122. }
  123. // 启动接受通知跳转到指定页面
  124. receiveRemoteNotification(launchOptions: launchOptions)
  125. }
  126. /// 添加别名
  127. func addAlias() {
  128. UMessage.addAlias(UserModel.shared().getModel()?.mobile ?? "", type: "CHXQ_MOBILE", response: { (data, error) in })
  129. }
  130. /// 重置别名
  131. func setAlias() {
  132. UMessage.setAlias(UserModel.shared().getModel()?.mobile ?? "", type: "CHXQ_MOBILE", response: { (data, error) in })
  133. }
  134. /// 移除别名
  135. func removeAlias() {
  136. UMessage.removeAlias(UserModel.shared().getModel()?.mobile ?? "", type: "CHXQ_MOBILE", response: { (data, error) in })
  137. }
  138. }
  139. @available(iOS 10.0, *)
  140. // MARK: - UNUserNotificationCenterDelegate
  141. extension UMManager:UNUserNotificationCenterDelegate {
  142. public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  143. let userInfo = notification.request.content.userInfo
  144. if (notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))! {
  145. UMessage.setAutoAlert(false)
  146. //应用处于前台时的远程推送接受
  147. // let json = JSON(userInfo)
  148. // RouterManager.shared.UMPush(pushModel: PushModel(JSONString: json.description) ?? PushModel())
  149. //必须加这句代码
  150. UMessage.didReceiveRemoteNotification(userInfo)
  151. }else {
  152. //应用处于前台时的本地推送接受
  153. }
  154. completionHandler(UNNotificationPresentationOptions(rawValue: UNNotificationPresentationOptions.sound.rawValue|UNNotificationPresentationOptions.alert.rawValue|UNNotificationPresentationOptions.badge.rawValue))
  155. }
  156. public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
  157. let userInfo = response.notification.request.content.userInfo
  158. if (response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))! {
  159. UMessage.setAutoAlert(false)
  160. //应用处于后台时的远程推送接受
  161. let json = JSON(userInfo)
  162. RouterManager.shared.UMPush(pushModel: PushModel(JSONString: json.description) ?? PushModel())
  163. //必须加这句代码
  164. UMessage.didReceiveRemoteNotification(userInfo)
  165. }else {
  166. //应用处于后台时的本地推送接受
  167. }
  168. }
  169. /// 启动跳转到指定页面
  170. func receiveRemoteNotification(launchOptions:[UIApplication.LaunchOptionsKey: Any]?) {
  171. let userInfo = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable : Any]
  172. if userInfo != nil {
  173. UMessage.setAutoAlert(false)
  174. let json = JSON(userInfo as Any)
  175. RouterManager.shared.UMPush(pushModel: PushModel(JSONString: json.description) ?? PushModel())
  176. //必须加这句代码
  177. UMessage.didReceiveRemoteNotification(userInfo)
  178. }
  179. }
  180. }
  181. // MARK: 分享
  182. public extension UMManager {
  183. /// 分享设置
  184. func share() -> Void {
  185. /*
  186. 设置微信的appKey和appSecret
  187. [微信平台从U-Share 4/5升级说明]http://dev.umeng.com/social/ios/%E8%BF%9B%E9%98%B6%E6%96%87%E6%A1%A3#1_1
  188. */
  189. UMSocialManager.default().setPlaform(UMSocialPlatformType.wechatSession, appKey: kWeiXinAppKey, appSecret: kWeiXinAppSecret, redirectURL: nil)
  190. /* 设置分享到QQ互联的appID
  191. * U-Share SDK为了兼容大部分平台命名,统一用appKey和appSecret进行参数设置,而QQ平台仅需将appID作为U-Share的appKey参数传进即可。
  192. 100424468.no permission of union id
  193. [QQ/QZone平台集成说明]http://dev.umeng.com/social/ios/%E8%BF%9B%E9%98%B6%E6%96%87%E6%A1%A3#1_3
  194. */
  195. UMSocialManager.default().setPlaform(UMSocialPlatformType.QQ, appKey: kQQAppKey, appSecret: kQQAppSecret, redirectURL: nil)
  196. /*
  197. 设置新浪的appKey和appSecret
  198. [新浪微博集成说明]http://dev.umeng.com/social/ios/%E8%BF%9B%E9%98%B6%E6%96%87%E6%A1%A3#1_2
  199. */
  200. UMSocialManager.default().setPlaform(UMSocialPlatformType.sina, appKey: kWeiboAppKey, appSecret: kWeiboAppSecret, redirectURL: "https://sns.whalecloud.com/sina2/callback")
  201. /*
  202. * 移除相应平台的分享,如微信收藏
  203. */
  204. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.alipaySession)
  205. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.yixinSession)
  206. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.yixinTimeLine)
  207. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.laiWangSession)
  208. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.sms)
  209. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.email)
  210. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.renren)
  211. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.facebook)
  212. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.twitter)
  213. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.douban)
  214. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.kakaoTalk)
  215. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.pinterest)
  216. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.line)
  217. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.linkedin )
  218. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.flickr)
  219. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.tumblr)
  220. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.instagram)
  221. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.whatsapp)
  222. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.dingDing)
  223. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.youDaoNote)
  224. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.everNote)
  225. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.googlePlus)
  226. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.pocket)
  227. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.dropBox)
  228. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.vKontakte)
  229. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.faceBookMessenger)
  230. UMSocialManager.default()?.removePlatformProvider(with: UMSocialPlatformType.tim)
  231. }
  232. /// 分享面板
  233. ///
  234. /// - Parameters:
  235. /// - shareType: 平台
  236. /// - viewController: 控制器
  237. /// - text: 分享文本
  238. /// - thumbImage: 缩略图
  239. /// - shareImage: 分享图
  240. /// - title: 标题
  241. /// - descr: 内容描述
  242. /// - webpageUrl: 链接地址
  243. func UMSocialUI(shareType:ShareType, viewController:UIViewController,text:String,thumbImage:Any,shareImage:Any,title:String,descr:String,webpageUrl:String) -> Void {
  244. UMSocialUIManager.showShareMenuViewInWindow(platformSelectionBlock: {[weak self] platformType, userInfo in
  245. switch shareType {
  246. case .text:
  247. self?.shareText(to: platformType, viewController: viewController,text: text,completion: nil)
  248. break
  249. case .image:
  250. self?.shareImage(to: platformType, viewController: viewController,thumbImage: thumbImage,shareImage: shareImage,completion: nil)
  251. break
  252. case .webPage:
  253. self?.shareWebPage(to: platformType, viewController: viewController,title: title,descr: descr, thumbImage: thumbImage,webpageUrl: webpageUrl, completion: nil)
  254. break
  255. }
  256. })
  257. }
  258. /// 分享文本
  259. ///
  260. /// - Parameters:
  261. /// - platformType: 平台
  262. /// - viewController: 控制器
  263. /// - text: 分享文本
  264. func shareText(to platformType: UMSocialPlatformType,viewController:UIViewController,text:String,completion: (() -> Void)?) {
  265. //创建分享消息对象
  266. let messageObject = UMSocialMessageObject()
  267. //设置文本
  268. messageObject.text = text
  269. //调用分享接口
  270. isPlatformInstall(platformType: platformType, messageObject: messageObject, viewController: viewController, completion: completion)
  271. }
  272. /// 分享图片
  273. ///
  274. /// - Parameters:
  275. /// - platformType: 平台
  276. /// - viewController: 控制器
  277. /// - thumbImage: 缩略图
  278. /// - shareImage: 分享图
  279. func shareImage(to platformType: UMSocialPlatformType,viewController:UIViewController,thumbImage:Any,shareImage:Any,completion: (() -> Void)?) {
  280. //创建分享消息对象
  281. let messageObject = UMSocialMessageObject()
  282. //创建图片内容对象
  283. let shareObject = UMShareImageObject()
  284. //如果有缩略图,则设置缩略图
  285. shareObject.thumbImage = thumbImage
  286. shareObject.shareImage = shareImage
  287. //分享消息对象设置分享内容对象
  288. messageObject.shareObject = shareObject
  289. //调用分享接口
  290. isPlatformInstall(platformType: platformType, messageObject: messageObject, viewController: viewController, completion: completion)
  291. }
  292. /// 分享网页
  293. ///
  294. /// - Parameters:
  295. /// - platformType: 平台
  296. /// - viewController: 控制器
  297. /// - title: 标题
  298. /// - descr: 内容描述
  299. /// - thumbImage: 缩略图
  300. /// - webpageUrl: 链接地址
  301. func shareWebPage(to platformType: UMSocialPlatformType,viewController:UIViewController,title:String,descr:String,thumbImage:Any,webpageUrl:String,completion: (() -> Void)?) {
  302. //创建分享消息对象
  303. let messageObject = UMSocialMessageObject()
  304. //创建网页内容对象
  305. let shareObject = UMShareWebpageObject.shareObject(withTitle: title, descr: descr, thumImage: thumbImage)
  306. //设置网页地址
  307. shareObject!.webpageUrl = webpageUrl
  308. //分享消息对象设置分享内容对象
  309. messageObject.shareObject = shareObject
  310. isPlatformInstall(platformType: platformType, messageObject: messageObject, viewController: viewController, completion: completion)
  311. }
  312. private func isPlatformInstall(platformType:UMSocialPlatformType,messageObject:UMSocialMessageObject,viewController:Any,completion: (() -> Void)?) {
  313. if platformType == .sina {
  314. //调用分享接口
  315. share(platformType: platformType, messageObject: messageObject, viewController: viewController, completion: completion)
  316. } else if platformType == .wechatSession || platformType == .wechatTimeLine {
  317. if (UMSocialManager.default()?.isInstall(platformType))! {
  318. //调用分享接口
  319. share(platformType: platformType, messageObject: messageObject, viewController: viewController, completion: completion)
  320. }else {
  321. SwiftProgressHUD.shared().showText("微信未安装,请您先安装")
  322. }
  323. } else if platformType == .QQ {
  324. if (UMSocialManager.default()?.isInstall(platformType))! {
  325. share(platformType: platformType, messageObject: messageObject, viewController: viewController, completion: completion)
  326. }else {
  327. SwiftProgressHUD.shared().showText("QQ未安装,请您先安装")
  328. }
  329. }
  330. }
  331. private func share(platformType:UMSocialPlatformType,messageObject:UMSocialMessageObject,viewController:Any,completion: (() -> Void)?) {
  332. //调用分享接口
  333. UMSocialManager.default().share(to: platformType, messageObject: messageObject, currentViewController: viewController) { data, error in
  334. if error != nil {
  335. SwiftProgressHUD.shared().showText("分享失败")
  336. } else {
  337. SwiftProgressHUD.shared().showText("分享成功")
  338. completion?()
  339. }
  340. }
  341. }
  342. }
  343. // MARK: 第三方登录
  344. extension UMManager {
  345. /// 第三方登录
  346. ///
  347. /// - Parameter platformType: 平台
  348. func loginGetUserInfo(platformType: UMSocialPlatformType,currentViewController:UIViewController,callBack: @escaping (UMLoginModel) -> (Void)) {
  349. if !AlamofireReachabilityManager.shared.isNetworkConnect() {
  350. return
  351. }
  352. // let isInstall = UMSocialManager.default()?.isInstall(platformType)
  353. // if isInstall! { //验证
  354. // getUserInfo(platformType: platformType, callBack: callBack)
  355. // }else {
  356. // if platformType == .wechatSession {
  357. // SwiftProgressHUD.shared().showText("微信未安装\n请您安装微信程序")
  358. // }else{
  359. // getUserInfo(platformType: platformType, callBack: callBack)
  360. // }
  361. // }
  362. getUserInfo(platformType: platformType,currentViewController:currentViewController, callBack: callBack)
  363. }
  364. }
  365. func getUserInfo(platformType: UMSocialPlatformType,currentViewController:UIViewController,callBack: @escaping (UMLoginModel) -> (Void)) {
  366. UMSocialManager.default().getUserInfo(with: platformType, currentViewController: currentViewController) { result, error in
  367. if error != nil {
  368. SwiftProgressHUD.shared().showText("授权失败,请重新登录")
  369. } else {
  370. let resp = result as? UMSocialUserInfoResponse
  371. let UserModel = UMLoginModel()
  372. // 第三方登录数据(为空表示平台未提供)
  373. // 授权数据
  374. if let anUid = resp?.uid {
  375. NXLLog(" uid: \(anUid)")
  376. UserModel.union_id = anUid
  377. }
  378. if let anOpenid = resp?.openid {
  379. NXLLog(" openid: \(anOpenid)")
  380. UserModel.open_id = anOpenid
  381. }
  382. if let anUnionId = resp?.unionId {
  383. NXLLog(" unionId: \(anUnionId)")
  384. UserModel.union_id = anUnionId
  385. }
  386. // 用户数据
  387. if let aName = resp?.name {
  388. NXLLog(" name: \(aName)")
  389. UserModel.username = aName
  390. }
  391. if let anIconurl = resp?.iconurl {
  392. NXLLog(" iconurl: \(anIconurl)")
  393. UserModel.avatar = anIconurl
  394. }
  395. if let aGender = resp?.unionGender {
  396. NXLLog(" gender: \(aGender)")
  397. if aGender == "男" {
  398. UserModel.gender = 1
  399. }else if aGender == "女" {
  400. UserModel.gender = 2
  401. }
  402. }
  403. callBack(UserModel)
  404. }
  405. }
  406. }