我正在制作一个应用程序,用户可以在其中发送带有预填电话号码的短信。 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/