gpt4 book ai didi

ios - 巨大的延迟(没有错误!)仅在请求地址簿权限后第一次

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:00 24 4
gpt4 key购买 nike

在请求用户允许使用他的地址簿后,我只有第一次出现巨大的延迟(5-6 秒)。第一次后,添加新联系人 View Controller 会立即显示。任何人都知道为什么会这样?我在 IOS 7.1 和 8 上使用 Xcode 5 或 6 beta(发生同样的事情)

这是我请求权限的方式:

    ABAddressBookRef addressBook = NULL;
CFErrorRef error = NULL;

switch (ABAddressBookGetAuthorizationStatus()) {
case kABAuthorizationStatusAuthorized: {
addressBook = ABAddressBookCreateWithOptions(NULL, &error);
[self addToContacts];
if (addressBook != NULL) CFRelease(addressBook);
break;
}
case kABAuthorizationStatusDenied: {
//NSLog(@"Access denied to address book");
NSString *msgString = @"You have denied access to contacts. Please go to settings to enable access, then try again.";
UIAlertView *returnAl = [[UIAlertView alloc] initWithTitle:@"Unable to save!" message:msgString delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[returnAl show];
[self performSelector:@selector(dismissAlert:) withObject:returnAl afterDelay:3];

break;
}
case kABAuthorizationStatusNotDetermined: {
addressBook = ABAddressBookCreateWithOptions(NULL, &error);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) {
NSLog(@"Access was granted trying to add");
[self addToContacts];
} else {
NSLog(@"Access was not granted");
}
if (addressBook != NULL) CFRelease(addressBook);
});
break;
}
case kABAuthorizationStatusRestricted: {
NSLog(@"access restricted to address book");
NSString *msgString = @"You have denied access to contacts. Please go to settings to enable access, then try again.";
UIAlertView *returnAl = [[UIAlertView alloc] initWithTitle:@"Unable to save!" message:msgString delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[returnAl show];
[self performSelector:@selector(dismissAlert:) withObject:returnAl afterDelay:3];
break;
}
}

这就是我发起添加新联系人的方式

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(nil,nil);
ABRecordRef person = ABPersonCreate();

// Setting basic properties
ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)(user[@"name"]) , nil);
ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFTypeRef)(user[@"surName"]), nil);
ABRecordSetValue(person, kABPersonJobTitleProperty, (__bridge CFTypeRef)(user[@"workTitle"]), nil);
//ABRecordSetValue(person, kABPersonDepartmentProperty, @"iPhone development department", nil);
ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFTypeRef)(user[@"company"]), nil);
ABRecordSetValue(person, kABPersonNoteProperty, @"Contact saved by introdU app", nil);
// Adding phone numbers
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFTypeRef)(user[@"mobile"]), (CFStringRef)@"iPhone", NULL);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFTypeRef)(user[@"tel"]), (CFStringRef)@"Work", NULL);
//ABMultiValueAddValueAndLabel(phoneNumberMultiValue, @"08701234567", (CFStringRef)@"0870", NULL);
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
if (phoneNumberMultiValue != NULL) { CFRelease(phoneNumberMultiValue); phoneNumberMultiValue = NULL; };

// Adding url
ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFTypeRef)(user[@"url"]), kABPersonHomePageLabel, NULL);
ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, nil);
CFRelease(urlMultiValue);

// Adding emails
ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(user[@"email"]), (CFStringRef)@"Work", NULL);
//ABMultiValueAddValueAndLabel(emailMultiValue, @"ondrej.rafaj@fuerteint.com", (CFStringRef)@"Work", NULL);
ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);
CFRelease(emailMultiValue);

// Adding address
ABMutableMultiValueRef addressMultipleValue = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
[addressDictionary setObject:user[@"address"] forKey:(NSString *)kABPersonAddressStreetKey];
[addressDictionary setObject:user[@"city"] forKey:(NSString *)kABPersonAddressCityKey];
[addressDictionary setObject:user[@"postalCode"] forKey:(NSString *)kABPersonAddressZIPKey];
[addressDictionary setObject:user[@"country"] forKey:(NSString *)kABPersonAddressCountryKey];
//[addressDictionary setObject:@"gb" forKey:(NSString *)kABPersonAddressCountryCodeKey];
ABMultiValueAddValueAndLabel(addressMultipleValue, (__bridge CFTypeRef)(addressDictionary), kABWorkLabel, NULL);
ABRecordSetValue(person, kABPersonAddressProperty, addressMultipleValue, nil);
CFRelease(addressMultipleValue);

ABPersonSetImageData(person, (__bridge CFTypeRef)(profileImageData), nil);
// Adding person to the address book
ABAddressBookAddRecord(addressBook, person, nil);
CFRelease(addressBook);


// Creating view controller for a new contact

ABNewPersonViewController *c = [[ABNewPersonViewController alloc] init];
[c setNewPersonViewDelegate:self];
[c setDisplayedPerson:person];
if (person != NULL) { CFRelease(person); person = NULL; };
[self.navigationController pushViewController:c animated:YES];

最佳答案

发生这种情况是因为在任意队列上调用了 ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) 的完成 block 。

因此,您第一次运行此代码时,您是在任意队列上调用您的 View Controller 表示 - 每隔一次您在主队列上调用它,因此它工作正常。

您需要做的就是将完成 block 分派(dispatch)到主队列。

ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) {
NSLog(@"Access was granted trying to add");
dispatch_async(dispatch_get_main_queue(), ^{
[self addToContacts];
});
} else {
NSLog(@"Access was not granted");
}
if (addressBook != NULL) CFRelease(addressBook);
});

关于ios - 巨大的延迟(没有错误!)仅在请求地址簿权限后第一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24303467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com