tags:

views:

41

answers:

1

I use the MFMessageComposeViewController for sending in App sms. in iPhone 4.0, if there is no SIM card, the app exits. it just gives a pop up message "no sim card installed". The delegate callback MessageComposeResultSent. But application exits. Is there any way to prevent it from exiting? or how to check if there is any SIM card in the phone?

Code snippets below:

    /* Open the system sms service, copying the sms text in system clipboard. */
- (void) sendSMSAsURLRequest {
    NSString *phoneNumber = friend.phoneMobile;
    UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
    NSString *textUTIType = (NSString *)kUTTypeUTF8PlainText; // add MobileCoreServices.framework for this type.
    [pasteBoard setValue:[self buildSMSText] forPasteboardType:textUTIType];
    NSString *urlString = [NSString stringWithFormat:@"sms:%@", phoneNumber];
    NSURL *url = [[NSURL alloc] initWithString: urlString];
    [[UIApplication sharedApplication] openURL: url];
    [url release];
}

-(void) sendInAppSMS {
    MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
    controller.delegate = self;
    if([MFMessageComposeViewController canSendText])
    {
        NSString *smsText = [self buildSMSText];
        controller.body = smsText;
        controller.recipients = [NSArray arrayWithObjects:friend.phoneMobile, nil];
        controller.messageComposeDelegate = self;        
        [self presentModalViewController:controller animated:YES];
    }
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    switch (result) {
        case MessageComposeResultCancelled:
            NSLog(@"Cancelled");
            break;
        case MessageComposeResultFailed:{
            NSString *alertString = NSLocalizedString(@"Unknown Error. Failed to send message", @"");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:alertString delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
            [alert release];
            break;
        }
        case MessageComposeResultSent:
            NSLog(@"SMS sent");
            break;
        default:
            break;
    }    
    [self dismissModalViewControllerAnimated:YES];
}
A: 

The work around I am using now is, a flag in the app delegate,

- (void)applicationWillResignActive:(UIApplication *)aNotification {
    if (shouldExitApp) {
        exit(0);
    }
}

In the SMS sending view controller,

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    ((LuupAppDelegate *)[[UIApplication sharedApplication] delegate]).shouldExitApp = NO;

And set the flag again, when in the SMS sending view controller,

- (void) viewDidAppear:(BOOL)animated {
    ((LuupAppDelegate *)[[UIApplication sharedApplication] delegate]).shouldExitApp = YES;
    [super viewDidAppear:animated];

}
karim