UMManager.swift 17 KB

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