SORU
19 EYLÜL 2010, Pazar


İOS tüm kişiler listesi

Bir iPhone tüm kişilerin listesini almak istiyorum.

Address Book referans olarak bir şeyi atlamış olabilirim ama kişilerin listesini almak için bir yöntem sağlar görmedim kontrol ettim.

CEVAP
1 Mayıs 2014, PERŞEMBE


Sorunuzu, ama aynı zamanda cevapları burada verilen çoğu sadece ya izin istemek için başarısız, 7 ** aşacağımızı yok düzgün ya da sızıntı olan tepkiler birkaç:

  1. Belli ki, AddressBook çerçeve alımı:

    #import <AddressBook/AddressBook.h>
    

    ya

    @import AddressBook;
    
  2. Uygulaması için izin rehbere erişmek için istemeniz gerekir. Örneğin:

    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
    
    if (status == kABAuthorizationStatusDenied || status == kABAuthorizationStatusRestricted) {
        // if you got here, user had previously denied/revoked permission for your
        // app to access the contacts, and all you can do is handle this gracefully,
        // perhaps telling the user that they have to go to settings to grant access
        // to contacts
    
        [[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
        return;
    }
    
    CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    
    if (!addressBook) {
        NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error));
        return;
    }
    
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        if (error) {
            NSLog(@"ABAddressBookRequestAccessWithCompletion error: %@", CFBridgingRelease(error));
        }
    
        if (granted) {
            // if they gave you permission, then just carry on
    
            [self listPeopleInAddressBook:addressBook];
        } else {
            // however, if they didn't give you permission, handle it gracefully, for example...
    
            dispatch_async(dispatch_get_main_queue(), ^{
                // BTW, this is not on the main thread, so dispatch UI updates back to the main queue
    
                [[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
            });
        }
    
        CFRelease(addressBook);
    });
    
  3. Not yukarıda, desen başkaları tarafından önerilen kullanılmaz:

    CFErrorRef *error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    

    Bu doğru değil. Yukarıda göreceğiniz gibi, seni istiyorum:

    CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    

    Eski desen ikincisi; oysa bir hata yakalama doğru olmaz. error NULL, olmaz CFRelease bunu unutma (ya da benim yaptığım gibi ARC için mülkiyet aktarımı) veya başka bir nesne sızıntısı.

  4. Bu kişiler arasında dolaşmak için:

    - (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
    {
        NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
        NSInteger numberOfPeople = [allPeople count];
    
        for (NSInteger i = 0; i < numberOfPeople; i  ) {
            ABRecordRef person = (__bridge ABRecordRef)allPeople[i];
    
            NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
            NSString *lastName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
            NSLog(@"Name:%@ %@", firstName, lastName);
    
            ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
    
            CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
            for (CFIndex i = 0; i < numberOfPhoneNumbers; i  ) {
                NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
                NSLog(@"  phone:%@", phoneNumber);
            }
    
            CFRelease(phoneNumbers);
    
            NSLog(@"=============================================");
        }
    }
    
  5. Oldukça önemli bir ayrıntı dikkatinizi, yani "Create Rule": çizmek istiyorum

    Temel fonksiyonları döndürülen bir nesne kendi zaman belirten isimler var:

    • Nesne oluşturma var fonksiyonlar “Create” adı altında gömülü;

    • Nesne çoğaltma “Copy” adı altında gömülü. işlevler

    Eğer bir nesne varsa, bunu bitirdiğinizde mülkiyet (CFRelease kullanarak) feragat etmek sizin sorumluluğunuzdadır.

    Bu herhangi bir nesne Create adına Copy ile herhangi bir Temel işlevi tarafından döndürülen serbest bırakmak için sorumluluk anlamına gelir. Senin de Ara CFRelease açıkça (gibi yaptım yukarıda *ile 23* phoneNumbers) ya da, nesneleri destekleyen ücretsiz köprü, transfer mülkiyetine ARK __bridge_transfer CFBridgingRelease (gibi yaptım yukarıda allPeople, lastName, firstName, ve phoneNumber).

    Statik analiz (basınshiftkomutBböyle büyük mükafat ya da "dan" Ürün "menüsü) bu gözlemlemek için ihmal edilen birçok durum belirleyebilir" "ve uygun serbest bırakmak için başarısız nesneleri Oluşturma Kuralı Analiz seçin Bu gibi Temel kod yazma zaman, her zaman belirgin herhangi bir sızıntı yok emin olmak için statik analiz programını Çalıştır.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Mismag822 - The Card Trick Teacher

    Mismag822 -

    18 EKİM 2008
  • Crossover

    Crossover

    18 HAZİRAN 2007
  • WHZGUD2

    WHZGUD2

    21 EYLÜL 2011