![](/img/trans.png)
[英]iOS 7/8 - Detecting iOS Messaging App Opening, and if it has been sending SMS
[英]Detecting that SMS failed to be sent on 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的意思是“用戶成功排隊或發送了消息”。 這意味着,我期望的行為是正確的。
那么,如何知道我的上一條短信發送成功還是發送失敗呢?
您可以驗證設備是否被允許通過發送文本消息canSendText
的方法MFMessageComposeViewController
。
當您關心發送消息時,請在下面的代碼中添加此方法(此方法檢測到您的設備不支持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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.