6

次のコード (coffeescript) を使用して、iPhone アドレス帳のすべての連絡先を一覧表示するアプリケーションを作成しようとしています。

listContacts: ->
    options = new ContactFindOptions()
    options.filter = '';
    options.multiple = true
    fields = ["id", "photos", "name", "phoneNumbers"]
    navigator.contacts.find(fields, @onSuccess, @onError, options)

onSuccess: (contacts) ->
    console.log contacts.length

onError: (error) ->
    console.log error

これは、たくさんの連絡先にうまく機能するようです。しかし、3000 では連絡先は返されません。面白いことに、これは iOsSimulator で完全に機能します。

取得できる連絡先の数に制限はありますか?

4

2 に答える 2

2

300 件の連絡先で同じ問題が発生し、約 5 分かかりました。パッチを適用した後、わずか 10 秒しかかかりません。

これが私のプルリクエストです: https://github.com/phonegap/phonegap/pull/19

彼らは各写真の一時ファイルを生成する必要があり、狂ったループを使用して空きファイル パスを見つけています。何かのようなもの :

do {        
  filePath = [NSString stringWithFormat:@"%@/photo_%03d.jpg", docsPath, i++];       
} while ([fileMgr fileExistsAtPath:filePath]);

今はmktempを使用しており、すべてが高速です。

フル解像度の写真が必要ない場合は、以下を置き換えることもできます:

CFDataRef photoData = ABPersonCopyImageData(self.record);

に :

CFDataRef photoData = ABPersonCopyImageDataWithFormat(self.record, kABPersonImageFormatThumbnail);

それがあなたを助けることを願っています!

編集 :

IOS は、アプリケーションを起動するたびに一時ディレクトリをフラッシュします。

作成した一時ファイルは、お客様の責任で削除してください。システムは起動時にそれらをクリーンアップしますが、それには非常に長い時間がかかる可能性があります.

から: http://cocoadev.com/wiki/NSTemporaryDirectory

アプリケーションのブートストラップを遅くしたくない場合は、連絡先 ID に基づいて常に同じファイルパスを使用する必要があります。ファイルが既に存在する場合は、クリーンアップと書き込みの時間を節約できます。

- (NSObject*)extractPhotos
{
    NSMutableArray* photos = nil;

    if (ABPersonHasImageData(self.record)) {

        //CFDataRef photoData = ABPersonCopyImageDataWithFormat(self.record, kABPersonImageFormatThumbnail);
        CFDataRef photoData = ABPersonCopyImageData(self.record);
        NSData* data = (__bridge NSData*)photoData;

        // write to temp directory and store URI in photos array
        // get the temp directory path
        NSString* docsPath = [NSTemporaryDirectory ()stringByStandardizingPath];
        NSError* err = nil;
        int recordId = ABRecordGetRecordID(self.record);

        NSFileManager* fileMgr = [[NSFileManager alloc] init];
        NSString* filePath = [NSString stringWithFormat:@"%@/photo_%03d.jpg", docsPath, recordId];
        BOOL hasImage = NO;

        if ([fileMgr fileExistsAtPath:filePath]) {
            hasImage = YES;
        } else if ([data writeToFile:filePath options:NSAtomicWrite error:&err]) {
            hasImage = YES;
        }

        if (hasImage) {
            photos = [NSMutableArray arrayWithCapacity:1];
            NSMutableDictionary* newDict = [NSMutableDictionary dictionaryWithCapacity:2];
            [newDict setObject:filePath forKey:kW3ContactFieldValue];
            [newDict setObject:@"url" forKey:kW3ContactFieldType];
            [newDict setObject:@"false" forKey:kW3ContactFieldPrimary];
            [photos addObject:newDict];
        }

        CFRelease(photoData);
    }
    return photos;
}

編集 (2013 年 8 月 1 日) : 参考までに: コルドバにマージ: http://git-wip-us.apache.org/repos/asf/cordova-ios/commit/c6a1dbe3

于 2012-12-12T11:56:44.927 に答える
1

まず、端末のコマンドラインからプラグインを追加する必要があります

$ cordova plugin add org.apache.cordova.contacts

onDeviceReady メソッドを呼び出して連絡先リストを開くことができます

function chooseContact() {
    var options = new ContactFindOptions();
    options.fields = ["displayName", "name", "emails", "phoneNumbers"];
    navigator.contacts.chooseContact(onSuccess, options);
}

function onSuccess(id, contact) {
    console.log(JSON.stringify(contact));
}
于 2014-02-04T13:29:27.380 に答える