func application()

in Sasquatch/SasquatchSwift/AppDelegate.swift [43:160]


  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    Crashes.delegate = self
#if canImport(AppCenterDistribute)
    Distribute.delegate = self
#endif
    AppCenter.logLevel = LogLevel.verbose

    // Set max storage size.
    let storageMaxSize = UserDefaults.standard.object(forKey: kMSStorageMaxSizeKey) as? Int
    if storageMaxSize != nil {
      AppCenter.setMaxStorageSize(storageMaxSize!, completionHandler: { success in
        DispatchQueue.main.async {
          if success {
            let realSize = Int64(ceil(Double(storageMaxSize!) / Double(kMSStoragePageSize))) * Int64(kMSStoragePageSize)
            UserDefaults.standard.set(realSize, forKey: kMSStorageMaxSizeKey)
          } else {

            // Remove invalid value.
            UserDefaults.standard.removeObject(forKey: kMSStorageMaxSizeKey)

            // Show alert.
            let alertController = UIAlertController(title: "Warning!",
                                                    message: "The maximum size of the internal storage could not be set.",
                                                    preferredStyle: .alert)
            alertController.addAction(UIAlertAction(title: "OK", style: .default))
            self.topMostViewController()?.present(alertController, animated: true)
          }
        }
      })
    }

    let logUrl = UserDefaults.standard.string(forKey: kMSLogUrl)
    if logUrl != nil {
      AppCenter.logUrl = logUrl
    }
    
    // Set manual session tracker before App Center start.
    if UserDefaults.standard.bool(forKey: kMSManualSessionTracker) {
      Analytics.enableManualSessionTracker()
    }
#if canImport(AppCenterDistribute)
    if let updateTrackValue = UserDefaults.standard.value(forKey: kMSUpdateTrackKey) as? Int,
       let updateTrack = UpdateTrack(rawValue: updateTrackValue) {
        Distribute.updateTrack = updateTrack
    }
    if UserDefaults.standard.bool(forKey: kSASAutomaticCheckForUpdateDisabledKey) {
        Distribute.disableAutomaticCheckForUpdate()
    }
#endif

    // Start App Center SDK.
    var services = [Analytics.self, Crashes.self]
#if canImport(AppCenterDistribute)
    services.append(Distribute.self)
#endif
    let appSecret = UserDefaults.standard.string(forKey: kMSAppSecret) ?? kMSSwiftCombinedAppSecret
    let startTarget = StartupMode(rawValue: UserDefaults.standard.integer(forKey: kMSStartTargetKey))!
    let latencyTimeValue = UserDefaults.standard.integer(forKey: kMSTransmissionIterval);
    Analytics.transmissionInterval = UInt(latencyTimeValue);
    switch startTarget {
    case .APPCENTER:
      AppCenter.start(withAppSecret: appSecret, services: services)
      break
    case .ONECOLLECTOR:
      AppCenter.start(withAppSecret: "target=\(kMSSwiftTargetToken)", services: services)
      break
    case .BOTH:
      AppCenter.start(withAppSecret: "\(appSecret);target=\(kMSSwiftTargetToken)", services: services)
      break
    case .NONE:
      AppCenter.start(services: services)
      break
    case .SKIP:
      break
    }
    
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    locationManager.requestWhenInUseAuthorization()

    // Set user id.
    let userId = UserDefaults.standard.string(forKey: kMSUserIdKey)
    if userId != nil {
      AppCenter.userId = userId;
    }

    // Crashes Delegate.
    Crashes.userConfirmationHandler = ({ (errorReports: [ErrorReport]) in

      // Show a dialog to the user where they can choose if they want to update.
      let alertController = UIAlertController(title: "Sorry about that!",
              message: "Do you want to send an anonymous crash report so we can fix the issue?",
              preferredStyle: .alert)

      // Add a "Don't send"-Button and call the notifyWithUserConfirmation-callback with UserConfirmation.dontSend
      alertController.addAction(UIAlertAction(title: "Don't send", style: .cancel) { _ in
        Crashes.notify(with: .dontSend)
      })

      // Add a "Send"-Button and call the notifyWithUserConfirmation-callback with UserConfirmation.send
      alertController.addAction(UIAlertAction(title: "Send", style: .default) { _ in
        Crashes.notify(with: .send)
      })

      // Add a "Always send"-Button and call the notifyWithUserConfirmation-callback with UserConfirmation.always
      alertController.addAction(UIAlertAction(title: "Always send", style: .default) { _ in
        Crashes.notify(with: .always)
      })

      // Show the alert controller.
      self.topMostViewController()?.present(alertController, animated: true)

      return true
    })

    setAppCenterDelegate()
    return true
  }