3

Hi I am Working on SMSComposer and want to send SMS to Contacts from Address Book.I want to detect whether my iPhone has SIM Card in it or not

Is there any way i can reach this.?

I tried REachability Classes but it gives you information about WWAN and internet Connectivity.Does Core Telephony Framework has such Facility.? Something Like Carrier NAme etc for iPhone.??

Any help would be appreciated. Thanks Vikas

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
Vikas Ojha
  • 444
  • 1
  • 8
  • 23

2 Answers2

9

Swift / iOS 12+

you can try it via the CoreTelephony framework, eg.

let noAvailability = CTTelephonyNetworkInfo()
    .serviceSubscriberCellularProviders?
    .values
    .compactMap { $0.mobileCountryCode }
    .isEmpty ?? false

The official docs of mobileCountryCode (of CTCarrier) contains a bit more information about some edge cases, which may be worth to take into account.


original answer

try to link the CoreTelephony.framework then:

#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>

NSString *_code = [[[[CTTelephonyNetworkInfo alloc] init] subscriberCellularProvider] mobileCountryCode];

you can find more information in the official documentation about that but briefly:

The value for this property is nil if any of the following applies:

  • There is no SIM card in the device.

  • The device is outside of cellular service range.

The value may be nil on hardware prior to iPhone 4S when in Airplane mode.

holex
  • 23,961
  • 7
  • 62
  • 76
  • It doesn't actually return, I read in some blogs that, the iOS returns the sim values and is not updated with the current availability of the sim. I have allocated the tni = [[CTTelephonyNetworkInfo alloc] init]; NSString *simStatusMNC = tni.subscriberCellularProvider.mobileNetworkCode; NSString *simStatusISO = tni.subscriberCellularProvider.isoCountryCode; NSString *simStatusMCC = tni.subscriberCellularProvider.mobileCountryCode; But still it returns the old values. I checked in my application. Please update – Dheeraj Jami Dec 03 '12 at 09:25
  • 1
    @DheerajJami, thank you for your comment, but you are wrong in this case. it is totally works as I described in my answer, which relates the **official documentation**. you are mixing it with the `carrierName` property, I advice you, please try that code what is in my answer, not something else. this is why I've written what I've written; and when you would be stuck, please feel free to read carefully the **official documentation** as well instead of the untrusted sources. thank you! – holex Dec 03 '12 at 12:27
  • Hi Holex, Yes it is true that the above code returns nil if the sim is not available or in aeroplane mode or if the device out of cellular service range, but even if the sim is in its normal mode, it is still returning nil values. Please help us in this regard. NSString *_code = [[[CTCarrier alloc] init] mobileCountryCode]; NSString *code1 = [[[CTCarrier alloc] init] isoCountryCode]; NSString *code2 = [[[CTCarrier alloc] init] mobileNetworkCode]; – Dheeraj Jami Dec 04 '12 at 08:20
  • Hello Dhiraj, how do i import core telephony framework to use this code – Vikas Ojha Dec 04 '12 at 09:29
  • Does not work for me. Always returns nil. http://nanostuffs.com/Blog/?p=999 works. – Sunkas Apr 23 '14 at 09:42
  • use CTTelephonyNetworkInfo to get CTCarrier ,this always return nil,more see http://stackoverflow.com/questions/10488898/iphone-detecting-sim-card-availability – NSDeveloper Dec 08 '15 at 02:32
1

For this purpose you need to add MessageUI.framework framework...

Try this code...It may help you

Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
if([messageClass canSendText]){
    // Sim available
}
else{
    //Sim not available
}
Murali
  • 1,869
  • 1
  • 14
  • 22
  • 1
    it says only (I quote): `Returns a Boolean value indicating whether the current device is capable of sending text messages.` it can give info about the _capability_ only, not about the _availability_. – holex Nov 30 '12 at 12:07
  • Hi Thanks for your Help,,, Regarding /*! canSendText if the user has set up the device for sending text only messages. If the return value is YES, the client can set the recipients and body of the message. If the return value is NO, the client may notify the user of the failure, or the client may open an SMS URL via -[UIApplication openURL:]. */ + (BOOL)canSendText – Vikas Ojha Nov 30 '12 at 13:06
  • Here as much as i know it tells you whether you can send Message or not,Also if its true but SIM is not in iPhone our Message gets saved in OUTBOX but the code you provided does not tell if sim is installed in iPhone .PLEASE CORRECT ME IF I AM WRONG – Vikas Ojha Nov 30 '12 at 13:06
  • have you tested it? I have used it in one of my projects and it is working fine.... – Murali Nov 30 '12 at 13:12
  • what about if sim card available but in sim card no enough balance for sending SMS..? – Nitin Gohel Jun 06 '13 at 12:35
  • 1
    Does not work for me. `canSendText` always returns YES even if SIM card is out. – Sunkas Apr 23 '14 at 09:43
  • 1
    You can send a text message without Simcard over iMessage. So it has no exact solution. – Onder OZCAN Oct 22 '15 at 13:56