我正在制作一个应用程序,用户可以在其中发送带有预填电话号码的SMS。 loadString,loadString2和loadString3是UITextField的字符串,用户可以填写并保存。但是,如果用户仅填写一个或两个字段,而将一个字段留空,则收件人是所选的电话号码,而一个联系人称为好友姓名。

我的问题是,如何摆脱这种奇怪的联系,称为“好友名称”?

我的代码:

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.");
}

最佳答案

“好友名称”显示数组是否包含不是有效联系人的字符串,例如空字符串。仅将字符串添加到收件人数组(如果它们不是空的):

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 - 短信中的好友名称? (OBJ-C),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30563037/

10-10 21:13