您好我正在使用MFMessageComposeViewController
在iPhone应用中发送消息。
由于这是一个iPhone应用程序,它也支持iPod。当点击消息按钮时,应用程序崩溃,因为iPod上没有消息。那么有没有办法检查设备是否是iPod,以便我可以隐藏消息按钮,这样用户就不会点击iPod中的消息而崩溃。
这是我用于消息传递的代码。
- (IBAction)Message:(id)sender
{
MFMessageComposeViewController *messaging=[[MFMessageComposeViewController alloc]init];
messaging.messageComposeDelegate=self;
[messaging setBody:@"Will of the People""\n""http://bit.ly/1gZhZye"];
[self presentViewController:messaging animated:YES completion:nil];
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissViewControllerAnimated:YES completion:^{UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Done" message:nil delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
[alert show];
}];
}
这似乎在iPhone中运行良好。当用户使用iPod时,我需要一种方法来禁用此按钮。
答案 0 :(得分:3)
您可以使用canSendText类方法执行此操作:
- (IBAction)Message:(id)sender
{
if ([MFMessageComposeViewController canSendText])
{
MFMessageComposeViewController *messaging=[[MFMessageComposeViewController alloc]init];
messaging.messageComposeDelegate=self;
[messaging setBody:@"Will of the People""\n""http://bit.ly/1gZhZye"];
[self presentViewController:messaging animated:YES completion:nil];
}
}
参考:
<强> canSendText 强>
返回一个布尔值,指示当前设备是否为 能够发送短信。
+ (BOOL)canSendText
返回值
如果设备可以发送短信,则为“是”,如果不能,则为“否”。 讨论
在尝试呈现消息之前始终调用此方法 撰写视图控制器。如果设备可能无法发送消息 不支持消息传递或者当前未配置消息传递 发送信息。此方法仅适用于发送文本的功能 通过iMessage,短信和彩信发送消息。
收到有关发送文本可用性更改的通知 消息,注册为观察员 MFMessageComposeViewControllerTextMessageAvailabilityDidChangeNotification 通知。可用性
Available in iOS 4.0 and later.
在MFMessageComposeViewController.h中声明
答案 1 :(得分:3)
MFMEssageComposeViewController上有一个方法:
if ([MFMessageComposeViewController canSendText]) {
}
else {
NSLog(@"Cannot send text");
}
答案 2 :(得分:1)
NSString *deviceType = [UIDevice currentDevice].model;
if ([deviceType hasPrefix:@"iPod"])
{
//It's iPod;
//Disable button
}
答案 3 :(得分:1)
首先使用以下方式检测设备,如果设备不支持信使,则显示警报。
在此处,您可以了解有关不同设备检测的更多信息: Determine device (iPhone, iPod Touch) with iPhone SDK
- (IBAction)Message:(id)sender
{
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:@"iPod Touch 5G"]) {
// here your alert view to show msg
} else {
if ([MFMessageComposeViewController canSendText])
{
MFMessageComposeViewController *messaging=[[MFMessageComposeViewController alloc]init];
messaging.messageComposeDelegate=self;
[messaging setBody:@"Will of the People""\n""http://bit.ly/1gZhZye"];
[self presentViewController:messaging animated:YES completion:nil];
}
}
}
答案 4 :(得分:0)
您可以使用UIDevice
类检查设备类型
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType hasPrefix:@"iPod"])
// it's an iPod
或者您可以使用[MFMessageComposeViewController canSendText]
检查消息是否可以从设备发送
答案 5 :(得分:0)
-(IBAction)btnByEmailPressed:(id)sender
{
if ([MFMailComposeViewController canSendMail] == NO)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"This device is not able to send mail or Email account is not configured." delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
[alert show];
return;
}
else
{
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setTitle:@"Invitation"];
[controller setSubject:@"My Subject"];
[controller setMessageBody:@"Your Text" isHTML:YES];
[self presentViewController:controller animated:YES completion:nil];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self becomeFirstResponder];
NSString *strMailResult;
switch (result)
{
case MFMailComposeResultCancelled:
strMailResult = NSLocalizedString(@"E-Mail Cancelled",@"");
break;
case MFMailComposeResultSaved:
strMailResult = NSLocalizedString(@"E-Mail Saved",@"");
break;
case MFMailComposeResultSent:
strMailResult = NSLocalizedString(@"E-Mail Sent",@"");
break;
case MFMailComposeResultFailed:
strMailResult = NSLocalizedString(@"E-Mail Failed",@"");
break;
default:
strMailResult = NSLocalizedString(@"E-Mail Not Sent",@"");
break;
}
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"STEP-BY-STEP-STORY",@"") message:strMailResult delegate:self cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil];
[alertView show];
[self dismissViewControllerAnimated:YES completion:nil];
}