【问题标题】:iphone app exit with "No SIM card installed"iphone 应用程序退出并显示“未安装 SIM 卡”
【发布时间】:2015-07-27 16:42:10
【问题描述】:

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

下面的代码sn-ps:

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

【问题讨论】:

  • 我猜这是一个非常普遍的问题。但是,我已经添加了代码。我接受了所有我认为可以接受的答案。可能我总是问奇怪的问题!
  • 应用退出时,是抛出异常,还是因为访问不正确而崩溃?当您在调试器中运行它时(带有异常断点和 NSZombies),它会在哪里停止?
  • 在应用程序委托中调用“applicationWillResignActive”以显示警报消息“未安装 SIM 卡”。所以应用程序进入后台。调试器正常终止。

标签: iphone sms


【解决方案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
}

【讨论】:

    【解决方案2】:

    我现在使用的解决方法是,应用程序委托中的一个标志,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-30
      相关资源
      最近更新 更多