【问题标题】:MFMessageComposeViewController crashes in iOS 6MFMessageComposeViewController 在 iOS 6 中崩溃
【发布时间】:2014-05-21 05:11:02
【问题描述】:

我正在使用以下功能发送短信。

if([MFMessageComposeViewController canSendText])
{
     MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
    controller.body = @"SMS Text";

    controller.messageComposeDelegate = (id)self;
    [self presentViewController:controller animated:TRUE completion:nil];
}

它在 iOS 7 及更高版本中运行良好。但它在运行 6.0.1 的 iPod 中崩溃并出现错误

* * NSDictionary 中的断言失败 *_UIRecordArgumentOfInvocationAtIndex(NSInvocation *, NSUInteger, BOOL)(), /SourceCache/UIKit/UIKit-2372/UIAppearance.m:1118

2014-05-21 11:33:38.170 Project[764:4913] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“未知键,标题文本属性字典中的“NSColor”” * 首先抛出调用栈: (0x36c922a3 0x3346997f 0x36c9215d 0x383b430b 0x37717925 0x37716a7b 0x36c8f62f 0x377167f5 0x377f45e5 0x37782cd7 0x37782b6d 0x359d390f 0x37782a61 0x3778c0d5 0x359c683b 0x3778c0b1 0x359c611f 0x359c599b 0x359c5895 0x359d4215 0x359d43b9 0x36163a11 0x361638a4) libc++abi.dylib:终止调用抛出异常

任何帮助将不胜感激

【问题讨论】:

  • 你能调试一下它在哪一行崩溃。试试这个,[MFMessageComposeViewController canSendText]
  • 根据这个错误,尝试使用UITextAttributeTextColor而不是NSForegroundColorAttributeName

标签: ios sms


【解决方案1】:

可能的原因是您的选择器是nil。该代码仅适用于可以发送文本的设备。同样,您应该始终使用[MFMessageComposeViewController canSendText] 检查消息发送能力,然后才显示选择器。来自 Apple 的 MFMessageComposeViewController 文档:

Before presenting a message composition view, call the canSendText class method to ensure that the user’s device is appropriately configured. Do not attempt to present a message composition view if the canSendText method returns NO. If SMS delivery isn’t available, you can notify the user or simply disable the SMS features in your application.

Starting in iOS 5, you can register to be notified of changes to the availability of text message sending by way of the MFMessageComposeViewControllerTextMessageAvailabilityDidChangeNotification notification.

编辑: 使用UITextAttributeTextColor 而不是NSForegroundColorAttributeName,无论你在哪里设置NSForegroundColorAttributeName,在你的应用程序中使用UIAppearance 方法应该可以修复错误。

【讨论】:

  • 我已经修改了问题。但是我得到了同样的错误
  • 能否添加异常断点并运行应用程序?当崩溃发生时,程序应该显示导致崩溃的罪魁祸首。
  • 尝试使用 UITextAttributeTextColor 而不是 NSForegroundColorAttributeName 无论您在哪里设置 NSForegroundColorAttributeName 都可能使用应用程序中的 UIAppearance 方法
  • 不客气,如果您能将我的答案标记为正确并投票,那就太好了? :) 我会将我的评论添加到我的答案中作为编辑。
  • 完成,我添加了一个带有答案的编辑。
【解决方案2】:

您不能在 iPod 上使用 SMS,它仅适用于 iPhone。你应该这样做:-

 MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];

 if([MFMessageComposeViewController canSendText])
  {
   //TODO: your other code
    controller.body = @"SMS Text";
    controller.messageComposeDelegate = self;
    [self presentViewController:picker animated:YES completion:NULL];
  }

确保在视图控制器的标头上实现 MFMessageComposeViewControllerDelegate

以下链接也可能有帮助:-

  1. Weird MFMailComposeViewController Crash
  2. MFMailComposeViewController Crashes because of Global Appearance Properties on iOS6

【讨论】:

  • 我已经修改了问题。但是我得到了同样的错误
  • 为什么要将视图控制器转换为类型 ID?我刚刚更新了我的答案。
【解决方案3】:

试试这个:

if ([MFMessageComposeViewController canSendText])
// The device can send sms.
{
    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;

// You can specify one or more preconfigured recipients.  The user has
// the option to remove or add recipients from the message composer view
// controller.
// You can specify the initial message text that will appear in the message
// composer view controller.

picker.body = @"Hello Text";

[self presentViewController:picker animated:YES completion:NULL];
}

你检查过+[MFMessageComposeViewController canSendText]吗?

参考:

canSendText

Returns a Boolean value indicating whether the current device is capable of sending text messages. + (BOOL)canSendText

返回值

YES if the device can send text messages or NO if it cannot. Discussion

Always call this method before attempting to present the message compose view controller. A device may be unable to send messages if it does not support messaging or if it is not currently configured to send messages. This method applies only to the ability to send text messages via iMessage, SMS, and MMS.

To be notified of changes in the availability of sending text messages, register as an observer of the MFMessageComposeViewControllerTextMessageAvailabilityDidChangeNotification notification. Availability

Available in iOS 4.0 and later.
Declared In MFMessageComposeViewController.h

【讨论】:

  • 我已经修改了问题。但是我得到了同样的错误
  • 从代码中删除 presentViewController。分配后崩溃
  • 是的,你能把你修改的内容粘贴到这里吗?
  • 简单添加 if([MFMessageComposeViewController canSendText])
  • 根据您的错误,尝试使用UITextAttributeTextColor,而不是NSForegroundColorAttributeNameeverywhere。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多