How to check if sim card is installed or not - iphone

I am developing an app which has call and message functionality , i want to check if sim card is installed or not coz i am facing problem with messaging as it gives alerts for " Message Sent Successful"
Please help me out.

There might be different ways but one way is by using MFMessageComposeViewController class to see if you can send the text message. If you can then sim is available otherwise not.
if ([MFMessageComposeViewController canSendText]) {
NSLog(#"SIM Available");
} else {
NSLog(#"no SIM card installed");
}
In cases you have iMessage available then this might return you true, you could also check if you can make a call, you might want to use CTTelephonyNetworkInfo for that purpose.

You can also check using like this.... First read this doc
http://developer.apple.com/library/ios/#DOCUMENTATION/NetworkingInternet/Reference/CTCarrier/Reference/Reference.html#//apple_ref/doc/uid/TP40009596-CH1-SW1
NSString *_code = [[[CTCarrier alloc] init] mobileCountryCode];
The value for this property is nil if any of the following apply:
The device is in Airplane mode.
There is no SIM card in the device.
The device is outside of cellular service range.

First you have to be sure that device is iPhone (not iPod or iPad) then check if device can make call or not, just like this............
if([[UIDevice currentDevice].model isEqualToString:#"iPhone"])
{
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"tel:123456"]])
{
NSLog(#"Device can make call or send message");
}
else
{
NSLog(#"Device can not make call or send message");
}
}
else
{
NSLog(#"Device can not make call or send message");
}
Hope it will help you........

Related

How can I get CTCallCenter Events in an iPhone Enterprise app in the background

I am looking for a way to get CTCallCenter Events on an enterprise app while it is in the background. I came across the suggested solution by OhadM on this post: How to get a call event using CTCallCenter:setCallEventHandler: that occurred while the app was suspended?, but I can't get it to work with the app in the background.
I created a new single view app, added a callCenter property on the appDelegate and set the callEventHandler as shown in the post and listed below. I also added the Voice over IP and Background fetch Background Modes Capabilities as described.
Is there any other settings or code not mentioned in the solution that may help? I am using iOS 9.3.1 and Xcode 7.3.1.
[self.callCenter setCallEventHandler:^(CTCall *call)
{
NSLog(#"Event handler called");
if ([call.callState isEqualToString: CTCallStateConnected])
{
NSLog(#"Connected");
}
else if ([call.callState isEqualToString: CTCallStateDialing])
{
NSLog(#"Dialing");
}
else if ([call.callState isEqualToString: CTCallStateDisconnected])
{
NSLog(#"Disconnected");
} else if ([call.callState isEqualToString: CTCallStateIncoming])
{
NSLog(#"Incomming");
}
}];

CTCallCenter is not giving disconnected state

I am using call functionality in my application. I want to call one API when call is disconnected. I am using following code in application didFinishLaunchingWithOptions.
callCenter = [[CTCallCenter alloc] init];
callCenter.callEventHandler = ^(CTCall* call) {
if (call.callState == CTCallStateDisconnected)
{
NSLog(#"Call has been disconnected===================");
}
else if (call.callState == CTCallStateConnected)
{
NSLog(#"Call has just been connected======================");
}
else if(call.callState == CTCallStateDialing)
{
NSLog(#"Call is dialing======================");
}
else
{
NSLog(#"None of the conditions===============");
}
};
This code is giving me correct state for very first time. i.e When I make call, it gives me call dialing and when call ends, it gives me disconnected state. But when I install same app again in device without deleting it, then it gives me dialing state. But when call ends, it do not give me disconnected state. Please help. Thank you.
'CTCallStateDisconnected' will be called only if you decline the GSM call. if you answer GSM call then 'CTCallStateConnected' will be called, here you have to set some flag saying GSM is connected once the GSM call ends then in your app applicationDidBecomeActive() using the flag you have to do whatever you want eg. resume calls.

iOS Native Facebook Share no internet callback

I'm trying to implement the native facebook share for iOS 6 and need check if a share did succeed or not. This is the code I have used:
BOOL displayedNativeDialog =
[FBNativeDialogs
presentShareDialogModallyFrom:delegate
initialText:#"test"
image:nil
url:nil
handler:^(FBNativeDialogResult result, NSError *error) {
if (error) {
/* handle failure */
NSLog(#"error:%#, %#", error, [error localizedDescription]);
} else {
if (result == FBNativeDialogResultSucceeded) {
/* handle success */
NSLog(#"handle success");
} else {
/* handle user cancel */
NSLog(#"user cancel");
}
}
}];
if (!displayedNativeDialog) {
/* handle fallback to native dialog */
}
My problem is when I try this with no internet connection available I still get the FBNativeDialogResultSucceeded
It looks like you should get an error when no internet connection is available but it seems that it doesn't work like that.
If there are some solution where I don't need to use the reachability SDK that would be great.
You'll likely have to use the reachability SDK at this point. The Facebook SDK builds on top of the SLComposeViewController for the native functionality. That view controller returns two possible choices:
SLComposeViewControllerResultCancelled
SLComposeViewControllerResultDone
SLComposeViewControllerResultDone: The view controller is dismissed and the message is being sent in the background. This occurs when the user selects Done.
So since Facebook mirrors this the success case means the user clicked done and the message has been sent in the background.
However if you run this and there is no internet connection, the user should still see a pop-up indicating that the post could not be sent due to a connection failure.

iPhone app, alert users when they aren't connected to the internet?

What's an easy way to code an alert that warns users when they aren't connected to the Internet? I'm using Xcode, right now when there is no connection it is just a white screen in uiwebview.
Here you can check if wifi has connection, 3g,or none atall:
if ([[Reachability reachabilityWithHostName:#"google.com"] currentReachabilityStatus] == ReachableViaWiFi) {
// Do something that requires wifi
} else if ([[Reachability reachabilityWithHostName:#"google.com"] currentReachabilityStatus] == ReachableViaWWAN) {
// Do something that doesnt require wifi
} else if ([[Reachability reachabilityWithHostName:#"google.com"] currentReachabilityStatus] == NotReachable) {
// Show alert because no wifi or 3g is available..
}
Apple provides all the necessary Reachability api/source here: Reachability Reference
I have made these custom convenience functions for myself in all my projects:
+ (BOOL)getConnectivity {
return [[Reachability reachabilityWithHostName:#"google.com"] currentReachabilityStatus] != NotReachable;
}
+ (BOOL)getConnectivityViaWiFiNetwork {
return [[Reachability reachabilityWithHostName:#"google.com"] currentReachabilityStatus] == ReachableViaWiFi;
}
+ (BOOL)getConnectivityViaCarrierDataNetwork {
return [[Reachability reachabilityWithHostName:#"google.com"] currentReachabilityStatus] == ReachableViaWWAN;
}
And used like so:
if ([ServerSupport getConnectivity]) {
// do something that requires internet...
else {
// display an alert
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Network Unavailable"
message:#"App content may be limited without a network connection!"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil] autorelease];
[alert show];
}
If your application requires persistent internet connection, you can include the key UIRequiresPersistentWiFi in your plist file. Using this, iOS will automatically display an alert to the user in the airport mode.
Also, iOS will ensure that the wifi radio isn't switched off/hibernated while your app is in the foreground.
Note that internet connectivity is very dynamic on a mobile device (multiple radios, multiple access points, multiple cell towers, travel, competing radio interference, "your holding it wrong", etc.), and thus Reachability is not a perfect solution, as the net connectivity can very often can change just before, during or after Reachability performs its checks. Reachability can outright lie (yes we're connected to an access point, true even if broadband is dead on the other side of the access point). It's also quite common for Reachability to report no connectivity, just as its own request turns the radios on to get a great connection a couple seconds later.
Best thing to do is to just try to access data from the network, spin an activity indicator while waiting, and offer the user some API element to give up if they end up waiting too long, in their opinion, not some developer's opinion... They know better than you do how long it takes for a (re)connection to come up in their area, and how long they are willing to wait.

Check if iPhone can send texts w/ MFMessageComposeViewController

I am building an app that takes advantage of 4.0 features like in-app messaging but I also want my app to work on all 3.x versions. How do I check if the device can use MFMessageComposeViewController?
Apple advises to use the NSClassFromString function to determine if the class is available. Don't forgot to weak link against the message framework as mentioned before.
Example from the MessageComposer sample:
-(IBAction)showSMSPicker:(id)sender {
// The MFMessageComposeViewController class is only available in iPhone OS 4.0 or later.
// So, we must verify the existence of the above class and log an error message for devices
// running earlier versions of the iPhone OS. Set feedbackMsg if device doesn't support
// MFMessageComposeViewController API.
Class messageClass = (NSClassFromString(#"MFMessageComposeViewController"));
if (messageClass != nil) {
// Check whether the current device is configured for sending SMS messages
if ([messageClass canSendText]) {
[self displaySMSComposerSheet];
}
else {
feedbackMsg.hidden = NO;
feedbackMsg.text = #"Device not configured to send SMS.";
}
}
else {
feedbackMsg.hidden = NO;
feedbackMsg.text = #"Device not configured to send SMS.";
}
}
Link: MessageComposer sample
Update: The sample code I posted checks for the availability of the SMS composer, checking for the mail composer is similar.
Try this:
#import <MessageUI/MessageUI.h>
#include <dlfcn.h>
if ( dlsym(RTLD_DEFAULT, "MFMailComposeErrorDomain") != NULL ) {
// MFMessageComposeViewController framework is available
} else {
// do alternative for no MFMessageComposeViewController
}
Don't forget to weak link the MFMessageUI framework.
You could check the device's current operating system using the UIDevice class.
http://developer.apple.com/iphone/library/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html
The systemName and systemVersion properties may be what you're looking for. You'll also need to weak link the framework!