Skip to content

Commit

Permalink
feat: add carrier info (#27)
Browse files Browse the repository at this point in the history
* feat: add carrier info

* feat: add macro for the build

* feat: use where instead of for if, add comment to plugin example

* fix: typo
  • Loading branch information
liuyang1520 authored Dec 16, 2022
1 parent 1323f65 commit 3a946d8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by Alyssa.Yu on 12/7/22.
//

// This plugin example currently supports iOS 14+ only.
// NOTE: You can see this plugin in use in the AmplitudeSwiftUIExample application.
//
// This plugin is NOT SUPPORTED by Amplitude. It is here merely as an example,
Expand Down
23 changes: 15 additions & 8 deletions Sources/Amplitude/Plugins/ContextPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
// Created by Marvin Liu on 10/28/22.
//

#if os(iOS)
import CoreTelephony
#endif
import Foundation

class ContextPlugin: Plugin {
Expand Down Expand Up @@ -63,14 +66,18 @@ class ContextPlugin: Plugin {
staticContext["language"] = Locale.preferredLanguages[0]
}

// TODO: need to add logic for multi carrier
/* let networkInfo = NSClassFromString("CTTelephonyNetworkInfo")
if networkInfo != nil {
let subscriberCellularProvider = NSSelectorFromString("subscriberCellularProvider")
let carrier = networkInfo?.method(for: subscriberCellularProvider) ?? "unknown"
staticContext["carrier"] = carrier
var carrier = "Unknown"
#if os(iOS)
let networkInfo = CTTelephonyNetworkInfo()
if let providers = networkInfo.serviceSubscriberCellularProviders {
for (_, provider) in providers where provider.mobileNetworkCode != nil {
carrier = provider.carrierName ?? carrier
// As long as we get one carrier information, we break.
break
}
}
*/
#endif
staticContext["carrier"] = carrier

if Locale.preferredLanguages.count > 0 {
staticContext["country"] = Locale.current.regionCode
Expand Down Expand Up @@ -141,7 +148,7 @@ class ContextPlugin: Plugin {
if trackingOptions?.shouldTrackIDFV() ?? false {
event.idfv = context["idfv"] as? String
}
if (trackingOptions?.shouldTrackLatLng() ?? false) && (self.amplitude?.locationInfoBlock != nil) {
if (trackingOptions?.shouldTrackLatLng() ?? false) && (self.amplitude?.locationInfoBlock != nil) {
let location = self.amplitude?.locationInfoBlock!()
event.locationLat = location?.lat
event.locationLng = location?.lng
Expand Down

0 comments on commit 3a946d8

Please sign in to comment.