<commit_before>//
//  AppDelegate.swift
//  mala-ios
//
//  Created by <NAME> on 11/6/15.
//  Copyright © 2015 Mala Online. All rights reserved.
//

import UIKit
import IQKeyboardManagerSwift



@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var deviceToken: NSData?
    var notRegisteredPush = true
    

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        // Setup Window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        let mainViewController = MainViewController()
        MalaMainViewController = mainViewController
        window?.rootViewController = mainViewController
        window?.makeKeyAndVisible()
        
        // 全局的外观自定义
        customAppearance()
        registerThirdParty()
        
        // 配置JPush
        #if DevDebug
            JPUSHService.setupWithOption(launchOptions, appKey: Mala_JPush_AppKey, channel: "AppStore", apsForProduction: false)
        #else
            JPUSHService.setupWithOption(launchOptions, appKey: Mala_JPush_AppKey, channel: "AppStore", apsForProduction: true)
        #endif
        
        let kUserNotificationBSA: UIUserNotificationType = [.Badge, .Sound, .Alert]
        JPUSHService.registerForRemoteNotificationTypes(kUserNotificationBSA.rawValue, categories: nil)
        
        if MalaUserDefaults.isLogined {
            
            // 记录启动通知类型
            if let
                notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? UILocalNotification,
                userInfo = notification.userInfo {
                    MalaRemoteNotificationHandler().handleRemoteNotification(userInfo)
            }
        }

        return true
    }

    
    // MARK: - Life Cycle
    func applicationWillResignActive(application: UIApplication) {
        
        println("Will Resign Active")
        
        // 发生支付行为跳回时，取消遮罩
        ThemeHUD.hideActivityIndicator()
        UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    }

    func applicationDidEnterBackground(application: UIApplication) {
        
        println("Did Enter Background")
        
        MalaIsForeground = false
    }

    func applicationWillEnterForeground(application: UIApplication) {
        
        println("Will Enter Foreground")
        
        MalaIsForeground = true
    }

    func applicationDidBecomeActive(application: UIApplication) {
        
        println("Did Become Active")
        
        application.applicationIconBadgeNumber = 0
    }

    func applicationWillTerminate(application: UIApplication) {
        
    }
    
    
    // MARK: - APNs
    func registerThirdPartyPushWithDeciveToken(deviceToken: NSData, pusherID: String) {
        
        JPUSHService.registerDeviceToken(deviceToken)
        JPUSHService.setTags(Set(["iOS"]), alias: pusherID, callbackSelector:nil, object: nil)
    }
    
    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        
        println("didRegisterForRemoteNotificationsWithDeviceToken - \(MalaUserDefaults.parentID.value)")
        
        if let parentID = MalaUserDefaults.parentID.value {
            if notRegisteredPush {
                notRegisteredPush = false
                registerThirdPartyPushWithDeciveToken(deviceToken, pusherID: String(parentID))
            }
        }
        
        // 纪录设备token，用于初次登录或注册有 pusherID 后，或“注销再登录”
        self.deviceToken = deviceToken
    }
    
    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        
        println("didReceiveRemoteNotification: - \(userInfo)")
        JPUSHService.handleRemoteNotification(userInfo)
        
        if MalaUserDefaults.isLogined {
            if MalaRemoteNotificationHandler().handleRemoteNotification(userInfo) {
                completionHandler(UIBackgroundFetchResult.NewData)
            }
        }
    }
    
    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
        
        println(String(format: "did Fail To Register For Remote Notifications With Error: %@", error))
    }
    
    
    // MARK: - openURL
    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
        
        // 微信,支付宝 回调
        let canHandleURL = Pingpp.handleOpenURL(url) { (result, error) -> Void in
            // 处理Ping++回调
            let handler = HandlePingppBehaviour()
            handler.handleResult(result, error: error, currentViewController: MalaPaymentController)
        }
        return canHandleURL
    }
    
    func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
        
        // 微信,支付宝 回调
        let canHandleURL = Pingpp.handleOpenURL(url) { (result, error) -> Void in
            // 处理Ping++回调
            let handler = HandlePingppBehaviour()
            handler.handleResult(result, error: error, currentViewController: MalaPaymentController)
        }
        return canHandleURL
    }
    
    
    // MARK: - SDK Configuration
    func registerThirdParty() {

        // Configure tracker from GoogleService-Info.plist.
        var configureError:NSError?
        GGLContext.sharedInstance().configureWithError(&configureError)
        assert(configureError == nil, "Error configuring Google services: \(configureError)")
        
        // Optional: configure GAI options.
        let gai = GAI.sharedInstance()
        gai.trackUncaughtExceptions = true  // report uncaught exceptions
        #if DevDebug
            gai.logger.logLevel = GAILogLevel.Verbose
        #endif
        
        
        
        // Ping++ - 开启DEBUG模式log
        Pingpp.setDebugMode(true)
        
        // IQKeyboardManager - 开启键盘自动管理
        IQKeyboardManager.sharedManager().enable = true
    }
        
    
    // MARK: - UI
    /// 设置公共外观样式
    private func customAppearance() {<commit_msg>IOS-298 disable google uncaught exceptions report<commit_after>//
//  AppDelegate.swift
//  mala-ios
//
//  Created by <NAME> on 11/6/15.
//  Copyright © 2015 Mala Online. All rights reserved.
//

import UIKit
import IQKeyboardManagerSwift



@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var deviceToken: NSData?
    var notRegisteredPush = true
    

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        // Setup Window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        let mainViewController = MainViewController()
        MalaMainViewController = mainViewController
        window?.rootViewController = mainViewController
        window?.makeKeyAndVisible()
        
        // 全局的外观自定义
        customAppearance()
        registerThirdParty()
        
        // 配置JPush
        #if DevDebug
            JPUSHService.setupWithOption(launchOptions, appKey: Mala_JPush_AppKey, channel: "AppStore", apsForProduction: false)
        #else
            JPUSHService.setupWithOption(launchOptions, appKey: Mala_JPush_AppKey, channel: "AppStore", apsForProduction: true)
        #endif
        
        let kUserNotificationBSA: UIUserNotificationType = [.Badge, .Sound, .Alert]
        JPUSHService.registerForRemoteNotificationTypes(kUserNotificationBSA.rawValue, categories: nil)
        
        if MalaUserDefaults.isLogined {
            
            // 记录启动通知类型
            if let
                notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? UILocalNotification,
                userInfo = notification.userInfo {
                    MalaRemoteNotificationHandler().handleRemoteNotification(userInfo)
            }
        }

        return true
    }

    
    // MARK: - Life Cycle
    func applicationWillResignActive(application: UIApplication) {
        
        println("Will Resign Active")
        
        // 发生支付行为跳回时，取消遮罩
        ThemeHUD.hideActivityIndicator()
        UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    }

    func applicationDidEnterBackground(application: UIApplication) {
        
        println("Did Enter Background")
        
        MalaIsForeground = false
    }

    func applicationWillEnterForeground(application: UIApplication) {
        
        println("Will Enter Foreground")
        
        MalaIsForeground = true
    }

    func applicationDidBecomeActive(application: UIApplication) {
        
        println("Did Become Active")
        
        application.applicationIconBadgeNumber = 0
    }

    func applicationWillTerminate(application: UIApplication) {
        
    }
    
    
    // MARK: - APNs
    func registerThirdPartyPushWithDeciveToken(deviceToken: NSData, pusherID: String) {
        
        JPUSHService.registerDeviceToken(deviceToken)
        JPUSHService.setTags(Set(["iOS"]), alias: pusherID, callbackSelector:nil, object: nil)
    }
    
    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        
        println("didRegisterForRemoteNotificationsWithDeviceToken - \(MalaUserDefaults.parentID.value)")
        
        if let parentID = MalaUserDefaults.parentID.value {
            if notRegisteredPush {
                notRegisteredPush = false
                registerThirdPartyPushWithDeciveToken(deviceToken, pusherID: String(parentID))
            }
        }
        
        // 纪录设备token，用于初次登录或注册有 pusherID 后，或“注销再登录”
        self.deviceToken = deviceToken
    }
    
    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        
        println("didReceiveRemoteNotification: - \(userInfo)")
        JPUSHService.handleRemoteNotification(userInfo)
        
        if MalaUserDefaults.isLogined {
            if MalaRemoteNotificationHandler().handleRemoteNotification(userInfo) {
                completionHandler(UIBackgroundFetchResult.NewData)
            }
        }
    }
    
    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
        
        println(String(format: "did Fail To Register For Remote Notifications With Error: %@", error))
    }
    
    
    // MARK: - openURL
    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
        
        // 微信,支付宝 回调
        let canHandleURL = Pingpp.handleOpenURL(url) { (result, error) -> Void in
            // 处理Ping++回调
            let handler = HandlePingppBehaviour()
            handler.handleResult(result, error: error, currentViewController: MalaPaymentController)
        }
        return canHandleURL
    }
    
    func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
        
        // 微信,支付宝 回调
        let canHandleURL = Pingpp.handleOpenURL(url) { (result, error) -> Void in
            // 处理Ping++回调
            let handler = HandlePingppBehaviour()
            handler.handleResult(result, error: error, currentViewController: MalaPaymentController)
        }
        return canHandleURL
    }
    
    
    // MARK: - SDK Configuration
    func registerThirdParty() {

        // Configure tracker from GoogleService-Info.plist.
        var configureError:NSError?
        GGLContext.sharedInstance().configureWithError(&configureError)
        assert(configureError == nil, "Error configuring Google services: \(configureError)")
        
        // Optional: configure GAI options.
        let gai = GAI.sharedInstance()
        // gai.trackUncaughtExceptions = true  // report uncaught exceptions
        #if DevDebug
            gai.logger.logLevel = GAILogLevel.Verbose
        #endif
        
        
        
        // Ping++ - 开启DEBUG模式log
        Pingpp.setDebugMode(true)
        
        // IQKeyboardManager - 开启键盘自动管理
        IQKeyboardManager.sharedManager().enable = true
    }
        
    
    // MARK: - UI
    /// 设置公共外观样式
    private func customAppearance() {
{'fraction_non_alphanumeric': 0.07888686276064379, 'fraction_numerical': 0.0019820819789106475, 'mean_word_length': 2.379957127545552, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3005722', 'n_tokens_mistral': 3535, 'n_tokens_neox': 3436, 'n_words': 661}