Problems with Reachability in iOS 5.1? - iphone

We were using Apple's Reachability a few months ago to check for the network status of the iPad. It worked before. Now when I try to test for it, I always get network unreachable and am wondering why. Any thoughts on this? Here's some of my code:
// In the class I want to check for reachability to send email from, I access my singleton object WebServiceManager
WebServiceManager *wmgr = [WebServiceManager sharedInstance];
[wmgr CheckNetworkStatus];
if (wmgr.isInternetAvailable) {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[self presentModalViewController:mailComposer animated:YES];
}
else {
UIAlertView *mailNotConfiguredAlert = [[UIAlertView alloc] initWithTitle:#"This device is not configured to send mail" message:#"Please set up your mail account in order to send an email." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[mailNotConfiguredAlert show];
[mailNotConfiguredAlert release];
}
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Network Connection Unavailable" message:#"The network connection is currently unavailable. Please try again later." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
}
// In WebServiceManager
- (void)CheckNetworkStatus {
NetworkStatus internetStatus = [_networkReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
isInternetAvailable = NO;
break;
}
case ReachableViaWiFi:
{
isInternetAvailable = YES;
break;
}
case ReachableViaWWAN:
{
isInternetAvailable = YES;
break;
}
}
}

Probably it's not Reachability, we're using it in 5.1 at the moment and it doesn't produce any weird behavior.
Check if your boolean property isInternetAvailable is being set properly (probably some NSLog debugging would help).

Related

MFMessageController crash sometimes

I have the concept of sharing to contacts in my App and used MFMessageComposeViewController.
-(IBAction)btnAddClicked:(id)sender {
#try {
selections = [[NSMutableArray alloc] init];
for(NSIndexPath *indexPath in arSelectedRows) {
NSMutableDictionary *searchable = [[NSMutableDictionary alloc
]init];
[searchable setObject:[[contactsArray objectAtIndex:indexPath.row]objectForKey:#"Phone"]forKey:#"Phone"];
[selections addObject:searchable];
}
if([selections count]>0)
{
NSString *temp1=#"";
for(int i=0;i<[selections count];i++)
{
toRecepients=[[selections objectAtIndex:i]objectForKey:#"Phone"];
temp1=[temp1 stringByAppendingString:toRecepients];
temp1=[temp1 stringByAppendingString:#","];
}
temp1 = [temp1 substringToIndex:[temp1 length]-1];
if(![MFMessageComposeViewController canSendText]) {
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Your device doesn't support SMS!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
return;
}
NSArray *recipents = [temp1 componentsSeparatedByString:#","];
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
messageController.navigationBar.topItem.leftBarButtonItem.title = #"Cancel";
[messageController setRecipients:recipents];
[messageController setBody:self.message];
[self presentModalViewController:messageController animated:YES];
}
else{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Select the contacts you would like to share to" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
return;
}
}
#catch (NSException *exception) {
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
{
ErrorView *errorView=[[ErrorView alloc]initWithNibName:#"ErrorView" bundle:nil];
if([[[UIDevice currentDevice]systemVersion]floatValue]<5.0)
{
[self presentModalViewController:errorView animated:YES];
}
else
{
[self presentViewController:errorView animated:YES completion:nil];
}
[errorView release];
}
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {
ErrorView *errorView=[[ErrorView alloc]initWithNibName:#"ErrorView~iPad" bundle:nil];
if([[[UIDevice currentDevice]systemVersion]floatValue]<5.0)
{
[self presentModalViewController:errorView animated:YES];
}
else
{
[self presentViewController:errorView animated:YES completion:nil];
}
[errorView release];
}
} }
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result {
switch (result) {
case MessageComposeResultCancelled:
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Failed to send SMS!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
break;
}
case MessageComposeResultFailed:
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Failed to send SMS!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
break;
}
case MessageComposeResultSent:
{
NSString *messa=[NSString stringWithFormat:#"Shared to %lu contact(s)",(unsigned long)[selections count]];
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Succesful" message:messa delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
break;
}
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
This is the code Im using, which crashes sometimes and displays the error
Assertion failed: (result == KERN_SUCCESS), function +[XPCMachSendRight wrapSendRight:], file /SourceCache/XPCObjects/XPCObjects-46/XPCMachSendRight.m, line 27.
I have put breakpoint to debug, but xcode doesn't showup where the error is produced.
Any ideas/ suggestions would be appreciable..
Enable zombie Objects to find out the line of actual crash.
You can enable Zombie by following steps:
1.Select you project scheme and chose edit scheme.
2.A window will appear, now select diagnostics.
3.Select check mark for enable Zombie objects.
Now run your project.

Reachability says no coverage when app is installed from Store or TestFlight

I'm having this really weird issue with Reachability on iOS. If I run my app in debug on a device there's no problem at all, the app runs fine. But when I install it from the store or from TestFlight I get my No Coverage error even if I'm on Wi-Fi, but only when I try to do certain actions. If I don't do that specific action the app runs fine until I do.
This is the part of my code that deals with Reachability:
- (void)connectionReachabilityChanged:(NSNotification *)notice {
NetworkStatus status = [self.connectionReachability currentReachabilityStatus];
if (status == NotReachable) {
self.inCoverage = NO;
} else {
self.inCoverage = YES;
}
}
- (void)hostReachabilityChanged:(NSNotification *)notice {
NetworkStatus status = [self.hostReachability currentReachabilityStatus];
if (status == NotReachable) {
self.inCoverage = NO;
} else {
self.inCoverage = YES;
}
}
- (void)displayAlertOfType:(AlertType)type {
if (type == AlertTypeNoCoverage) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"No coverage"
message: #"You current have no data coverage, try again later"
delegate: self
cancelButtonTitle: #"OK"
otherButtonTitles: nil];
[alert show];
}
if (type == AlertTypeOperationNotCompleted) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Whoops... Something went wrong"
message:#"The operation couldn't be completed, try again later"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
Reachability *reachability = [Reachability sharedReachability];
[reachability setHostName:#"http://www.google.com/"];
NetworkStatus remoteHostStatus = [reachability remoteHostStatus];
if(remoteHostStatus == NotReachable) {
//no internet connection
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"No coverage"
message: #"You current have no data coverage, try again later"
delegate: self
cancelButtonTitle: #"OK"
otherButtonTitles: nil];
[alert show];
}
else if (remoteHostStatus == ReachableViaWiFiNetwork) {
//wifi connection found
}
else if (remoteHostStatus == ReachableViaCarrierDataNetwork) {
//EDGE or 3G connection found
}

iPhone iOS 5 SDK in app SMS error

thanks in advance every time I call to create an in app SMS i get the following error...
Application tried to push a nil view controller on target .
This is the code I have that worked in OS 4 SDK...
MFMailComposeViewController *MailController;
MFMessageComposeViewController *SMScontroller;
NSError *error;
NSString *EmailMessage;
NSString *SubjectMessage;
-(IBAction)sendInAppSMS
{
if([MFMessageComposeViewController canSendText])
{
SMScontroller = [[MFMessageComposeViewController alloc] init];
SMScontroller.messageComposeDelegate = self;
NSString *MessageString = #"Hello";
SMScontroller.body = MessageString;
SMScontroller.navigationBar.tintColor = [UIColor blackColor];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
[self presentModalViewController:SMScontroller animated:YES];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
}
else{
// alertView to tell user to setup a mail account.
NSString *message = [[NSString alloc] initWithFormat:#"To use this feature, you need to have an iPhone with SMS abilities."];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"SMS Account Not Detected" message:message delegate:nil cancelButtonTitle:#"Understood" otherButtonTitles:nil];
[alert show];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result) {
case MessageComposeResultCancelled:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"I Like It" message:#"User cancelled sending the SMS"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
break;
case MessageComposeResultFailed:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"I Like It" message:#"Error occured while sending the SMS"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
break;
case MessageComposeResultSent:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"I Like It" message:#"SMS sent successfully..!"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
I believe if you are running this in the simulator you'll get the error since it doesn't have sms. I get the same error in simulator but if i connect to my phone it works fine.

MFMailComposeViewController navigation bar buttons are disabled

I use MFMailComposeViewController to send mail in my app. But when present mail compose view controller, all of navigation buttons are disabled (except back button in select mail address screen), i must use Home button to quit app. Does anyone has idea?
Here is screen shot:
Code:
- (void)shareVieEmail
{
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:#"Test subject"];
[mailViewController setMessageBody:#"Mail message body" isHTML:NO];
NSData *imageData = [NSData dataWithContentsOfFile:photourl];
[mailViewController addAttachmentData:imageData mimeType:#"image/jpg" fileName:#"example_photo"];
[self presentModalViewController:mailViewController animated:YES];
} else {
[[[UIAlertView alloc] initWithTitle:#"Cannot send mail" message:#"Device is unable to send email in its current state" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show];
}
}
Delegate method :
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
//NSLog(#"Result: canceled");
break;
case MFMailComposeResultSaved:
//NSLog(#"Result: saved");
break;
case MFMailComposeResultSent:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Result" message:#"Mail Sent Successfully" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
break;
case MFMailComposeResultFailed:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Result" message:#"Mail Sent Failed" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
break;
default:
//NSLog(#"Result: not sent");
break;
}
if (error) {
[[[UIAlertView alloc] initWithTitle:#"Cannot send mail" message:[NSString stringWithFormat:#"ERROR:%#", [error userInfo]] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show];
}
[self dismissModalViewControllerAnimated:YES];
}
And in header file, I declared implement MFMailComposeViewControllerDelegate.
I had exactly the same problem. and it took me a while to figure this out but no surprise it came down to customized UIBarButtonItem
I bet in your UIBarButtonItem.h there is a method
-(void)setEnabled:(BOOL)enabled ;
and the implementation looks like this:
-(void)setEnabled:(BOOL)enabled {
if (self.customView) {
if ([[self.customView.subviews objectAtIndex:0] isKindOfClass:[UIButton class]]) {
((UIButton*)[self.customView.subviews objectAtIndex:0]).enabled = enabled;
}
}
}
and this is causing problem so as soon as you comment out this method your problem should go away.
I also had this problem, but it my case it was because I had overridden setNavigationBarHidden:animated: from UINavigationController as proposed in this workaround for a bug in CNContactViewController. One solution that would still include the workaround and solve the problem in MFMailComposeViewController would be to use method swizzling to be able to call either the original method or the overridden one, depending on the class of the current topViewController.
In your MFMailComposeViewController's delegate you need to implement didFinishWithResult: and dismiss the modal view controller from there.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// you can test the result of the mail sending here if you want
[self dismissModalViewControllerAnimated:YES];
}
For swift 4.0+
func mailComposeController(controller: MFMailComposeViewController,
didFinishWithResult result: MFMailComposeResult, error: NSError?) {
// Check the result or perform other tasks.
// Dismiss the mail compose view controller.
controller.dismissViewControllerAnimated(true, completion: nil)
}

Trouble opening the MFMailComposeViewController on the device, works in simulator.

I am doing this its working in simulator but when we try to open in device then program is terminating.
Plz suggess me fast.
MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
mail.mailComposeDelegate=self;
[mail setToRecipients:[NSArray arrayWithObjects:#"marketing#realestateinvestar.com.au",nil]];
//[self becomeFirstResponder];
mail.navigationBar.tintColor=[UIColor blackColor];
[self presentModalViewController:mail animated:YES];
if ([MFMessageComposeViewController canSendText])
Your problem is here. You are trying to check whether device will be able to send Text messages and not email Message. you should try using
if([MFMailComposeViewController canSendMail])
The problem might be that your device is not configured to any accounts in the mail.Please check this once.
Have you implement MFMailComposeViewControllerdelegate methods in your code??
#pragma mark --------------------------------------------
#pragma mark MFMailComposeViewController delegate Methods
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultCancelled:
NSLog(#"Mail send canceled.");
/*
Execute your code for canceled event here ...
*/
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved.");
/*
Execute your code for email saved event here ...
*/
break;
case MFMailComposeResultSent: {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Mail Sent" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.tag = 1;
alert.delegate = self;
[alert show];
[alert release];
break;
}
case MFMailComposeResultFailed: {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Mail Sending Failed" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.tag = 2;
alert.delegate = self;
[alert show];
[alert release];
break;
}
default:
break;
}
[controller dismissModalViewControllerAnimated:YES];//dismissing modal view controller
}
Your code looks ok, but did you check if the device can send mail:
if ([MFMailComposeViewController canSendText]) {
MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
mail.mailComposeDelegate=self;
[mail setToRecipients:[NSArray arrayWithObjects:#"marketing#realestateinvestar.com.au",nil]];
mail.navigationBar.tintColor=[UIColor blackColor];
[self presentModalViewController:mail animated:YES];
[mail release], mail = nil;
} else {
// show message to the use that he can't send an email.
}