iphone app退出并显示“未安装SIM卡”

时间:2010-10-08 09:34:55

标签: iphone sms

我使用MFMessageComposeViewController在App中发送短信。在iPhone 4.0中,如果没有SIM卡,该应用程序将退出。它只是弹出一条消息“没有安装SIM卡”。 委托回调MessageComposeResultSent。但应用程序退出。有没有办法阻止它退出?或者如何检查手机中是否有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];
}

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

我现在正在使用的工作是,app代理中的一个标志,

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

在SMS发送视图控制器中,

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

}