AppDelegate.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //
  2. // AppDelegate.swift
  3. // SolarBT
  4. //
  5. // Created by weclouds on 2019/1/23.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. /*
  9. //发布boundleid
  10. like@szshuori.com
  11. Sr26458295
  12. com.weclouds.srnebt
  13. 测试 boudleid
  14. com.weclouds.solarbt
  15. 从命令行创建一个新的仓库
  16. touch README.md
  17. git init
  18. git add README.md
  19. git commit -m "first commit"
  20. git remote add origin http://gogs.weclouds.xyz:3000/cxf/SolarBT_iOS.git
  21. git push -u origin master
  22. 从命令行推送已经创建的仓库
  23. git remote add origin http://gogs.weclouds.xyz:3000/cxf/SolarBT_iOS.git
  24. git push -u origin master
  25. */
  26. import UIKit
  27. import IQKeyboardManagerSwift
  28. import QuickLook
  29. import CoreLocation
  30. //import UMCommon
  31. @UIApplicationMain
  32. class AppDelegate: UIResponder, UIApplicationDelegate {
  33. let Appkey = "5d799f434ca35748e6000984"
  34. var window: UIWindow?
  35. var docURL :URL?
  36. var docVC : UIDocumentInteractionController?
  37. var blockRotation: Bool = false
  38. //处理微信回调
  39. // func onReq(_ req: BaseReq) {
  40. //// /** 错误码 */
  41. //// @property (nonatomic, assign) int errCode;
  42. //// /** 错误提示字符串 */
  43. //// @property (nonatomic, copy) NSString *errStr;
  44. //// /** 响应类型 */
  45. //// @property (nonatomic, assign) int type;
  46. //
  47. // log.debug("微信回调 = \(req)")
  48. // }
  49. func getdataPath() -> String{
  50. let str = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last!
  51. let urlPath = str.appending("Exception.txt")
  52. return urlPath
  53. }
  54. func setDefaultHandler() {
  55. NSSetUncaughtExceptionHandler { (exception) in
  56. let arr:NSArray = exception.callStackSymbols as NSArray
  57. let reason:String = exception.reason!
  58. let name:String = exception.name.rawValue
  59. let date:NSDate = NSDate()
  60. let timeFormatter = DateFormatter()
  61. timeFormatter.dateFormat = "YYYY/MM/dd hh:mm:ss SS"
  62. let strNowTime = timeFormatter.string(from: date as Date) as String
  63. let url:String = String.init(format: "========异常错误报告========\ntime:%@\nname:%@\nreason:\n%@\ncallStackSymbols:\n%@",strNowTime,name,reason,arr.componentsJoined(by: "\n"))
  64. let documentpath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last!
  65. let path = documentpath.appending("Exception.txt")
  66. do{
  67. try
  68. url.write(toFile: path, atomically: true, encoding: String.Encoding.utf8)
  69. }catch{}
  70. }
  71. }
  72. //上传服务器
  73. func uploadToSever() {
  74. //1、判断本地是否有保存内容,如果有,保存,如果没有,则不保存
  75. //第一次不上传内容
  76. }
  77. func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
  78. if self.blockRotation{
  79. return UIInterfaceOrientationMask.landscapeRight
  80. } else {
  81. return UIInterfaceOrientationMask.portrait
  82. }
  83. }
  84. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  85. // Override point for customization after application launch.
  86. //请求权限
  87. //注册微信
  88. // WXApi.registerApp("wx8daad789692c92e8", universalLink: <#String#>)
  89. IQKeyboardManager.shared.enable = true
  90. //注册bug统计
  91. // UMConfigure.initWithAppkey(Appkey, channel: "")
  92. let userdef = UserDefaults.standard
  93. let langueage = userdef.object(forKey: "SOLARLANGUAGE") as? String
  94. if langueage == nil || langueage == "0" {
  95. DAConfig.userLanguage = nil
  96. }else{
  97. //设置当前语言 --
  98. let language = DAConfig.currentLanguage()
  99. DAConfig.userLanguage = language
  100. }
  101. //1、创建window
  102. window = UIWindow(frame: UIScreen.main.bounds)
  103. window?.backgroundColor = UIColor.white
  104. //2、创建根控制器
  105. let homeVC = SBTHomeVCtr()
  106. let nav = RootNavigationController(rootViewController: homeVC)
  107. window?.rootViewController = nav
  108. //显示界面
  109. window?.makeKeyAndVisible()
  110. return true
  111. }
  112. func applicationWillResignActive(_ application: UIApplication) {
  113. // 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.
  114. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
  115. }
  116. func applicationDidEnterBackground(_ application: UIApplication) {
  117. // 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.
  118. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  119. }
  120. func applicationWillEnterForeground(_ application: UIApplication) {
  121. // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
  122. }
  123. func applicationDidBecomeActive(_ application: UIApplication) {
  124. // 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.
  125. }
  126. func applicationWillTerminate(_ application: UIApplication) {
  127. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  128. }
  129. func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
  130. print("打开了回掉信息 \(url)")
  131. docURL = url
  132. if url.scheme == "file" {
  133. docVC = UIDocumentInteractionController(url: url)
  134. docVC?.delegate = self
  135. docVC?.presentPreview(animated: true)
  136. }
  137. return true
  138. }
  139. //获取当前屏幕显示的viewcontroller
  140. func getCurrentVC() -> UIViewController {
  141. let rootViewController = UIApplication.shared.keyWindow?.rootViewController
  142. let currentVC = getCurrentVCFrom(rootVC: rootViewController)
  143. return currentVC
  144. }
  145. func getCurrentVCFrom( rootVC : UIViewController?) -> UIViewController {
  146. var currentVC : UIViewController?
  147. var rootVC = rootVC
  148. if (rootVC!.presentedViewController != nil) {
  149. //视图是被presented出来的d
  150. rootVC = rootVC?.presentedViewController
  151. }
  152. if (rootVC?.isKind(of: UITabBarController.self))! {
  153. //根试图为UITabBarController
  154. let vc = rootVC as! UITabBarController
  155. currentVC = getCurrentVCFrom(rootVC: vc.selectedViewController )
  156. }else if((rootVC?.isKind(of: UINavigationController.self))!){
  157. let vc = rootVC as! UINavigationController
  158. currentVC = getCurrentVCFrom(rootVC: vc.visibleViewController)
  159. }else{
  160. //根试图为非导航类
  161. currentVC = rootVC
  162. }
  163. return currentVC!
  164. }
  165. }
  166. extension AppDelegate: UIDocumentInteractionControllerDelegate{
  167. //预览
  168. func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
  169. // 返回当前控制器
  170. print("ForPreview")
  171. return getCurrentVC()
  172. }
  173. func documentInteractionControllerWillBeginPreview(_ controller: UIDocumentInteractionController) {
  174. print("WillBeginPreview")
  175. }
  176. //完成预览 点击了Done
  177. func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) {
  178. print("DidEndPreview")
  179. // 在完成预览之后,将文件保存到沙盒之中,然后从沙盒之中读取x会爱
  180. print("name -- \(controller.name) \n uti \(controller.uti) \n url -\(controller.url) \n \(controller.icons), \n \(controller.annotation) ")
  181. let vc = getCurrentVC()
  182. let home = NSHomeDirectory() // 获取沙盒根路径
  183. let fileName = controller.name as! String
  184. let documentsDirectory = home + "/Documents/Inbox/"
  185. let filePath = documentsDirectory.appending(fileName)
  186. log.debug("filePath : \(filePath)")
  187. let fileManager = FileManager.default
  188. if fileManager.fileExists(atPath: filePath) {
  189. log.debug("文件存在")
  190. //需要确认文件编码 使用的是中文GBK
  191. let encolde = CFStringConvertEncodingToNSStringEncoding(UInt32(CFStringEncodings.GB_18030_2000.rawValue))
  192. // let text = try! String(contentsOfFile: filePath, encoding: String.Encoding(rawValue: encolde))
  193. let text = try! String(contentsOfFile: filePath, encoding: String.Encoding.utf8)
  194. log.debug("文件内容 : \(text)")
  195. vc.view.makeToast("文件保存成功")
  196. // vc.paramContent = text
  197. }else{
  198. print("文件不存在")
  199. }
  200. /**
  201. 实际在此时,该文档已经复制到“readerApp”的“document/inbox”文件夹下。
  202. 我们需要将该文档移动到其他位置,因为每次其他应用调用“readerApp”打开该文档时,都会在inbox文件夹下复制一份文件(不考虑该文件是否已经存在),如果多次使用,那么inbox将会占用大量的存储空间。
  203. */
  204. // try! FileManager.default.removeItem(atPath: filePath)
  205. }
  206. }