|
| 1 | +// |
| 2 | +// CellularCarrier.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Brandon Sneed on 4/12/22. |
| 6 | +// |
| 7 | + |
| 8 | +// NOTE: You can see this plugin in use in the SwiftUIKitExample application. |
| 9 | +// |
| 10 | +// This plugin is NOT SUPPORTED by Segment. It is here merely as an example, |
| 11 | +// and for your convenience should you find it useful. |
| 12 | + |
| 13 | +// MIT License |
| 14 | +// |
| 15 | +// Copyright (c) 2021 Segment |
| 16 | +// |
| 17 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 18 | +// of this software and associated documentation files (the "Software"), to deal |
| 19 | +// in the Software without restriction, including without limitation the rights |
| 20 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 21 | +// copies of the Software, and to permit persons to whom the Software is |
| 22 | +// furnished to do so, subject to the following conditions: |
| 23 | +// |
| 24 | +// The above copyright notice and this permission notice shall be included in all |
| 25 | +// copies or substantial portions of the Software. |
| 26 | +// |
| 27 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 28 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 29 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 30 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 31 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 32 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 33 | +// SOFTWARE. |
| 34 | + |
| 35 | +import Foundation |
| 36 | +import Segment |
| 37 | +import CoreTelephony |
| 38 | + |
| 39 | +/** |
| 40 | + An example plugin to retrieve cellular information. |
| 41 | + |
| 42 | + This plugin will add all carrier information to the event's context.network object if cellular |
| 43 | + is currently in use. Example contents: |
| 44 | + |
| 45 | + { |
| 46 | + "home": "T-Mobile", |
| 47 | + "roaming": "AT&T", |
| 48 | + "secondary": "Verizon", |
| 49 | + } |
| 50 | + |
| 51 | + */ |
| 52 | +class CellularCarrier: Plugin { |
| 53 | + var type: PluginType = .enrichment |
| 54 | + |
| 55 | + var analytics: Analytics? |
| 56 | + |
| 57 | + func execute<T: RawEvent>(event: T?) -> T? { |
| 58 | + guard var workingEvent = event else { return event } |
| 59 | + |
| 60 | + if let isCellular: Bool = workingEvent.context?[keyPath: "network"], |
| 61 | + isCellular, |
| 62 | + let carriers = self.carriers |
| 63 | + { |
| 64 | + workingEvent.context?[keyPath: "network.carriers"] = carriers |
| 65 | + } |
| 66 | + |
| 67 | + return workingEvent |
| 68 | + } |
| 69 | + |
| 70 | + // done as a compute-once stored property; your use case may be different. |
| 71 | + var carriers: [String: String]? = { |
| 72 | + let info = CTTelephonyNetworkInfo() |
| 73 | + if let providers = info.serviceSubscriberCellularProviders { |
| 74 | + var results = [String: String]() |
| 75 | + for (key, value) in providers { |
| 76 | + if let carrier = value.carrierName, !carrier.isEmpty { |
| 77 | + results[key] = value.carrierName |
| 78 | + } |
| 79 | + } |
| 80 | + if !results.isEmpty { |
| 81 | + return results |
| 82 | + } |
| 83 | + } |
| 84 | + return nil |
| 85 | + }() |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | +/** |
| 90 | + An example plugin to retrieve cellular information. |
| 91 | + |
| 92 | + This plugin will add primary ("home") carrier information to the event's context.network object if cellular |
| 93 | + is currently in use. This mimics the operation of the analytics-ios SDK. |
| 94 | + */ |
| 95 | +class PrimaryCellularCarrier: Plugin { |
| 96 | + var type: PluginType = .enrichment |
| 97 | + |
| 98 | + var analytics: Analytics? |
| 99 | + |
| 100 | + func execute<T: RawEvent>(event: T?) -> T? { |
| 101 | + guard var workingEvent = event else { return event } |
| 102 | + |
| 103 | + if let isCellular: Bool = workingEvent.context?[keyPath: "network"], |
| 104 | + isCellular, |
| 105 | + let carrier = self.carrier |
| 106 | + { |
| 107 | + workingEvent.context?[keyPath: "network.carriers"] = carrier |
| 108 | + } |
| 109 | + |
| 110 | + return workingEvent |
| 111 | + } |
| 112 | + |
| 113 | + // done as a compute-once stored property; your use case may be different. |
| 114 | + var carrier: String? = { |
| 115 | + let info = CTTelephonyNetworkInfo() |
| 116 | + if let providers = info.serviceSubscriberCellularProviders { |
| 117 | + let primary = providers["home"] |
| 118 | + if let carrier = primary?.carrierName, !carrier.isEmpty { |
| 119 | + return carrier |
| 120 | + } |
| 121 | + } |
| 122 | + return nil |
| 123 | + }() |
| 124 | +} |
| 125 | + |
0 commit comments