Share via


빠른 시작: 사용자 연락처 선택(HTML)

[ 이 문서는 Windows 런타임 앱을 작성하는 Windows에서 8.x 및 Windows Phone 8.x 개발자를 대상으로 합니다. Windows 10용으로 개발하는 경우에는 최신 설명서를 참조하세요.]

Windows.ApplicationModel.Contacts 네임스페이스를 통해 연락처를 선택하는 여러 가지 옵션이 있습니다. 여기서는 단일 연락처나 여러 연락처를 선택하는 방법을 설명하고 연락처 선택 기능을 구성하여 앱에 필요한 연락처 정보만 검색하는 방법을 보여 줍니다.

사전 요구 사항

  • Microsoft Visual Studio 및 관련 템플릿에 대해 잘 알고 있는 것이 좋습니다.
  • JavaScript에 대해 잘 알고 있는 것이 좋습니다.

연락처 선택 기능 설정

Windows.ApplicationModel.Contacts.ContactPicker 인스턴스를 만들고 변수에 할당합니다.

var picker = Windows.ApplicationModel.Contacts.ContactPicker();

커밋 단추의 이름 지정

커밋 단추는 사용자가 선택한 연락처를 확인하기 위해 누르는 단추입니다. Windows.ApplicationModel.Contacts.CommitButtonText를 사용하여 이 단추의 이름을 설정합니다.

picker.commitButtonText = "Select";

선택 모드 설정(옵션)

기본적으로 연락처 선택 기능은 사용자가 선택하는 연락처에 대해 사용 가능한 데이터를 모두 검색합니다. selectionMode 속성을 사용하면 연락처 선택 기능이 앱에 필요한 데이터 필드만 검색하도록 구성할 수 있습니다. 사용 가능한 연락처 데이터의 하위 집합만 필요한 경우에 이것은 연락처 선택 기능을 더 효율적으로 사용하는 방법입니다.

먼저 selectionMode 속성을 fields로 설정합니다.

picker.selectionMode = Windows.ApplicationModel.Contacts.ContactSelectionMode.fields;

그런 다음 desiredFieldsWithContactFieldType 속성을 사용하여 연락처 선택 기능에서 검색할 필드를 지정합니다. 이 예제에서는 연락처 선택 기능에서 메일 주소를 검색하도록 구성합니다:

picker.desiredFieldsWithContactFieldType.append(Windows.ApplicationModel.Contacts.ContactFieldType.email);    

선택기 시작

이제 선택기를 시작할 수 있습니다. 사용자가 연락처를 하나만 선택하도록 하려면 pickContactAsync를 사용합니다. 사용자가 선택하는 연락처를 처리하려면 done을 사용하여 선택기가 반환하는 연락처를 처리할 함수를 호출합니다.

        picker.pickContactAsync().done(function (contact) {
            if (contact !== null) {
                // Create UI to display the contact information for the selected contact
                var contactElement = document.createElement("div");
                contactElement.className = "contact";

                // Display the name
                contactElement.appendChild(createTextElement("h3", contact.displayName));

사용자가 하나 이상의 연락처를 선택하도록 하려면 pickContactsAsync를 사용합니다. 사용자가 선택하는 연락처를 처리하려면 done을 사용하여 선택기가 반환하는 연락처를 처리할 함수를 호출합니다.


        picker.pickContactsAsync().done(function (contacts) {
            if (contacts.length > 0) {
                // Display the selected contact names
                var output = "Selected contacts:\n";
                contacts.forEach(function (contact) {
                    output += contact.displayName + "\n";
                });

완전한 예제(단일 연락처)

이 예제에서는 연락처 선택 기능을 사용하여 단일 연락처의 이름, 메일 주소, 전화 번호 및 주소를 검색합니다.

(function () {
    "use strict";
    var page = WinJS.UI.Pages.define("/html/scenarioSingle.html", {
        ready: function (element, options) {
            // Use element.querySelector() instead of document.getElementById() to ensure that the correct default.html page is targeted.
            element.querySelector("#open").addEventListener("click", pickContact, false);
        }
    });

    function pickContact() {
        // Clean scenario output
        WinJS.log && WinJS.log("", "sample", "status");

        // Create the picker
        var picker = new Windows.ApplicationModel.Contacts.ContactPicker();
        picker.commitButtonText = "Select";

        // Open the picker for the user to select a contact
        picker.pickContactAsync().done(function (contact) {
            if (contact !== null) {
                // Create UI to display the contact information for the selected contact
                var contactElement = document.createElement("div");
                contactElement.className = "contact";

                // Display the name
                contactElement.appendChild(createTextElement("h3", contact.displayName));

                // Add the different types of contact data
                if (contact.emails.length > 0) {
                    contactElement.appendChild(createTextElement("h4", "Emails:"));
                    contact.emails.forEach(function (email) {
                        switch (email.kind) {
                            case Windows.ApplicationModel.Contacts.ContactEmailKind.personal:
                                contactElement.appendChild(createTextElement("div", email.address + " (personal)"));
                                break;
                            case Windows.ApplicationModel.Contacts.ContactEmailKind.work:
                                contactElement.appendChild(createTextElement("div", email.address + " (work)"));
                                break;
                            case Windows.ApplicationModel.Contacts.ContactEmailKind.other:
                                contactElement.appendChild(createTextElement("div", email.address + " (other)"));
                                break;
                        }
                    });
                }
                
                if (contact.phones.length > 0) {
                    contactElement.appendChild(createTextElement("h4", "Phone Numbers:"));
                    contact.phones.forEach(function (phone) {
                        switch (phone.kind) {
                            case Windows.ApplicationModel.Contacts.ContactPhoneKind.home:
                                contactElement.appendChild(createTextElement("div", phone.number + " (home)" ));
                                break;
                            case Windows.ApplicationModel.Contacts.ContactPhoneKind.work:
                                contactElement.appendChild(createTextElement("div", phone.number + " (work)" ));
                                break;
                            case Windows.ApplicationModel.Contacts.ContactPhoneKind.mobile:
                                contactElement.appendChild(createTextElement("div", phone.number + " (mobile)" ));
                                break;
                            case Windows.ApplicationModel.Contacts.ContactPhoneKind.other:
                                contactElement.appendChild(createTextElement("div", phone.number + " (other)" ));
                                break;
                        }
                    });
                }

                if (contact.addresses.length > 0) {
                    contactElement.appendChild(createTextElement("h4", "Addresses:"));
                    contact.addresses.forEach(function (address) {
                        switch (address.kind) {
                            case Windows.ApplicationModel.Contacts.ContactAddressKind.home:
                                contactElement.appendChild(createTextElement("div", getUnstructuredAddress(address) + " (home)" ));
                                break;
                            case Windows.ApplicationModel.Contacts.ContactAddressKind.work:
                                contactElement.appendChild(createTextElement("div", getUnstructuredAddress(address) + " (work)"));
                                break;
                            case Windows.ApplicationModel.Contacts.ContactAddressKind.other:
                                contactElement.appendChild(createTextElement("div", getUnstructuredAddress(address) + " (other)"));
                                break;
                        }
                    });
                }

                // Add the contact element to the page
                document.getElementById("output").appendChild(contactElement);
            } else {
                // The picker was dismissed without selecting a contact
                WinJS.log && WinJS.log("No contact was selected", "sample", "status");
            }
        });
    }

    function createTextElement(tag, text) {
        var element = document.createElement(tag);
        element.className = "singleLineText";
        element.innerText = text;
        return element;
    }

    function getUnstructuredAddress(contactAddress) {
        var unstructuredAddress = contactAddress.streetAddress + ", " + contactAddress.locality + ", " + contactAddress.region + ", " + contactAddress.postalCode;
        return unstructuredAddress;
    }


})();

완전한 예제(여러 연락처)

이 예제에서는 연락처 선택 기능을 사용하여 여러 연락처를 검색한 다음 연락처 이름을 표시합니다.


(function () {
    "use strict";
    var page = WinJS.UI.Pages.define("/html/scenarioMultiple.html", {
        ready: function (element, options) {
            // Use element.querySelector() instead of document.getElementById() to ensure that the correct default.html page is targeted.
            element.querySelector("#open").addEventListener("click", pickContacts, false);
        }
    });

    function pickContacts() {
        // Clean scenario output
        WinJS.log && WinJS.log("", "sample", "status");

        // Create the picker
        var picker = new Windows.ApplicationModel.Contacts.ContactPicker();
        picker.commitButtonText = "Select";

        // Open the picker for the user to select contacts
        picker.pickContactsAsync().done(function (contacts) {
            if (contacts.length > 0) {
                // Display the selected contact names
                var output = "Selected contacts:\n";
                contacts.forEach(function (contact) {
                    output += contact.displayName + "\n";
                });
                WinJS.log && WinJS.log(output, "sample", "status");
            } else {
                // The picker was dismissed without selecting any contacts
                WinJS.log && WinJS.log("No contacts were selected", "sample", "status");
            }
        });
    }
})();

요약 및 다음 단계

지금까지 연락처 선택 기능을 사용하여 연락처 정보를 검색하는 방법을 간략히 살펴보았습니다. 연락처와 연락처 선택 기능을 사용하는 방법에 대한 더 많은 예제를 보려면 코드 갤러리(영문)에서 연락처 선택 기능 앱 샘플(영문)을 다운로드하세요.

관련 항목

연락처 선택 기능 앱 샘플