【问题标题】:ABAddressBookCreate() , ABAddressBookGetGroupCount , ... return @"0x00000000 <nil>"?ABAddressBookCreate() , ABAddressBookGetGroupCount , ...返回@“0x00000000 <nil>”?
【发布时间】:2012-12-23 10:44:23
【问题描述】:

我正在尝试获取组名称,但在多次调用此方法“由用户重新加载联系人”后,它给出了 nil 值和以下错误。

-(void) getGroupsName
    {
        [groupsName removeAllObjects];
        //address book object to interact with iPhone contacts.
        ABAddressBookRef addressbook = ABAddressBookCreate();
        //get groups count
        CFIndex groupsCount          = ABAddressBookGetGroupCount(addressbook);
        //get all available groups as array
        CFArrayRef allGroups         = ABAddressBookCopyArrayOfAllGroups(addressbook);

        for (int i = 0; i<groupsCount; i++) {
            //get group of index=i from groups array
            ABRecordRef group = CFArrayGetValueAtIndex(allGroups, i);
            //get group name, I use __bridge_transfer to transfer from C to objective-c.
            [groupsName addObject:(__bridge_transfer NSString*)ABRecordCopyCompositeName(group)];

        }
        CFRelease(allGroups);
        CFRelease(addressbook);
    }
//////////////////////////////////////////////////////////////

    warning: Could not compile statement PRAGMA journal_mode = wal;: unable to open database file error 14 creating properties table: unable to open database file warning: Could not compile statement SELECT value FROM _SqliteDatabaseProperties WHERE key = ?;: unable to open database file warning: Could not compile statement SELECT value FROM _SqliteDatabaseProperties WHERE key = ?;: unable to open database file warning: Could not compile statement SELECT value FROM
_SqliteDatabaseProperties WHERE key = ?;: unable to open database file warning: Could not compile statement SELECT ROWID, First, Last, Middle, NULL, NULL, NULL, Organization, NULL, NULL, Kind, NULL, NULL, Nickname, Prefix, Suffix, FirstSort, LastSort, CreationDate, ModificationDate, CompositeNameFallback, NULL, StoreID, NULL, FirstSortSection, LastSortSection, FirstSortLanguageIndex, LastSortLanguageIndex, NULL, NULL, NULL, PersonLink, NULL, IsPreferredName FROM ABPerson;: unable to open database file warning: Could not compile statement SELECT ROWID, First, Last, Middle, NULL, NULL, NULL, Organization, NULL, NULL, Kind, NULL, NULL, Nickname, Prefix, Suffix, FirstSort, LastSort, CreationDate, ModificationDate, CompositeNameFallback, NULL, StoreID, NULL, FirstSortSection, LastSortSection, FirstSortLanguageIndex, LastSortLanguageIndex, NULL, NULL, NULL, PersonLink, NULL, IsPreferredName FROM ABPerson;: unable to open database file warning: Could not compile statement INSERT OR REPLACE INTO _SqliteDatabaseProperties VALUES (?, ?);: unable to open database file warning: Could not compile statement SELECT value FROM
_SqliteDatabaseProperties WHERE key = ?;: unable to open database file warning: Could not compile statement INSERT OR REPLACE INTO
_SqliteDatabaseProperties VALUES (?, ?);: unable to open database file warning: Could not compile statement SELECT value FROM
_SqliteDatabaseProperties WHERE key = ?;: unable to open database file warning: Could not compile statement SELECT value FROM
_SqliteDatabaseProperties WHERE key = ?;: unable to open database file warning: Could not compile statement SELECT ROWID FROM ABGroup;: unable to open database file warning: Could not compile statement SELECT ROWID, Name, ExternalIdentifier, StoreID, NULL, NULL, NULL FROM ABGroup;: unable to open database file

所以我使用本机通知让我知道 addressbook 何时被修改以减少我访问 addressbook 的次数,但如果用户每次都进行多次更新,那么到那时仍然不好addrssbook 得到修改必须调用这个杂项或任何其他与addressbook 相关的名称。

所以还需要你的帮助???

【问题讨论】:

  • @rmaddy 情况不一样,我的代码第一次和每次都获得所有联系但是,当用户点击它超过 20 次时,我有重新加载按钮,它返回上面的错误跨度>
  • 对不起,我没有意识到您只是在多次迭代后才出现问题。

标签: iphone objective-c ios ios5 abaddressbook


【解决方案1】:

ABAddressBookCreate 在 iOS6 中已被弃用。使用ABAddressBookCreateWithOptions,如果您无权访问地址簿,它将返回一个CFErrorRef 对象。

【讨论】:

  • 在多次调用该方法后仍然给我同样的问题它会返回 null 所以我认为从 sdk 中它自己??
  • 您是否尝试查看 CFErrorRef?
  • 就像你看到它是警告而不是错误所以不能记录它或为 &error 做任何细化
【解决方案2】:

您为什么不尝试使用以下方法尽量减少创建和访问ABAddressBookRef 的时间:

void ABAddressBookRegisterExternalChangeCallback (
   ABAddressBookRef addressBook,
   ABExternalChangeCallback callback,
   void *context
);

这允许您的应用知道地址簿何时被外部修改。并让您知道您需要重新访问地址簿。

与您发布它的方法相同,每次调用它都会创建具有新访问地址簿的新对象

【讨论】:

  • thx 这对我有用,现在没有崩溃,也没有 SQL 冲突 :)
【解决方案3】:

正如@thelaws 所指出的,您必须在 IOS6 中使用 ABAddressBookCreateWithOptions。 if ([[[UIDevice currentDevice] systemVersion] floatValue] > 5.1) {

   ABAddressBookRef ab = ABAddressBookCreateWithOptions(NULL, NULL);
    __block BOOL accessGranted = NO;
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(ab, ^(bool granted, CFErrorRef error) {
            // First time access has been granted, add the contact
            accessGranted = granted;
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)        {
        // The user has previously given access, add the contact
        accessGranted = YES;
    }
    else {
        // The user has previously denied access
        // Send an alert telling user to change privacy setting in settings app
    }
    if (accessGranted) {
// your code goes here
 }
}
else {
// your code goes here
}

另一件事是你有没有初始化,groupsName 某处?..

除此之外,您是否使用 ABExternalChangeCallBack 来获取地址簿更改的通知?

我认为您应该将组代码放在您访问联系人的同一模块中。 (如果这对您没有帮助,请澄清!)

【讨论】:

  • 第一件事是,我使用 ABExternalChangeCallBack,这给了我更多 10 时间来避免崩溃“当用户更新然后应用程序收到通知。如果这种情况发生多次,则重新访问地址簿”。第二:如果我多次请求它,获取联系人也是一样的,它会崩溃或返回给我 nil 值。所以这无济于事:(
  • 原则上是正确的,但是-1表示取系统版本的floatValue。这不是检查 API 可用性的推荐方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-18
  • 2019-06-20
  • 2017-12-16
  • 2014-09-27
  • 2017-06-11
  • 2014-12-29
相关资源
最近更新 更多