2010-10-08 5 views
1

J'utilise le MFMessageComposeViewController pour envoyer des SMS. dans l'iPhone 4.0, s'il n'y a pas de carte SIM, l'application se ferme. cela donne juste un message contextuel "aucune carte sim installée". Le rappel du délégué MessageComposeResultSent. Mais l'application se termine. Est-il possible de l'empêcher de sortir? ou comment vérifier s'il y a une carte SIM dans le téléphone?sortie de l'application iphone avec "Aucune carte SIM installée"

extraits de code ci-dessous:

/* 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

Je Guss est un problème très commun. Cependant, j'ai ajouté le code. J'ai accepté toutes les réponses qui me semblent acceptables. Peut-être que je pose toujours des questions bizarres! – karim

+0

Lorsque l'application se termine, lance-t-elle une exception ou se bloque-t-elle en raison d'un accès incorrect? Lorsque vous l'exécutez dans le débogueur (avec les points d'arrêt d'exception et les NSZombies activés), où s'arrête-t-il? –

+0

Dans le délégué de l'application "applicationWillResignActive" est appelé pour afficher le message d'alerte "Aucune carte SIM installée". Donc, l'application va en arrière-plan. Le débogueur se termine normalement. – karim

Répondre

0

Le travail autour Je me sers maintenant, un drapeau dans le délégué de l'application,

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

Dans le contrôleur de vue l'envoi de SMS,

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

Et définir à nouveau le drapeau, lorsque dans le contrôleur de vue d'envoi SMS,

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

} 
1

Pour détecter Sim Card est installé ou ne pas utiliser le code suivant:

@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 
} 
Questions connexes