【问题标题】:How can i render a list of all iPhone contacts with Cordova (phone gap)如何使用 Cordova 呈现所有 iPhone 联系人的列表(电话间隙)
【发布时间】:2014-02-04 21:29:27
【问题描述】:

我正在尝试使用以下代码(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 上完美运行。

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

【问题讨论】:

  • 以前没有人这样做过。真的吗?
  • 我认为加载 3000 个联系人不是一个好主意。您是否尝试过某种寻呼方式?
  • 我们也考虑过这一点。电话间隙有选择吗?我在 api 文档上找不到页面选项。拥有类似 .find({limit=100}) 的东西会很酷
  • 只是资助这个问题:stackoverflow.com/questions/12335003/… 没有答案;(
  • 我没有找到任何解决方案。您应该在问题跟踪器中打开一个问题。在解决此问题之前,解决此问题的唯一方法可能是按名称 name="A*" 的字母进行过滤,但这很难看。

标签: ios cordova ios6 spine.js


【解决方案1】:

我在处理 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;
}

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

【讨论】:

    【解决方案2】:

    首先你必须从终端命令行添加插件

    $ 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));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      • 1970-01-01
      • 2012-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多