2010-10-08 41 views
1

我使用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

回答

0

周圍的工作我現在用的就是,一個標誌,在應用程序的委託,

- (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]; 

} 
1

查出安裝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 
}