ios - 短信中的好友姓名? ( objective-C )

标签 ios objective-c

我正在制作一个应用程序,用户可以在其中发送带有预填电话号码的短信。 loadString、loadString2 和 loadString3 是用户可以填写并保存的 UITextFields 的字符串。但是,如果用户只填写一两个字段,留空一个,则收件人是所选的电话号码,以及一个名为 Buddy name 的联系人。

我的问题是,如何摆脱这个名为“Buddy name”的奇怪联系人?

我的代码:

if ([MFMessageComposeViewController canSendText])

{

    [controller setRecipients:[NSArray arrayWithObjects:loadString, loadString2, loadString3, nil]];
    [controller setBody:theLocation];
    [self presentViewController:controller animated:YES completion:NULL];

} else {        
    NSLog(@"Can't open text.");
}

最佳答案

“Buddy Name”显示数组是否包含一个字符串,该字符串不是像空字符串那样的有效联系人。仅将非空字符串添加到收件人数组中:

if ([MFMessageComposeViewController canSendText])
{
    NSMutableArray *recipents = [NSMutableArray array];
    if ([self isValidPhoneNumber:loadString]) {
         [recipents addObject:loadString];
    }
    if ([self isValidPhoneNumber:loadString2]) {
         [recipents addObject:loadString2];
    }
    if ([self isValidPhoneNumber:loadString3]) {
         [recipents addObject:loadString3];
    }
    [controller setRecipients:recipents];
    [controller setBody:theLocation];
    [self presentViewController:controller animated:YES completion:NULL];

} else {
    NSLog(@"Can't open text.");
}

...

- (BOOL)isValidPhoneNumber:(NSString *)phoneNumberString {
    // If the length is 0 it's invalid. You may want to add other checks here to make sure it's not invalid for other reasons.
    if (phoneNumberString.length == 0) {
        return NO;
    } else {
        return YES;
    }
}

关于ios - 短信中的好友姓名? ( objective-C ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30563037/

相关文章:

ios - 将未编译的源代码添加到 iOS 应用程序

ios - 从部署目标 iOS 中删除特定设备

ios - 如何获取包含 UIImage 的 NSAttributedString 的自定义文本等效项?

objective-c - Foundation框架导致的 "memory leaks"怎么办?

ios - 将1ch 16000Hz PCM转换为2ch 44100Hz PCM

ios - 您可以为 NSKeyedArchiver archivedDataWithRootObject 禁用 "mangled runtime name"崩溃/警告吗?

iphone - 如何在选择单元格后立即显示 UIAlert 并在显示新的 UIView 后结束它?

ios - 取消等待执行的dispatch_async队列

iphone - (iOS)通过locationManager的多个通知:didExitRegion: when exiting a region

ios - 如何仅启用数字数字键盘 ios Swift