iOS : Check whether the phone is dual SIM? - ios

In Android we can detect whether the phone is Single SIM or Dual SIM. And as we know iPhone XS, iPhone XS Max, iPhone XR, and later feature Dual SIM with a nano-SIM and an eSIM except china mainland where there is no eSIM.
A) I can programatically invoke MFMessageComposeViewController
from a button click to send the SMS to a particular number. Here is
the following code: This code can tell us whether the SIM is
inserted or not. But will it work for eSIM too?
-(void)openMessageViewWithName:(NSString*)contactName withPhone:(NSString *)phone{
CTTelephonyNetworkInfo *networkInfo=[[CTTelephonyNetworkInfo alloc]init];
CTCarrier *carrier=networkInfo.subscriberCellularProvider;
NSString *Countrycode = carrier.isoCountryCode;
if ([Countrycode length]>0) //Check If Sim Inserted
{
[self sendSMS:msg recipientList:[NSMutableArray arrayWithObject:phone]];
}
else
{
[AlertHelper showAlert:#"Message" withMessage:#"No sim card inserted"];
}
//Method for sending message:
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSMutableArray *)recipients{
MFMessageComposeViewController *controller1 = [[MFMessageComposeViewController alloc] init] ;
controller1 = [[MFMessageComposeViewController alloc] init] ;
if([MFMessageComposeViewController canSendText])
{
controller1.body = bodyOfMessage;
controller1.recipients = recipients;
controller1.messageComposeDelegate = self;
[self presentViewController:controller1 animated:YES completion:Nil];
}
}
B) Is it possible to determine whether my iPhone is Dual SIM or Single SIM programatically? If yes it is DUAL SIM then can we choose the SIM from my application to send the SMS ?
Please help me with these and help me with a workaround .

As #PaulW11 mentioned in comment "No, you cannot detect sim details nor select the sim that is used to send messages. This is controlled by the user
"
Read Carefully for understanding
A) First of all no SIM related datas or phone number you are going to get in iOS, the only details you get is regarding the network like carriername,mobilecountrycode,isocountrycode,mobilenetworkcode. So if you replace a Verizon SIM with Verizon you will see the carrier name as Verizon only , nothing else you are going to get
SOLUTION Invoke MFMessageController from a button click as you are doing.This opens the iOS default SMS/Messages app. Then allow the user to choose the SIM in case of Dual SIM from the Messages App itself.
B) It is not possible to select the particular SIM/Phone Number inside your application. Because there is no provision in iOS to do that as in Android. Instead you must invoke the default messages app in iOS where you can choose your desired SIM (in case of DUAL sim) for sending SMS or else in case of single SIM it directly sends the message from that number

did you try this?
CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc]init];
if (#available(iOS 12.0, *)) {
NSDictionary *providers = [networkInfo serviceSubscriberCellularProviders];
NSEnumerator *e = [providers objectEnumerator];
if ([[e.allObjects firstObject] isKindOfClass:(CTCarrier.class)]) {
NSString *str = ((CTCarrier *)[e.allObjects firstObject]).isoCountryCode;
}
} else {
// do a fallback
}

Related

Getting existing app with fingerprint authentication to work with iPhone X FaceId

I have an app already developed and is live on the store. Its working well and has fingerprint authentication implemented. Now that Apple has made it mandatory to provide iPhone X support, I will be releasing one more update of the app.
But, i wish to understand what will happen if install the already submitted version of the app on iPhone X..?
I have read that -
App will provide a compatibility alert saying This app was designed to use Touch ID and may not fully support FaceID, But it goes away if we insert a key NSFaceIDUsageDescription in the plist.
I have also read that the app will crash if build using iOS 11 SDk (without the key NSFaceIDUsageDescription in the plist).
Here is my question - what if the app is build using iOS 8 and installed in iOS 11, will it still crash ? if not, how will be the exact behaviour..? I have tested in the simulator and the app did not crash but i do not have iPhone X available at hands and cannot crosscheck the same on the actual device.
NOTE - This crash is observed on the device more and not on simulator.
How important it is to have the word FaceId in the app running on iPhone X. What if I use a general terms like "Biometric authentication" in place of touchId & FaceId. Will the app still be fine and won't end up rejected ?
Any help or leads will be truly appreciated!
You're right in that you need the NSFaceIDUsageDescription key in your plist to properly support Face ID.
If you built the app using Xcode 9 against the iOS 11 SDK, even if you set the target to iOS 8, you need to include that key to support iPhone X.
That said, I don't see how this might be crashing. The same code that you use for Touch ID also supports Face ID.
I use this to handle faceID or touchID
if (#available(iOS 11.0, *)) {
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
if (myContext.biometryType == LABiometryTypeTouchID) {
SwitchItem *touchIdItem = [[SwitchItem alloc] init];
touchIdItem.name = NSLocalizedString(#"PROFILE_SETTINGS_TOUCHID", nil);
touchIdItem.active = [[[Global instance] objectInKey:KEY_TOUCHID_ENABLED] boolValue];
[self.dataSource addObject:touchIdItem];
} else if (myContext.biometryType == LABiometryTypeFaceID) {
SwitchItem *faceIdItem = [[SwitchItem alloc] init];
faceIdItem.name = NSLocalizedString(#"PROFILE_SETTINGS_FACEID", nil);
faceIdItem.active = [[[Global instance] objectInKey:KEY_FACEID_ENABLED] boolValue];
[self.dataSource addObject:faceIdItem];
}
}
} else {
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
SwitchItem *touchIdItem = [[SwitchItem alloc] init];
touchIdItem.name = NSLocalizedString(#"PROFILE_SETTINGS_TOUCHID", nil);
touchIdItem.active = [[[Global instance] objectInKey:KEY_TOUCHID_ENABLED] boolValue];
[self.dataSource addObject:touchIdItem];
}
}
You have to evaluate iOS version and after that yo can suppose if the gadget have a touchID or faceID

Invite feature in iOS, how to send a person's contact a message from within your app? [duplicate]

Does anybody know if it's possible, and how, to programmatically send a SMS from the iPhone, with the official SDK / Cocoa Touch?
Restrictions
If you could send an SMS within a program on the iPhone, you'll be able to write games that spam people in the background. I'm sure you really want to have spams from your friends, "Try out this new game! It roxxers my boxxers, and yours will be too! roxxersboxxers.com!!!! If you sign up now you'll get 3,200 RB points!!"
Apple has restrictions for automated (or even partially automated) SMS and dialing operations. (Imagine if the game instead dialed 911 at a particular time of day)
Your best bet is to set up an intermediate server on the internet that uses an online SMS sending service and send the SMS via that route if you need complete automation. (ie, your program on the iPhone sends a UDP packet to your server, which sends the real SMS)
iOS 4 Update
iOS 4, however, now provides a viewController you can import into your application. You prepopulate the SMS fields, then the user can initiate the SMS send within the controller. Unlike using the "SMS:..." url format, this allows your application to stay open, and allows you to populate both the to and the body fields. You can even specify multiple recipients.
This prevents applications from sending automated SMS without the user explicitly aware of it. You still cannot send fully automated SMS from the iPhone itself, it requires some user interaction. But this at least allows you to populate everything, and avoids closing the application.
The MFMessageComposeViewController class is well documented, and tutorials show how easy it is to implement.
iOS 5 Update
iOS 5 includes messaging for iPod touch and iPad devices, so while I've not yet tested this myself, it may be that all iOS devices will be able to send SMS via MFMessageComposeViewController. If this is the case, then Apple is running an SMS server that sends messages on behalf of devices that don't have a cellular modem.
iOS 6 Update
No changes to this class.
iOS 7 Update
You can now check to see if the message medium you are using will accept a subject or attachments, and what kind of attachments it will accept. You can edit the subject and add attachments to the message, where the medium allows it.
iOS 8 Update
No changes to this class.
iOS 9 Update
No changes to this class.
iOS 10 Update
No changes to this class.
iOS 11 Update
No significant changes to this class
Limitations to this class
Keep in mind that this won't work on phones without iOS 4, and it won't work on the iPod touch or the iPad, except, perhaps, under iOS 5. You must either detect the device and iOS limitations prior to using this controller, or risk restricting your app to recently upgraded 3G, 3GS, and 4 iPhones.
However, an intermediate server that sends SMS will allow any and all of these iOS devices to send SMS as long as they have internet access, so it may still be a better solution for many applications. Alternately, use both, and only fall back to an online SMS service when the device doesn't support it.
Here is a tutorial which does exactly what you are looking for: the MFMessageComposeViewController.
http://blog.mugunthkumar.com/coding/iphone-tutorial-how-to-send-in-app-sms/
Essentially:
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"SMS message here";
controller.recipients = [NSArray arrayWithObjects:#"1(234)567-8910", nil];
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
And a link to the docs.
https://developer.apple.com/documentation/messageui/mfmessagecomposeviewcontroller
You must add the MessageUI.framework to your Xcode project
Include an #import <MessageUI/MessageUI.h> in your header file
Add these delegates to your header file MFMessageComposeViewControllerDelegate & UINavigationControllerDelegate
In your IBAction method declare instance of MFMessageComposeViewController say messageInstance
To check whether your device can send text use [MFMessageComposeViewController canSendText] in an if condition, it'll return Yes/No
In the if condition do these:
First set body for your messageInstance as:
messageInstance.body = #"Hello from Shah";
Then decide the recipients for the message as:
messageInstance.recipients = [NSArray arrayWithObjects:#"12345678", #"87654321", nil];
Set a delegate to your messageInstance as:
messageInstance.messageComposeDelegate = self;
In the last line do this:
[self presentModalViewController:messageInstance animated:YES];
You can use a sms:[target phone number] URL to open the SMS application, but there are no indications on how to prefill a SMS body with text.
One of the systems of inter-process communication in MacOS is XPC. This system layer has been developed for inter-process communication based on the transfer of plist structures using libSystem and launchd. In fact, it is an interface that allows managing processes via the exchange of such structures as dictionaries. Due to heredity, iOS 5 possesses this mechanism as well.
You might already understand what I mean by this introduction. Yep, there are system services in iOS that include tools for XPC communication. And I want to exemplify the work with a daemon for SMS sending. However, it should be mentioned that this ability is fixed in iOS 6, but is relevant for iOS 5.0—5.1.1. Jailbreak, Private Framework, and other illegal tools are not required for its exploitation. Only the set of header files from the directory /usr/include/xpc/* are needed.
One of the elements for SMS sending in iOS is the system service com.apple.chatkit, the tasks of which include generation, management, and sending of short text messages. For the ease of control, it has the publicly available communication port com.apple.chatkit.clientcomposeserver.xpc. Using the XPC subsystem, you can generate and send messages without user's approval. 
Well, let's try to create a connection.
xpc_connection_t myConnection;
dispatch_queue_t queue = dispatch_queue_create("com.apple.chatkit.clientcomposeserver.xpc", DISPATCH_QUEUE_CONCURRENT);
myConnection = xpc_connection_create_mach_service("com.apple.chatkit.clientcomposeserver.xpc", queue, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED);
Now we have the XPC connection myConnection set to the service of SMS sending. However, XPC configuration provides for creation of suspended connections —we need to take one more step for the activation.
xpc_connection_set_event_handler(myConnection, ^(xpc_object_t event){
xpc_type_t xtype = xpc_get_type(event);
if(XPC_TYPE_ERROR == xtype)
{
NSLog(#"XPC sandbox connection error: %s\n", xpc_dictionary_get_string(event, XPC_ERROR_KEY_DESCRIPTION));
}
// Always set an event handler. More on this later.
NSLog(#"Received a message event!");
});
xpc_connection_resume(myConnection);
The connection is activated. Right at this moment iOS 6 will display a message in the telephone log that this type of communication is forbidden. Now we need to generate a dictionary similar to xpc_dictionary with the data required for the message sending.
NSArray *recipient = [NSArray arrayWithObjects:#"+7 (90*) 000-00-00", nil];
NSData *ser_rec = [NSPropertyListSerialization dataWithPropertyList:recipient format:200 options:0 error:NULL];
xpc_object_t mydict = xpc_dictionary_create(0, 0, 0);
xpc_dictionary_set_int64(mydict, "message-type", 0);
xpc_dictionary_set_data(mydict, "recipients", [ser_rec bytes], [ser_rec length]);
xpc_dictionary_set_string(mydict, "text", "hello from your application!");
Little is left: send the message to the XPC port and make sure it is delivered.
xpc_connection_send_message(myConnection, mydict);
xpc_connection_send_barrier(myConnection, ^{
NSLog(#"The message has been successfully delivered");
});
That's all. SMS sent.
Add the MessageUI.Framework and use the following code
#import <MessageUI/MessageUI.h>
And then:
if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *messageComposer =
[[MFMessageComposeViewController alloc] init];
NSString *message = #"Your Message here";
[messageComposer setBody:message];
messageComposer.messageComposeDelegate = self;
[self presentViewController:messageComposer animated:YES completion:nil];
}
and the delegate method -
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result {
[self dismissViewControllerAnimated:YES completion:nil];
}
You can use this approach:
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:#"sms:MobileNumber"]]
iOS will automatically navigate from your app to the messages app's message composing page. Since the URL's scheme starts with sms:, this is identified as a type that is recognized by the messages app and launches it.
Follow this procedures
1 .Add MessageUI.Framework to project
2 . Import #import <MessageUI/MessageUI.h> in .h file.
3 . Copy this code for sending message
if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *messageComposer =
[[MFMessageComposeViewController alloc] init];
NSString *message = #"Message!!!";
[messageComposer setBody:message];
messageComposer.messageComposeDelegate = self;
[self presentViewController:messageComposer animated:YES completion:nil];
}
4 . Implement delegate method if you want to.
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
///your stuff here
[self dismissViewControllerAnimated:YES completion:nil];
}
Run And GO!
//Add the Framework in .h file
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
//Set the delegate methods
UIViewController<UINavigationControllerDelegate,MFMessageComposeViewControllerDelegate>
//add the below code in .m file
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
MFMessageComposeViewController *controller =
[[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
NSString *str= #"Hello";
controller.body = str;
controller.recipients = [NSArray arrayWithObjects:
#"", nil];
controller.delegate = self;
[self presentModalViewController:controller animated:YES];
}
}
- (void)messageComposeViewController:
(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result
{
switch (result)
{
case MessageComposeResultCancelled:
NSLog(#"Cancelled");
break;
case MessageComposeResultFailed:
NSLog(#"Failed");
break;
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
Here is the Swift version of code to send SMS in iOS. Please noted that it only works in real devices. Code tested in iOS 7+. You can read more here.
1) Create a new Class which inherits MFMessageComposeViewControllerDelegate and NSObject:
import Foundation
import MessageUI
class MessageComposer: NSObject, MFMessageComposeViewControllerDelegate {
// A wrapper function to indicate whether or not a text message can be sent from the user's device
func canSendText() -> Bool {
return MFMessageComposeViewController.canSendText()
}
// Configures and returns a MFMessageComposeViewController instance
func configuredMessageComposeViewController(textMessageRecipients:[String] ,textBody body:String) -> MFMessageComposeViewController {
let messageComposeVC = MFMessageComposeViewController()
messageComposeVC.messageComposeDelegate = self // Make sure to set this property to self, so that the controller can be dismissed!
messageComposeVC.recipients = textMessageRecipients
messageComposeVC.body = body
return messageComposeVC
}
// MFMessageComposeViewControllerDelegate callback - dismisses the view controller when the user is finished with it
func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
}
2) How to use this class:
func openMessageComposerHelper(sender:AnyObject ,withIndexPath indexPath: NSIndexPath) {
var recipients = [String]()
//modify your recipients here
if (messageComposer.canSendText()) {
println("can send text")
// Obtain a configured MFMessageComposeViewController
let body = Utility.createInvitationMessageText()
let messageComposeVC = messageComposer.configuredMessageComposeViewController(recipients, textBody: body)
// Present the configured MFMessageComposeViewController instance
// Note that the dismissal of the VC will be handled by the messageComposer instance,
// since it implements the appropriate delegate call-back
presentViewController(messageComposeVC, animated: true, completion: nil)
} else {
// Let the user know if his/her device isn't able to send text messages
self.displayAlerViewWithTitle("Cannot Send Text Message", andMessage: "Your device is not able to send text messages.")
}
}
There is a class in iOS 4 which supports sending messages with body and recipents from your application. It works the same as sending mail. You can find the documentation here: link text
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
UIImage *ui =resultimg.image;
pasteboard.image = ui;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"sms:"]];
}
//call method with name and number.
-(void)openMessageViewWithName:(NSString*)contactName withPhone:(NSString *)phone{
CTTelephonyNetworkInfo *networkInfo=[[CTTelephonyNetworkInfo alloc]init];
CTCarrier *carrier=networkInfo.subscriberCellularProvider;
NSString *Countrycode = carrier.isoCountryCode;
if ([Countrycode length]>0) //Check If Sim Inserted
{
[self sendSMS:msg recipientList:[NSMutableArray arrayWithObject:phone]];
}
else
{
[AlertHelper showAlert:#"Message" withMessage:#"No sim card inserted"];
}
}
//Method for sending message
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSMutableArray *)recipients{
MFMessageComposeViewController *controller1 = [[MFMessageComposeViewController alloc] init] ;
controller1 = [[MFMessageComposeViewController alloc] init] ;
if([MFMessageComposeViewController canSendText])
{
controller1.body = bodyOfMessage;
controller1.recipients = recipients;
controller1.messageComposeDelegate = self;
[self presentViewController:controller1 animated:YES completion:Nil];
}
}
If you want, you can use the private framework CoreTelephony which called CTMessageCenter class. There are a few methods to send sms.
Use this:
- (void)showSMSPicker
{
Class messageClass = (NSClassFromString(#"MFMessageComposeViewController"));
if (messageClass != nil) {
// Check whether the current device is configured for sending SMS messages
if ([messageClass canSendText]) {
[self displaySMSComposerSheet];
}
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
//feedbackMsg.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MessageComposeResultCancelled:
{
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:#"Message" message:#"SMS sending canceled!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert1 show];
[alert1 release];
}
// feedbackMsg.text = #"Result: SMS sending canceled";
break;
case MessageComposeResultSent:
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Message" message:#"SMS sent!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert2 show];
[alert2 release];
}
// feedbackMsg.text = #"Result: SMS sent";
break;
case MessageComposeResultFailed:
{
UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:#"Message" message:#"SMS sending failed!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert3 show];
[alert3 release];
}
// feedbackMsg.text = #"Result: SMS sending failed";
break;
default:
{
UIAlertView *alert4 = [[UIAlertView alloc] initWithTitle:#"Message" message:#"SMS not sent!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert4 show];
[alert4 release];
}
// feedbackMsg.text = #"Result: SMS not sent";
break;
}
[self dismissModalViewControllerAnimated: YES];
}
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:#"sms:number"]]
This would be the best and short way to do it.
You can present MFMessageComposeViewController, which can send SMS, but with user prompt(he taps send button). No way to do that without user permission. On iOS 11, you can make extension, that can be like filter for incoming messages , telling iOS either its spam or not. Nothing more with SMS cannot be done
You need to use the MFMessageComposeViewController if you want to show creating and sending the message in your own app.
Otherwise, you can use the sharedApplication method.

Apple pay PKPaymentauthorizationViewController always returning nil when loaded with Payment request

I am getting the PK Payment auth view controller instance returned as nil. What is wrong with this code?
if([PKPaymentAuthorizationViewController canMakePayments])
{
if ([PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:#[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa]])
{
PKPaymentRequest *request = [[PKPaymentRequest alloc] init];
request.currencyCode = #"USD";
request.countryCode = #"US";
request.merchantCapabilities = 0;
request.requiredBillingAddressFields=PKAddressFieldAll;
request.merchantIdentifier = #"merchant.com.domain.mine";
PKPaymentSummaryItem *item = [[PKPaymentSummaryItem alloc] init];
item.label=#"Merchant";
item.amount=[NSDecimalNumber decimalNumberWithString:#"10"];
request.paymentSummaryItems=#[item];
PKPaymentAuthorizationViewController *viewController = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
viewController.delegate = self;
[self presentViewController:viewController animated:YES completion:nil];
}
}
Before accessing the PKPaymentAuthorizationViewController, you should configure Apple Pay properly on your iPhone device. If you have not configured Apple Pay on your device you'll get nil value for PKPaymentAuthorizationViewController. You can even find an exception on the console stating "This device cannot make payment."
To configure Apple Pay on your device follow the below steps:
Go to Settings.
Select Passbook and Apple Pay option (if this option is not visible in settings, go to General -> Language & Region, change your region to US or UK, after this you'll be able to see the Passbook & Apple Pay option in Settings)
Open Passbook application from your home screen and configure a valid credit/debit card (US/UK based card only).
After verifying the added card, run your application you'll get a valid PKPaymentAuthorizationViewController instance.
Hope this will help.
I had a similar issue. It looks like you included it, but for anyone else struggling with this, my problem was not initially supplying merchantCapabilities to the request.
Swift:
request.merchantCapabilities = PKMerchantCapability.capability3DS
https://developer.apple.com/documentation/passkit/pkmerchantcapability?language=objc
If you are instantiating the supporting networks with raw value, make sure they are done so with the proper capitalization.
// Summarized for posting purposes
let networks = ["AmEx", "Visa", "MasterCard", "Discover"].reduce(into: [PKPaymentNetwork]()) { $0.append(PKPaymentNetwork($1)) }
if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: networks, capabilities: .capability3DS) {
// Hooray
}

Is it possible to determine if the SIM/Phone number has changed?

We have a product where the user registers by providing their phone number.
However after they register they could potentially change their sim.
Is it possible to programatically determine if the sim has been removed or inserted?
(Thanks if you provide it, but any digression comments on the use of using the phone number in the first place would be irrelevant to this discussion, I don't want to discuss that aspect of things, only the sim aspect).
Yes, of course it is possible. Link CoreTelephony.framework to make following code compile:
CTTelephonyNetworkInfo* info = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier* carrier = info.subscriberCellularProvider;
NSString *mobileCountryCode = carrier.mobileCountryCode;
NSString *carrierName = carrier.carrierName;
NSString *isoCountryCode = carrier.isoCountryCode;
NSString *mobileNetworkCode = carrier.mobileNetworkCode;
// Try this to track CTCarrier changes
info.subscriberCellularProviderDidUpdateNotifier = ^(CTCarrier* inCTCarrier) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"User did change SIM");
});
};
By values of mobileCountryCode, mobileNetworkCode, carrierName, isoCountryCode you can judge about presence of SIM. (Without SIM they become incorrect).
There is also some undocumented functions/notifications in CoreTelephony, but your app may be banned by Apple if you'll use them. Anyway:
// Evaluates to #"kCTSIMSupportSIMStatusReady" when SIM is present amd ready;
// there are some other values like #"kCTSIMSupportSIMStatusNotInserted"
NSString* CTSIMSupportGetSIMStatus();
// Use #"kCTSIMSupportSIMStatusChangeNotification" to track changes of SIM status:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(SIMNotification:)
name:#"kCTSIMSupportSIMStatusChangeNotification"
object:nil
];
// This one copies current phone number
NSString* CTSettingCopyMyPhoneNumber()
Addendum Another possible (and legal) solution: if your company has a database of phone numbers, you can send an sms or call(and cut) any specific number to verify that user still uses the same phone number.
UPDATE Function NSString* CTSettingCopyMyPhoneNumber() doesn't work anymore (returns empty string).

How to programmatically send SMS on the iPhone?

Does anybody know if it's possible, and how, to programmatically send a SMS from the iPhone, with the official SDK / Cocoa Touch?
Restrictions
If you could send an SMS within a program on the iPhone, you'll be able to write games that spam people in the background. I'm sure you really want to have spams from your friends, "Try out this new game! It roxxers my boxxers, and yours will be too! roxxersboxxers.com!!!! If you sign up now you'll get 3,200 RB points!!"
Apple has restrictions for automated (or even partially automated) SMS and dialing operations. (Imagine if the game instead dialed 911 at a particular time of day)
Your best bet is to set up an intermediate server on the internet that uses an online SMS sending service and send the SMS via that route if you need complete automation. (ie, your program on the iPhone sends a UDP packet to your server, which sends the real SMS)
iOS 4 Update
iOS 4, however, now provides a viewController you can import into your application. You prepopulate the SMS fields, then the user can initiate the SMS send within the controller. Unlike using the "SMS:..." url format, this allows your application to stay open, and allows you to populate both the to and the body fields. You can even specify multiple recipients.
This prevents applications from sending automated SMS without the user explicitly aware of it. You still cannot send fully automated SMS from the iPhone itself, it requires some user interaction. But this at least allows you to populate everything, and avoids closing the application.
The MFMessageComposeViewController class is well documented, and tutorials show how easy it is to implement.
iOS 5 Update
iOS 5 includes messaging for iPod touch and iPad devices, so while I've not yet tested this myself, it may be that all iOS devices will be able to send SMS via MFMessageComposeViewController. If this is the case, then Apple is running an SMS server that sends messages on behalf of devices that don't have a cellular modem.
iOS 6 Update
No changes to this class.
iOS 7 Update
You can now check to see if the message medium you are using will accept a subject or attachments, and what kind of attachments it will accept. You can edit the subject and add attachments to the message, where the medium allows it.
iOS 8 Update
No changes to this class.
iOS 9 Update
No changes to this class.
iOS 10 Update
No changes to this class.
iOS 11 Update
No significant changes to this class
Limitations to this class
Keep in mind that this won't work on phones without iOS 4, and it won't work on the iPod touch or the iPad, except, perhaps, under iOS 5. You must either detect the device and iOS limitations prior to using this controller, or risk restricting your app to recently upgraded 3G, 3GS, and 4 iPhones.
However, an intermediate server that sends SMS will allow any and all of these iOS devices to send SMS as long as they have internet access, so it may still be a better solution for many applications. Alternately, use both, and only fall back to an online SMS service when the device doesn't support it.
Here is a tutorial which does exactly what you are looking for: the MFMessageComposeViewController.
http://blog.mugunthkumar.com/coding/iphone-tutorial-how-to-send-in-app-sms/
Essentially:
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"SMS message here";
controller.recipients = [NSArray arrayWithObjects:#"1(234)567-8910", nil];
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
And a link to the docs.
https://developer.apple.com/documentation/messageui/mfmessagecomposeviewcontroller
You must add the MessageUI.framework to your Xcode project
Include an #import <MessageUI/MessageUI.h> in your header file
Add these delegates to your header file MFMessageComposeViewControllerDelegate & UINavigationControllerDelegate
In your IBAction method declare instance of MFMessageComposeViewController say messageInstance
To check whether your device can send text use [MFMessageComposeViewController canSendText] in an if condition, it'll return Yes/No
In the if condition do these:
First set body for your messageInstance as:
messageInstance.body = #"Hello from Shah";
Then decide the recipients for the message as:
messageInstance.recipients = [NSArray arrayWithObjects:#"12345678", #"87654321", nil];
Set a delegate to your messageInstance as:
messageInstance.messageComposeDelegate = self;
In the last line do this:
[self presentModalViewController:messageInstance animated:YES];
You can use a sms:[target phone number] URL to open the SMS application, but there are no indications on how to prefill a SMS body with text.
One of the systems of inter-process communication in MacOS is XPC. This system layer has been developed for inter-process communication based on the transfer of plist structures using libSystem and launchd. In fact, it is an interface that allows managing processes via the exchange of such structures as dictionaries. Due to heredity, iOS 5 possesses this mechanism as well.
You might already understand what I mean by this introduction. Yep, there are system services in iOS that include tools for XPC communication. And I want to exemplify the work with a daemon for SMS sending. However, it should be mentioned that this ability is fixed in iOS 6, but is relevant for iOS 5.0—5.1.1. Jailbreak, Private Framework, and other illegal tools are not required for its exploitation. Only the set of header files from the directory /usr/include/xpc/* are needed.
One of the elements for SMS sending in iOS is the system service com.apple.chatkit, the tasks of which include generation, management, and sending of short text messages. For the ease of control, it has the publicly available communication port com.apple.chatkit.clientcomposeserver.xpc. Using the XPC subsystem, you can generate and send messages without user's approval. 
Well, let's try to create a connection.
xpc_connection_t myConnection;
dispatch_queue_t queue = dispatch_queue_create("com.apple.chatkit.clientcomposeserver.xpc", DISPATCH_QUEUE_CONCURRENT);
myConnection = xpc_connection_create_mach_service("com.apple.chatkit.clientcomposeserver.xpc", queue, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED);
Now we have the XPC connection myConnection set to the service of SMS sending. However, XPC configuration provides for creation of suspended connections —we need to take one more step for the activation.
xpc_connection_set_event_handler(myConnection, ^(xpc_object_t event){
xpc_type_t xtype = xpc_get_type(event);
if(XPC_TYPE_ERROR == xtype)
{
NSLog(#"XPC sandbox connection error: %s\n", xpc_dictionary_get_string(event, XPC_ERROR_KEY_DESCRIPTION));
}
// Always set an event handler. More on this later.
NSLog(#"Received a message event!");
});
xpc_connection_resume(myConnection);
The connection is activated. Right at this moment iOS 6 will display a message in the telephone log that this type of communication is forbidden. Now we need to generate a dictionary similar to xpc_dictionary with the data required for the message sending.
NSArray *recipient = [NSArray arrayWithObjects:#"+7 (90*) 000-00-00", nil];
NSData *ser_rec = [NSPropertyListSerialization dataWithPropertyList:recipient format:200 options:0 error:NULL];
xpc_object_t mydict = xpc_dictionary_create(0, 0, 0);
xpc_dictionary_set_int64(mydict, "message-type", 0);
xpc_dictionary_set_data(mydict, "recipients", [ser_rec bytes], [ser_rec length]);
xpc_dictionary_set_string(mydict, "text", "hello from your application!");
Little is left: send the message to the XPC port and make sure it is delivered.
xpc_connection_send_message(myConnection, mydict);
xpc_connection_send_barrier(myConnection, ^{
NSLog(#"The message has been successfully delivered");
});
That's all. SMS sent.
Add the MessageUI.Framework and use the following code
#import <MessageUI/MessageUI.h>
And then:
if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *messageComposer =
[[MFMessageComposeViewController alloc] init];
NSString *message = #"Your Message here";
[messageComposer setBody:message];
messageComposer.messageComposeDelegate = self;
[self presentViewController:messageComposer animated:YES completion:nil];
}
and the delegate method -
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result {
[self dismissViewControllerAnimated:YES completion:nil];
}
You can use this approach:
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:#"sms:MobileNumber"]]
iOS will automatically navigate from your app to the messages app's message composing page. Since the URL's scheme starts with sms:, this is identified as a type that is recognized by the messages app and launches it.
Follow this procedures
1 .Add MessageUI.Framework to project
2 . Import #import <MessageUI/MessageUI.h> in .h file.
3 . Copy this code for sending message
if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *messageComposer =
[[MFMessageComposeViewController alloc] init];
NSString *message = #"Message!!!";
[messageComposer setBody:message];
messageComposer.messageComposeDelegate = self;
[self presentViewController:messageComposer animated:YES completion:nil];
}
4 . Implement delegate method if you want to.
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
///your stuff here
[self dismissViewControllerAnimated:YES completion:nil];
}
Run And GO!
//Add the Framework in .h file
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
//Set the delegate methods
UIViewController<UINavigationControllerDelegate,MFMessageComposeViewControllerDelegate>
//add the below code in .m file
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
MFMessageComposeViewController *controller =
[[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
NSString *str= #"Hello";
controller.body = str;
controller.recipients = [NSArray arrayWithObjects:
#"", nil];
controller.delegate = self;
[self presentModalViewController:controller animated:YES];
}
}
- (void)messageComposeViewController:
(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result
{
switch (result)
{
case MessageComposeResultCancelled:
NSLog(#"Cancelled");
break;
case MessageComposeResultFailed:
NSLog(#"Failed");
break;
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
Here is the Swift version of code to send SMS in iOS. Please noted that it only works in real devices. Code tested in iOS 7+. You can read more here.
1) Create a new Class which inherits MFMessageComposeViewControllerDelegate and NSObject:
import Foundation
import MessageUI
class MessageComposer: NSObject, MFMessageComposeViewControllerDelegate {
// A wrapper function to indicate whether or not a text message can be sent from the user's device
func canSendText() -> Bool {
return MFMessageComposeViewController.canSendText()
}
// Configures and returns a MFMessageComposeViewController instance
func configuredMessageComposeViewController(textMessageRecipients:[String] ,textBody body:String) -> MFMessageComposeViewController {
let messageComposeVC = MFMessageComposeViewController()
messageComposeVC.messageComposeDelegate = self // Make sure to set this property to self, so that the controller can be dismissed!
messageComposeVC.recipients = textMessageRecipients
messageComposeVC.body = body
return messageComposeVC
}
// MFMessageComposeViewControllerDelegate callback - dismisses the view controller when the user is finished with it
func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
}
2) How to use this class:
func openMessageComposerHelper(sender:AnyObject ,withIndexPath indexPath: NSIndexPath) {
var recipients = [String]()
//modify your recipients here
if (messageComposer.canSendText()) {
println("can send text")
// Obtain a configured MFMessageComposeViewController
let body = Utility.createInvitationMessageText()
let messageComposeVC = messageComposer.configuredMessageComposeViewController(recipients, textBody: body)
// Present the configured MFMessageComposeViewController instance
// Note that the dismissal of the VC will be handled by the messageComposer instance,
// since it implements the appropriate delegate call-back
presentViewController(messageComposeVC, animated: true, completion: nil)
} else {
// Let the user know if his/her device isn't able to send text messages
self.displayAlerViewWithTitle("Cannot Send Text Message", andMessage: "Your device is not able to send text messages.")
}
}
There is a class in iOS 4 which supports sending messages with body and recipents from your application. It works the same as sending mail. You can find the documentation here: link text
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
UIImage *ui =resultimg.image;
pasteboard.image = ui;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"sms:"]];
}
//call method with name and number.
-(void)openMessageViewWithName:(NSString*)contactName withPhone:(NSString *)phone{
CTTelephonyNetworkInfo *networkInfo=[[CTTelephonyNetworkInfo alloc]init];
CTCarrier *carrier=networkInfo.subscriberCellularProvider;
NSString *Countrycode = carrier.isoCountryCode;
if ([Countrycode length]>0) //Check If Sim Inserted
{
[self sendSMS:msg recipientList:[NSMutableArray arrayWithObject:phone]];
}
else
{
[AlertHelper showAlert:#"Message" withMessage:#"No sim card inserted"];
}
}
//Method for sending message
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSMutableArray *)recipients{
MFMessageComposeViewController *controller1 = [[MFMessageComposeViewController alloc] init] ;
controller1 = [[MFMessageComposeViewController alloc] init] ;
if([MFMessageComposeViewController canSendText])
{
controller1.body = bodyOfMessage;
controller1.recipients = recipients;
controller1.messageComposeDelegate = self;
[self presentViewController:controller1 animated:YES completion:Nil];
}
}
If you want, you can use the private framework CoreTelephony which called CTMessageCenter class. There are a few methods to send sms.
Use this:
- (void)showSMSPicker
{
Class messageClass = (NSClassFromString(#"MFMessageComposeViewController"));
if (messageClass != nil) {
// Check whether the current device is configured for sending SMS messages
if ([messageClass canSendText]) {
[self displaySMSComposerSheet];
}
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
//feedbackMsg.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MessageComposeResultCancelled:
{
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:#"Message" message:#"SMS sending canceled!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert1 show];
[alert1 release];
}
// feedbackMsg.text = #"Result: SMS sending canceled";
break;
case MessageComposeResultSent:
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Message" message:#"SMS sent!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert2 show];
[alert2 release];
}
// feedbackMsg.text = #"Result: SMS sent";
break;
case MessageComposeResultFailed:
{
UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:#"Message" message:#"SMS sending failed!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert3 show];
[alert3 release];
}
// feedbackMsg.text = #"Result: SMS sending failed";
break;
default:
{
UIAlertView *alert4 = [[UIAlertView alloc] initWithTitle:#"Message" message:#"SMS not sent!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert4 show];
[alert4 release];
}
// feedbackMsg.text = #"Result: SMS not sent";
break;
}
[self dismissModalViewControllerAnimated: YES];
}
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:#"sms:number"]]
This would be the best and short way to do it.
You can present MFMessageComposeViewController, which can send SMS, but with user prompt(he taps send button). No way to do that without user permission. On iOS 11, you can make extension, that can be like filter for incoming messages , telling iOS either its spam or not. Nothing more with SMS cannot be done
You need to use the MFMessageComposeViewController if you want to show creating and sending the message in your own app.
Otherwise, you can use the sharedApplication method.

Resources