21

Square の新しいカード ケース iOS アプリには、「アカウントの作成」機能があります。それをタップすると、アドレス帳からのユーザーのエントリが事前設定されたフォームが表示されます。

これはどのように可能ですか?誰でも知っていますか?この方法でユーザーの情報を取得するには、これは不可能だと思いました。これは iOS 5.0 の問題ではありません。

4

5 に答える 5

23

アプリがアドレス帳へのアクセス許可を要求する必要がある変更以降、この方法は機能しなくなりました。Nik Burns の回答はうまくいきましたが、アドレス帳にアクセスする必要があります。

Square Wallet (元の質問の Square Card Case 参照の後継) をダウンロードしましたが、サインアップ フォームに事前入力されなくなりました。パスは、デバイス名のアドレス帳のトリックを行うためにも使用されていましたが、サインアップ フォームに事前入力されなくなりました。

上記の方法を使用すると、連絡先へのアクセスを要求した後 にサインアップ フォームを事前入力することはできますが、必要な「魔法の」エクスペリエンスが失われます。

于 2013-04-24T21:40:46.517 に答える
0

いいえ、できます

http://developer.apple.com/library/ios/#documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Introduction.html#//apple_ref/doc/uid/TP40007744

4.X で利用可能だったことは知っています。5.0の機能だけではありません...以前に利用可能だったかどうかはわかりません。

于 2011-11-03T19:53:14.100 に答える
0

私は同じ問題を抱えていましたが、Swift で、Nik Burns の提案を使用して、それについてのブログ投稿を書きました: http://paulpeelen.com/2016/01/20/prefill-an-signup-form-in-ios-using -swift-and-cncontact/

これは基本的に Swift での最終結果です。

import Contacts

...

    @IBOutlet weak var nameFirst: UILabel!
    @IBOutlet weak var nameLast: UILabel!
    @IBOutlet weak var email: UILabel!
    @IBOutlet weak var phoneNo: UILabel!
    @IBOutlet weak var address: UILabel!

    /**
     Search the addressbook for the contact
     */
    private func getMe() {
        let ownerName = getOwnerName()
        let store = CNContactStore()

        do {
            // Ask permission from the addressbook and search for the user using the owners name
            let contacts = try store.unifiedContactsMatchingPredicate(CNContact.predicateForContactsMatchingName(ownerName), keysToFetch:[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey, CNContactPostalAddressesKey, CNContactEmailAddressesKey])

            //assuming contain at least one contact
            if let contact = contacts.first {
                // Checking if phone number is available for the given contact.
                if (contact.isKeyAvailable(CNContactPhoneNumbersKey)) {
                    // Populate the fields
                    populateUsingContact(contact)
                } else {
                    //Refetch the keys
                    let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey, CNContactPostalAddressesKey, CNContactEmailAddressesKey]
                    let refetchedContact = try store.unifiedContactWithIdentifier(contact.identifier, keysToFetch: keysToFetch)

                    // Populate the fields
                    populateUsingContact(refetchedContact)
                }
            }
        } catch let e as NSError {
            print(e.localizedDescription)
        }

    }

    /**
     Get the device owners name

     - returns: The owners name
     */
    private func getOwnerName() -> String {
        // Get the device owners name
        var ownerName = UIDevice.currentDevice().name.trimmedString.stringByReplacingOccurrencesOfString("'", withString: "")
        // Get the model of the device
        let model = UIDevice.currentDevice().model

        // Remove the device name from the owners name
        if let t = ownerName.rangeOfString("s \(model)") {
            ownerName = ownerName.substringToIndex(t.startIndex)
        }

        return ownerName.trimmedString
    }

    /**
     Populate the fields using a contact

     - parameter contact: The contact to use
     */
    private func populateUsingContact(contact: CNContact) {
        // Set the name fields
        nameFirst.text = contact.givenName
        nameLast.text = contact.familyName

        // Check if there is an address available, it might be empty
        if (contact.isKeyAvailable(CNContactPostalAddressesKey)) {
            if let
                addrv = contact.postalAddresses.first,
                addr = addrv.value as? CNPostalAddress where addrv.value is CNPostalAddress
            {
                let address = "\(addr.street)\n\(addr.postalCode) \(addr.city)\n\(addr.country)"
                self.address.text = address
            }
        }

        // Check if there is a phonenumber available, it might be empty
        if (contact.isKeyAvailable(CNContactPhoneNumbersKey)) {
            if let
                phonenumberValue = contact.phoneNumbers.first,
                pn = phonenumberValue.value as? CNPhoneNumber where phonenumberValue.value is CNPhoneNumber
            {
                phoneNo.text = pn.stringValue
            }
        }
    }

そして拡張子:

extension String {

    /// Trim a string
    var trimmedString: String {
        return stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
    }

}
于 2016-01-20T15:12:01.547 に答える