2016-01-20 87 views
1

在我的應用程序中,瞭解短信是否已發送非常重要。對於檢查中,我使用這個委託方法:檢測到在iOS上發送的短信失敗

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{ 

    switch (result) { 
     case MessageComposeResultCancelled: { 
      [NSThread detachNewThreadSelector:@selector(SMScancelled) toTarget:self withObject:nil]; 
     } 
      break; 
     case MessageComposeResultSent: { 
      [NSThread detachNewThreadSelector:@selector(SMSsent) toTarget:self withObject:nil]; 
     } 
      break; 
     case MessageComposeResultFailed: { 
      [NSThread detachNewThreadSelector:@selector(SMSfailed) toTarget:self withObject:nil]; 
     } 
      break; 
     default: 
      break; 
    } 

    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

我的問題是,在測試時,我在設置中開啓飛行模式(測試會發生什麼事),然後我想發送短信(使用我應用程序)。當然,iOS無法發送,系統通知我。在消息應用程序中,還顯示我沒有發送它。但委託方法仍然返回MessageComposeResultSent而不是MessageComposeResultFailed。當我在另一個沒有SIM卡的手機上測試時,也會發生這種情況。

我在iOS 7和iOS 8.

在文檔測試這一點,有被寫入時,即MessageComposeResultSent的意思是「用戶成功排隊或發送的消息」。這意味着,我期待的行爲是正確的。

那麼如何知道,我的最後一條短信是否已經成功發送,或者發送失敗了?

回答

1

通過使用MFMessageComposeViewControllercanSendText方法,您可以驗證設備是否被允許發送短信。

當你關心發送消息添加該代碼如下(此方法的設備的檢測不支持SMS)

if(![MFMessageComposeViewController canSendText]) { 
    UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your device doesn't support SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [warningAlert show]; 
    return; 
} 

您可以檢查郵件使用的委託方法失敗MFMessageComposeViewController

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result 
{ 
    switch (result) { 
     case MessageComposeResultCancelled: 
     break; 

     case MessageComposeResultFailed: 
     { 
      UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to send SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [warningAlert show]; 
      break; 
     } 

     case MessageComposeResultSent: 
      break; 

     default: 
      break; 
    } 

    [self dismissViewControllerAnimated:YES completion:nil]; 
} 
+0

謝謝,我知道canSendText的方法,但僅此而已。我可以在打開MFMessageComposeView之前測試它,它可以返回TRUE,但是當用戶嘗試發送SMS時,設備可能會丟失信號並且無法發送。 我正在使用messageComposeViewController:didFinishWithResult,但其行爲不是我所需要的(它在答案中提到)。 – Reconquistador

+0

然後你想要什麼?如果設備丟失信號,那麼它將返回'MessageComposeResultFailed' –

+0

如果這是真的,當我嘗試在飛行模式打開時發送MessageComposeResultSent,爲什麼它會返回?或者是這種不同的行爲而不是放鬆信號? – Reconquistador