如何使用Cordova呈现所有iPhone联系人列表(手机缺口)

rec*_*eco 6 ios spine.js cordova ios6

我正在尝试创建一个应用程序,它使用以下代码列出iPhone通讯录中的所有联系人(coffeescript)

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
Run Code Online (Sandbox Code Playgroud)

这似乎对一堆联系人很好.但有了3000,联系人永远不会回来.有趣的是,虽然这在iOsSimulator上完美运行.

可以检索的联系人数量是否有任何限制?

Emm*_*ard 2

我在 300 个联系人中也遇到了同样的问题,大约花了 5 分钟。我打完补丁后只需要10秒。

这是我的拉取请求:https ://github.com/phonegap/phonegap/pull/19

他们必须为每张图片生成一个临时文件,并且他们正在使用疯狂的循环来查找免费文件路径。就像是 :

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

现在我使用mktemp,一切都变得更快。

如果不需要全分辨率图片,也可以替换:

CFDataRef photoData = ABPersonCopyImageData(self.record);
Run Code Online (Sandbox Code Playgroud)

经过 :

CFDataRef photoData = ABPersonCopyImageDataWithFormat(self.record, kABPersonImageFormatThumbnail);
Run Code Online (Sandbox Code Playgroud)

我希望这会帮助你!

编辑 :

每次启动应用程序时,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;
}
Run Code Online (Sandbox Code Playgroud)

编辑(08/01/2013):仅供参考:合并到科尔多瓦:http://git-wip-us.apache.org/repos/asf/cordova-ios/commit/c6a1dbe3