iPhone应用程序退出“没有安装SIM卡”

问题描述:

我使用MFMessageComposeViewController发送应用程序短信。在iPhone 4.0中,如果没有SIM卡,应用程序将退出。它只是弹出消息“没有安装SIM卡”。 委托回调MessageComposeResultSent。但是应用程序退出。有没有办法阻止它退出?或者如何检查手机中是否有SIM卡?iPhone应用程序退出“没有安装SIM卡”

/* 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]; 
} 
+0

我GUSS它是一个非常普遍的问题。不过,我已经添加了代码。我接受了我认为可以接受的所有答案。可能是我总是问奇怪的问题! – karim 2010-10-08 12:47:32

+0

当应用程序退出时,它是否会抛出异常,或者由于访问不良而崩溃?当你在调试器中运行它(带有异常断点和NSZombies)时,它停在哪里? – 2010-10-08 15:34:09

+0

在应用程序委托“applicationWillResignActive”被调用来显示警告消息“没有安装SIM卡”。所以该应用程序背景。调试器正常结束。 – karim 2010-10-08 16:00:48

周围的工作我现在用的就是,一个标志,在应用程序的委托,

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

在短信发送视图控制器,

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

代码段

并再次设置标志,当在SMS发送视图控制器时,

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

} 

查出安装SIM卡或不使用下面的代码:

@import CoreTelephony; 


CTTelephonyNetworkInfo *networkInfo = [CTTelephonyNetworkInfo new]; 
CTCarrier *carrier = [networkInfo subscriberCellularProvider]; 
if (!carrier.isoCountryCode) { 
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"No SIM Card Installed" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
    [alert show]; 
} 
else{ 
//Paste Your code here 
}