Retrieving Carrier Name from iPhone programmatically
Asked Answered
H

11

50

Is there a way to know the cell carrier on an iPhone programmatically?

I am looking for the carrier name which the iPhone is connected to.

Hangover answered 12/5, 2009 at 15:46 Comment(5)
What, you mean there are iPhone carriers other than AT&T? <g/d/r>Energy
Are you talking about who's service the phone is currently connected to (i.e. might be roaming), or who is the carrier you get bills from?Ic
You might not have heard of it, but there are other countries in the world. And interestingly, they do have mobile technology.Mercury
Again - have a sense of humor folks.Cropper
The only mechanism to detect if you are roaming is through reading an undocumented system file See previous question which is against the Apple rulesPrecede
K
84

In iOS 4, the CoreTelephony framework is useable, here's a snippet to get the carrier name:

CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [netinfo subscriberCellularProvider];
NSLog(@"Carrier Name: %@", [carrier carrierName]);
[netinfo release];

Link against CoreTelephony and include in your headers:

#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
Kelsiekelso answered 16/10, 2010 at 7:51 Comment(4)
According to Apple's documentation, this always give you info about your honme carrier, not the carrier you're currently connected to when roaming.Gyroscope
for carrier name, it's just "Carrier", not something like verizon, ... .Woosley
Now CTCarrier is deprecated. developer.apple.com/documentation/coretelephony/ctcarrier . I am wondering what would be the alternative approach for this? I don't find alternative API for this.Handwoven
@RohitSingh Could it be CTTelephonyNetworkInfo .serviceSubscriberCellularProviders? That returns a dict of CTCarrier and is not deprecated... developer.apple.com/documentation/coretelephony/…Meteorology
O
15

Just to make a note here.. I tested this API on different SIMs and it seems that the name of the operator the iPhone is locked to is returned with [carrer carrierName]!!

I tested this on 2 iphones, one locked and the other not, and for the locked one, regardless of the SIM provider, it returns the name of the operator it is locked to everytime i run my test app. Note however that the MNC does change!

Od answered 19/5, 2011 at 13:9 Comment(3)
Good comment. Was not aware of this until I saw this reply. Can confirm I am getting the same behaviour - the carrierName returns the locked carrier name, not the SIM carrier.Rabbinate
Tested on iOS 7, unlocked phone: always returns "Carrier" as carrierName. So it is not possible to correctly figure out which operator is in the phone. The string returned is equal to string displayed in Settings -> General -> About under Carrier. The version number is lost, however.Dispend
This is correct. MNC is the only accurate test of carrier from CTCarrier. We cache MNC value as well, as it will return nil if the user has no network connection, is in airplane mode, or has removed their SIM.Survivor
D
6

CTCarrier, carrierName and other info is deprecated as of iOS 16 with no replacement: https://developer.apple.com/documentation/coretelephony/ctcarrier.

Delmardelmer answered 23/10, 2022 at 10:17 Comment(0)
D
4

For swift users you can try this:

import CoreTelephony

static var carrierName:String? {
    let networkInfo = CTTelephonyNetworkInfo()
    let carrier = networkInfo.subscriberCellularProvider
    return carrier?.carrierName
}
Dani answered 13/7, 2016 at 13:23 Comment(3)
carrier?.carrierName not exists currentlyDegraw
Currently exists carrier?.descriptionDegraw
The output of carrier?.description: [\"0000000100000001\": CTCarrier (0x2803a1980) {\n\tCarrier name: [Vodafone]\n\tMobile Country Code: [214]\n\tMobile Network Code:[01]\n\tISO Country Code:[es]\n\tAllows VOIP? [YES]\n}\n]Degraw
O
2

There is no public API for getting the carrier name. If you don't need to publish on the App Store you could look at using private api's.

VVCarrierParameters.h in the VisualVoiceMail package seems to have a carrierServiceName class method that might be what you need. Drop that header in your project and call [VVCarrierParameters carrierServiceName].

Note your app will most likely be rejected if you do this.

Oospore answered 12/5, 2009 at 17:45 Comment(0)
D
1

While developing Alpha, I encountered the same problem. The project itself was not limited to use only public API, so first I tried @Jason Harwig's solution. Because I could not get it to work, I thought of another option.

My solution uses private API to access the _serviceString ivar of the label (UIStatusBarServiceItemView) that is displayed in status bar.

It relies on status bar having a carrier value and only needs UIKit to work.

- (NSString *)carrierName
{
    UIView* statusBar = [self statusBar];

    UIView* statusBarForegroundView = nil;

    for (UIView* view in statusBar.subviews)
    {
        if ([view isKindOfClass:NSClassFromString(@"UIStatusBarForegroundView")])
        {
            statusBarForegroundView = view;
            break;
        }
    }

    UIView* statusBarServiceItem = nil;

    for (UIView* view in statusBarForegroundView.subviews)
    {
        if ([view isKindOfClass:NSClassFromString(@"UIStatusBarServiceItemView")])
        {
            statusBarServiceItem = view;
            break;
        }
    }

    if (statusBarServiceItem)
    {
        id value = [statusBarServiceItem valueForKey:@"_serviceString"];

        if ([value isKindOfClass:[NSString class]])
        {
            return (NSString *)value;
        }
    }

    return @"Unavailable";
}

- (UIView *)statusBar
{
    NSString *statusBarString = [NSString stringWithFormat:@"%@ar", @"_statusB"];
    return [[UIApplication sharedApplication] valueForKey:statusBarString];
}

I only tested the method with applications that have status bar visible. It returns the same string as it is displayed in status bar, so it works correctly even when roaming.

This method is not App Store safe.

Dispend answered 1/6, 2015 at 16:34 Comment(0)
A
1

Get carrier name from status bar in case if Core Telephony returns "Carrier"

func getCarrierName() -> String? {

    var carrierName: String?

    let typeName: (Any) -> String = { String(describing: type(of: $0)) }

    let statusBar = UIApplication.shared.value(forKey: "_statusBar") as! UIView

    for statusBarForegroundView in statusBar.subviews {
        if typeName(statusBarForegroundView) == "UIStatusBarForegroundView" {
            for statusBarItem in statusBarForegroundView.subviews {
                if typeName(statusBarItem) == "UIStatusBarServiceItemView" {
                    carrierName = (statusBarItem.value(forKey: "_serviceString") as! String)
                }
            }
        }
    }
    return carrierName
}
Allergen answered 30/1, 2018 at 8:56 Comment(1)
Is this method App Store safe or not?Electrocardiograph
L
1

for Swift and ios 12.0 < do the following:

import CoreTelephony

static var carrierName:String? {
    CTTelephonyNetworkInfo().serviceSubscriberCellularProviders?.first?.value.carrierName ?? ""
}
Landre answered 7/1, 2021 at 11:5 Comment(0)
E
0

https://developer.apple.com/iphone/prerelease/library/documentation/NetworkingInternet/Reference/CTCarrier/Reference/Reference.html#//apple_ref/doc/uid/TP40009596-CH1-DontLinkElementID_3

There is a such way however it's only available on iOS 4 so you won't be able to use it on previous versions. And this probably breaks your backward compatibility too.

Extradite answered 19/8, 2010 at 12:49 Comment(0)
D
0

When you print output of carrier?.description

This is what you see:

[\"0000000100000001\": CTCarrier (0x2803a1980) {\n\tCarrier name: [Vodafone]\n\tMobile Country Code: [214]\n\tMobile Network Code:[01]\n\tISO Country Code:[es]\n\tAllows VOIP? [YES]\n}\n]

Formatted (\n and \t):

[\"0000000100000001\": CTCarrier (0x2803a1980) {
    Carrier name: [Vodafone]
    Mobile Country Code: [214]
    Mobile Network Code:[01]
    ISO Country Code:[es]
    Allows VOIP? [YES]
}
]

So get carrier name from status bar is a good option (at least for me)

I mean the answer of "codethemall" user.

Degraw answered 2/4, 2019 at 9:43 Comment(0)
S
0

More of an important comment. Just to add docs about the carrierName:

The carrier provides this string, formatting it for presentation to the user. The value does not change if the user is roaming; it always represents the provider with which the user has an account.

If you configure a device for a carrier and then remove the SIM card, this property retains the name of the carrier. If you then install a new SIM card, its carrier name replaces the previous value of this property.

The value for this property is nil if the user never configured a carrier for the device.

Shannashannah answered 19/8, 2020 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.