我正在创建一个应用,用户可以使用预先填写的电话号码发送短信。 loadString,loadString2和loadString3是UITextFields的字符串,用户可以填写并保存。但是,如果用户只填写一个或两个字段,并留下一个空,则收件人是所选的电话号码,一个联系人称为好友名称。
我的问题是,如何摆脱这种名为“好友名字”的奇怪联系?
我的代码:
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.");
}
答案 0 :(得分:2)
“好友名称”显示数组是否包含不像空字符串那样的有效联系人的字符串。如果字符串非空,则仅将字符串添加到收件人数组:
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;
}
}