iPhone iOS 5 SDK in app SMS error - ios5

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.

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.

Warning: Attempt to present <SLTwitterComposeViewController: 0x233141c0> on <MainController: 0x2083ebb0> whose view is not in the window hierarchy

I am receiving this error when trying to post an image to Twitter using the following code, cannot think what I am doing wrong any ideas for how to solve?;
- (void)PostToTwitter
{
UIImage* pxImage = [[self GetResultImage] retain];
TWTweetComposeViewController *twitter = [[[TWTweetComposeViewController alloc] init] autorelease];
[twitter setInitialText:#"Twitter Pic"];
[twitter addImage:pxImage];
FaceAppDelegate* app = (FaceAppDelegate*)[[UIApplication sharedApplication] delegate];
[app.mainCtrl presentViewController:twitter animated:YES completion:NULL];
twitter.completionHandler = ^(TWTweetComposeViewControllerResult res)
{
if(res == TWTweetComposeViewControllerResultDone)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Success" message:#"You have posted successfully." delegate:self cancelButtonTitle:#"Close" otherButtonTitles: nil];
[alert show];
[alert release];
}
else if(res == TWTweetComposeViewControllerResultCancelled)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Cancelled" message:#"You have cancelled posting to Twitter." delegate:NULL cancelButtonTitle:#"Close" otherButtonTitles: nil];
[alert show];
[alert release];
}
[app.mainCtrl dismissModalViewControllerAnimated:YES];
};
[pxImage release];
}
I think, that your view isn't presented. Try to use app.window.rootViewController, not property mainContr.

Problems with Reachability in iOS 5.1?

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).

Iphone mail application

It is possible open Iphone Mail application without compose new message?
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"mailto:%#?subject=My Subject", _recipient]]];
This code not suitable for me.
I need just open mail application on main screen.
Thanks
P.S Sorry for my English ))
-(void) EmailMethod
{
MFMailComposeViewController *picker=[[MFMailComposeViewController alloc]init];
picker.mailComposeDelegate = self;
[picker setToRecipients:#"mailid#mymail.com"];
[picker setSubject:#"Place your subject of mail here."];
[picker setMessageBody:#"Place your body of mail here." isHTML:YES];
[self presentModalViewController:picker animated:YES];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#"Canceled !!"
message:#"Mail sending cancelled." delegate:nil
cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[ErrorAlert show];
[ErrorAlert release];
break;
case MFMailComposeResultSaved:
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#"Saved"
message:#"Mail saved to Drafts." delegate:nil
cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[ErrorAlert show];
[ErrorAlert release];
break;
case MFMailComposeResultSent:
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#"Email Sent"
message:#"Thank you for recommending us to your friends via Email."
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[ErrorAlert show];
[ErrorAlert release];
break;
case MFMailComposeResultFailed:
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#"Error !!"
message:#"Failed to send mail." delegate:nil
cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[ErrorAlert show];
[ErrorAlert release];
break;
default:
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#"Error !!"
message:#"Failed to send mail." delegate:nil
cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[ErrorAlert show];
[ErrorAlert release];
break;
}
[self becomeFirstResponder];
[self dismissModalViewControllerAnimated:YES];
}

How should I properly format this code?

I've a small issue here. I am using an if statement with UIAlertView and I have two situations, both result in UIAlertViews. However, in one situation, I want to dismiss just the UIAlertView, the other, I want the UIAlertView to be dismissed and view to return to root view.
This code describes is:
if([serverOutput isEqualToString:#"login.true"]){
[Alert dismissWithClickedButtonIndex:0 animated:YES];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
UIAlertView *success = [[UIAlertView alloc] initWithTitle:#"Success" message:#"The transaction was a success!"
delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[success show];
[success release];
} else {
UIAlertView *failure = [[UIAlertView alloc] initWithTitle:#"Failure" message:#"The transaction failed. Contact sales operator!"
delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[failure show];
[failure release];
}
}
-(void)alertView: (UIAlertView *)success clickedButtonAtIndex: (NSInteger)buttonIndex{
switch(buttonIndex) {
case 0: {
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
}
So, in both cases, they follow the above action, but obviously, that's not what I want. Any ideas on what I do here?
You will have to differentiate between the 2 uialertview in your clickedButtonAtIndex: method.
Use the tag property to differentiate.
When you create the alerview assign a tag id to them:
UIAlertView *success = [[UIAlertView alloc] initWithTitle:#"Success" message:#"The transaction was a success!" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
success.tag = 1;
[success show];
Similarly,
failure.tag = 2;
Then you switch on the tag ids
switch(alertView.tag){
case 1: //dismiss alertview
case 2: //dismiss alertview and return to root view
}
You could paste your code in Eclipse, and press ctrl+i.