ABPeoplePickerNavigationController被取消后的自定义操作

时间:2009-06-04 19:45:21

标签: iphone uinavigationcontroller sms

我向用户展示了一个ABPeoplePickerNavigationController并要求他们选择一个联系人。一旦他们选择了用户,我希望将他们发送到消息应用或电子邮件应用,具体取决于他们选择的属性。但是,我无法弄清楚如何自定义模态选择器被解除后发生的动作。

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
    if(property == kABPersonPhoneProperty){
        [self dismissModalViewControllerAnimated:YES];
        NSString* phoneNumber = (NSString *)ABRecordCopyValue(person, property);
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"sms:%@", phoneNumber]];
        [[UIApplication sharedApplication] openURL:url];
        [phoneNumber release];
        return NO;
    }

    if(property == kABPersonEmailProperty){
        [self dismissModalViewControllerAnimated:YES];
        NSString* emailAddress = (NSString *)ABRecordCopyValue(person, property);
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@", emailAddress]];
        [[UIApplication sharedApplication] openURL:url];
        [emailAddress release];
        return NO;
    }
    return YES;
}

那么,我该怎么做? 谢谢!

4 个答案:

答案 0 :(得分:2)

您只需要实现委托方法

   - (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifierForValue

在实现中只需返回YES(除非您需要处理所选属性)。然后,如果用户触摸了电话号码,您的应用将开始拨打该电话号码。同样,如果用户触摸了电子邮件地址,您的应用将切换到默认的撰写电子邮件视图。

不要忘记将自己指定为委托,否则不会调用方法:

ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
peoplePicker.peoplePickerDelegate = self;
[self presentModalViewController:peoplePicker animated:YES];

最后,将ABPeoplePickerNavigationControllerDelegate放入控制器头文件的协议部分。

注意:您应该在设备上测试,而不是在模拟器上测试。

答案 1 :(得分:0)

您可以使用两种方法(例如,pickedPhone和pickedMail)创建自定义协议,并让调用此类的类实现这些例程并将其自身指定为委托。做出决定后,检查是否定义了委托,然后调用正确的方法,让调用者处理如何处理它。

另外,你应该对“sms”网址进行一些测试。最后我检查它运行SMS应用程序,但没有传递任何东西。

但在你到达那里之前,通过调用那些openURL来调用外部电话或电子邮件应用程序,你的应用程序将停止,控制权将转移到这些应用程序。所以我不确定为什么你关心用户解雇模态选择器后会发生什么,因为你的应用程序将不再存在。

现在在OS 3.0下你可能有一些选择,但仍然......你可能想重新考虑这个问题。

答案 2 :(得分:0)

解决。 问题不在于委托方法。我创建的NSURL无效。 要使用所选的电话号码启动短信/消息应用,用户短信:// ... // 是关键! 文档似乎表明短信:是正确的格式,但我发现这是不正确的。

答案 3 :(得分:0)

只需在

中返回NO / FALSE / false
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier

并在相同的函数中编写代码以进行联系处理