AppDelegate.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // AppDelegate.swift
  3. // SocketChat
  4. //
  5. // Created by Gabriel Theodoropoulos on 1/31/16.
  6. // Copyright © 2016 AppCoda. All rights reserved.
  7. //
  8. import UIKit
  9. import SocketIOClientSwift
  10. let SCREEN_WIDTH = UIScreen.mainScreen().bounds.size.width
  11. let SCREEN_HEIGHT = UIScreen.mainScreen().bounds.size.height
  12. @UIApplicationMain
  13. class AppDelegate: UIResponder, UIApplicationDelegate {
  14. var window: UIWindow?
  15. let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
  16. var socket: SocketIOClient?
  17. var tcpClient:TCPClient?
  18. func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  19. // Override point for customization after application launch.
  20. self.window = UIWindow(frame: CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT))
  21. //注册推送
  22. let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
  23. let pushNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
  24. application.registerUserNotificationSettings(pushNotificationSettings)
  25. application.registerForRemoteNotifications()
  26. //是否已登陆
  27. if(isLogin()){
  28. self.showRootViewContorller()
  29. }
  30. else{
  31. self.showLoginViewContorller()
  32. }
  33. //
  34. //关联SB
  35. return true
  36. }
  37. func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
  38. print("##############################")
  39. print("##############################")
  40. print("DEVICE TOKEN = \(deviceToken)")
  41. print("##############################")
  42. print("##############################")
  43. var mytoken = trimstring(deviceToken.description, trimchar: "<>")
  44. mytoken = trimstring(mytoken)
  45. print(mytoken)
  46. // NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
  47. // NSString *deviceToken2 = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
  48. // [userDefaults setObject:deviceToken2 forKey:USERDEFAULT_APP_TOKEN];
  49. // [userDefaults synchronize];
  50. //
  51. ManagerUserDefault().InsertUserDefault(mytoken, key: USERDEFAULT_APP_TOKEN)
  52. }
  53. func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
  54. print("##############################")
  55. print("##############################")
  56. print("Fail to register APNs ----ErrorMsg:\(error)")
  57. print("##############################")
  58. print("##############################")
  59. }
  60. func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
  61. print(userInfo)
  62. }
  63. func applicationWillResignActive(application: UIApplication) {
  64. // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  65. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
  66. }
  67. func applicationDidEnterBackground(application: UIApplication) {
  68. // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  69. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  70. // SocketIOManager.sharedInstance.closeConnection()
  71. }
  72. func applicationWillEnterForeground(application: UIApplication) {
  73. // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
  74. }
  75. func applicationDidBecomeActive(application: UIApplication) {
  76. // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  77. // SocketIOManager.sharedInstance.establishConnection()
  78. }
  79. func applicationWillTerminate(application: UIApplication) {
  80. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  81. }
  82. func showLoginViewContorller(){
  83. let loginvc:LoginViewController = self.mainStoryboard.instantiateViewControllerWithIdentifier("LoginView") as! LoginViewController
  84. self.window?.rootViewController = loginvc
  85. self.window?.makeKeyAndVisible()
  86. }
  87. func showRootViewContorller(){
  88. let tabbarController = UITabBarController()
  89. let RecentController = UINavigationController(rootViewController: RecentViewController1())
  90. let ContactController = UINavigationController(rootViewController: ContactViewController())
  91. let NoticeController = UINavigationController(rootViewController: NoticeViewController())
  92. let SettingController = UINavigationController(rootViewController: SettingViewController())
  93. tabbarController.addChildViewController(RecentController)
  94. tabbarController.addChildViewController(ContactController)
  95. tabbarController.addChildViewController(NoticeController)
  96. tabbarController.addChildViewController(SettingController)
  97. //tabbarController.viewControllers = [RecentController,ContactController,NoticeController,SettingController]
  98. let tabitem1 = UITabBarItem(title: "消息", image: UIImage(named: "msg_icon"), selectedImage: UIImage(named: "msg_icon_"))
  99. let tabitem2 = UITabBarItem(title: "联系人", image: UIImage(named: "contact_icon"), selectedImage: UIImage(named: "contact_icon_sel"))
  100. let tabitem3 = UITabBarItem(title: "通知", image: UIImage(named: "function_icon_sel"), selectedImage: UIImage(named: "function_icon_sel"))
  101. let tabitem4 = UITabBarItem(title: "设置", image: UIImage(named: "setting_icon"), selectedImage: UIImage(named: "setting_icon_sel"))
  102. RecentController.tabBarItem = tabitem1
  103. ContactController.tabBarItem = tabitem2
  104. SettingController.tabBarItem = tabitem3
  105. NoticeController.tabBarItem = tabitem4
  106. //var MyAlert:UIAlertController?
  107. //self.mainStoryboard.instantiateViewControllerWithIdentifier("RootView") as! RootViewController
  108. //self.window?.rootViewController = window.rootViewController).selectedViewController;
  109. self.window?.rootViewController = tabbarController
  110. self.window?.makeKeyAndVisible()
  111. //建立链接
  112. }
  113. }