|
| 1 | +// |
| 2 | +// NetBlockerFlushPolicy.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Brandon Sneed on 4/26/23. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +// NOTE: You can see this task in use in the MacExample application. |
| 11 | + |
| 12 | +// MIT License |
| 13 | +// |
| 14 | +// Copyright (c) 2023 Segment |
| 15 | +// |
| 16 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 17 | +// of this software and associated documentation files (the "Software"), to deal |
| 18 | +// in the Software without restriction, including without limitation the rights |
| 19 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 20 | +// copies of the Software, and to permit persons to whom the Software is |
| 21 | +// furnished to do so, subject to the following conditions: |
| 22 | +// |
| 23 | +// The above copyright notice and this permission notice shall be included in all |
| 24 | +// copies or substantial portions of the Software. |
| 25 | +// |
| 26 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 27 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 28 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 29 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 30 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 31 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 32 | +// SOFTWARE. |
| 33 | + |
| 34 | +import Foundation |
| 35 | +import Segment |
| 36 | + |
| 37 | +public class NetBlockerFlushPolicy: FlushPolicy { |
| 38 | + public var type = PluginType.utility |
| 39 | + public weak var analytics: Segment.Analytics? |
| 40 | + |
| 41 | + static func networkBlockedHandler(error: Error, blockerPolicy: NetBlockerFlushPolicy) { |
| 42 | + switch error { |
| 43 | + case AnalyticsError.networkUnknown(let error): |
| 44 | + if let e = error as? URLError { |
| 45 | + if e.code == URLError.networkConnectionLost { |
| 46 | + // Little Snitch might be running.. |
| 47 | + // lets disable analytics for now. |
| 48 | + blockerPolicy.analytics?.enabled = false |
| 49 | + print("The network appears to be blocked. Disabling Analytics.") |
| 50 | + } |
| 51 | + } |
| 52 | + default: |
| 53 | + break |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public func configure(analytics: Segment.Analytics) { |
| 58 | + // if we've already been configured, exit. |
| 59 | + guard self.analytics == nil else { return } |
| 60 | + |
| 61 | + self.analytics = analytics |
| 62 | + // add our utility plugin portion of our policy ... |
| 63 | + // that way we can try to enable analytics when the app comes back to the foreground. |
| 64 | + self.analytics?.add(plugin: self) |
| 65 | + } |
| 66 | + |
| 67 | + public func shouldFlush() -> Bool { |
| 68 | + // if we're enabled, then flush. if we don't know if we're enabled |
| 69 | + // it's probably because our analytics pointer became nil somehow, so |
| 70 | + // to prevent unexpected catastrophe, assume true. |
| 71 | + return self.analytics?.enabled ?? true |
| 72 | + } |
| 73 | + |
| 74 | + public func updateState(event: Segment.RawEvent) { |
| 75 | + // do nothing |
| 76 | + } |
| 77 | + |
| 78 | + public func reset() { |
| 79 | + // if we're told to reset.. lets try again. |
| 80 | + analytics?.enabled = true |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +extension NetBlockerFlushPolicy: UtilityPlugin { |
| 85 | + // we can be a utility plugin as well to get lifecycle events to act on |
| 86 | + // see `type` defined in the main class above. |
| 87 | +} |
| 88 | + |
| 89 | +#if os(macOS) |
| 90 | +extension NetBlockerFlushPolicy: macOSLifecycle { |
| 91 | + public func applicationDidBecomeActive() { |
| 92 | + // try to turn back on analytics and see if it's allowed ... if net is still |
| 93 | + // blocked, analytics will be disabled again. |
| 94 | + analytics?.enabled = true |
| 95 | + print("Turning analytics back on ...") |
| 96 | + } |
| 97 | +} |
| 98 | +#endif |
| 99 | + |
| 100 | +#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) |
| 101 | +import UIKit |
| 102 | +extension NetBlockerFlushPolicy: iOSLifecycle { |
| 103 | + public func applicationDidBecomeActive(application: UIApplication?) { |
| 104 | + // try to turn back on analytics and see if it's allowed ... if net is still |
| 105 | + // blocked, analytics will be disabled again. |
| 106 | + analytics?.enabled = true |
| 107 | + print("Turning analytics back on ...") |
| 108 | + } |
| 109 | +} |
| 110 | +#endif |
0 commit comments