0

我在我的应用程序的 tableView 中获得了地址簿数据。此代码在模拟器中成功运行。我在模拟器中的应用程序的 tableView 中获得了联系人的数据,但是当我在我的设备中进行测试时,联系人的数据没有得到。我使用了 6.1 模拟器和 iPhone 设备 ios 6.1.3。请建议我这段代码中的实际问题是什么?此代码在模拟器中成功运行,但它的代码未在 iPhone 设备 iOS 6.1.3 中运行

感谢您

ClsMainPageAppDelegate.h

#import <UIKit/UIKit.h>
#import "Person.h"

@interface ClsMainPageAppDelegate : UIResponder <UIApplicationDelegate>
{
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) Person *objPerson;
@end

ClsMainPageAppDelegate.m

#import "ClsMainPageAppDelegate.h"
#import "ClsMainPageViewController.h"

@implementation ClsMainPageAppDelegate
@synthesize objPerson;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
}

人.h

#import <Foundation/Foundation.h>
@interface Person : NSObject

@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic, strong) NSString *fullName; 
@property (nonatomic, strong) NSString *phoneNumber;
@property (nonatomic, strong) NSString *workEmail;

@end

人.m

#import "Person.h"

@implementation Person


- (id)init
{
   self = [super init];
   if (self) 
   {

   }
   return self;
 }

@end

ClsAddressBookViewController.h

#import <UIKit/UIKit.h>

@interface ClsAddressBookViewController : UIViewController

- (IBAction)backcontacts:(id)sender;
@end

ClsAddressBookViewController.m

#import "ClsAddressBookViewController.h"
#import "ClsUpdateNetworkViewController.h"
#import "ClsMainPageAppDelegate.h"
#import "Person.h"
#import <AddressBook/AddressBook.h>


@interface ClsAddressBookViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *tableData;

@end

@implementation ClsAddressBookViewController

@synthesize tableData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
   if (self) {
    // Custom initialization
   }
   return self;
}

- (void)viewDidLoad
{
   [super viewDidLoad];
// Do any additional setup after loading the view.

 self.title = @"Contacts";
 self.tableData = [[NSMutableArray alloc]init];
 [self getPersonOutOfAddressBook];

}

- (void)getPersonOutOfAddressBook
{
 CFErrorRef error = NULL;

 ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

if (addressBook != nil)
{
    NSLog(@"Succesful.");

    NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

    NSUInteger i = 0;
    for (i = 0; i < [allContacts count]; i++)
    {
        Person *person = [[Person alloc] init];

        ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];

        NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
        NSString *lastName =  (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);


        NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];

        person.firstName = firstName;
        person.lastName = lastName;
        person.fullName = fullName;


        //Mobile Number
        ABMultiValueRef mobilenum = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
        NSUInteger k = 0;
        for(k = 0; k < ABMultiValueGetCount(mobilenum); k++)
        {
            NSString *mobile = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(mobilenum, k);
            if(k == 0)
            {
                person.phoneNumber = mobile;
                NSLog(@"person.mobilenum = %@", person.phoneNumber);
            }
            else if (k==1)
                person.phoneNumber = mobile;
        }

        [self.tableData addObject:person];
    }
}

CFRelease(addressBook);
}

#pragma mark TableView Delegate

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return [self.tableData count];    
}

 -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

   static NSString *cellIdentifier = @"Identifier";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

Person *person = [self.tableData objectAtIndex:indexPath.row];

cell.textLabel.text = person.fullName;

return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   Person *person = [self.tableData objectAtIndex:indexPath.row];


ClsMainPageAppDelegate *objdelegate = [[UIApplication sharedApplication]delegate];
objdelegate.objPerson = person;

UIStoryboard *storybrd = self.storyboard;
ClsAddressBookViewController  *svc = [storybrd instantiateViewControllerWithIdentifier:@"clsUpdatecontrol"];
[self presentViewController:svc animated:YES completion:nil ];


}
4

3 回答 3

1

从 IOS 6. 它需要授权您的应用程序访问系统数据:在您的 iPhone 的机密选项卡和联系人部分的设置中,验证您的应用程序是否有访问权限为“开启”

于 2013-09-20T17:04:45.400 回答
1

要继续 TDelepine 的建议,也许在您的 getPersonOutOfAddressBook 方法中,您可以检查您的应用程序是否具有地址簿访问权限。如果访问状态尚未确定,下面的代码将强制设备要求用户访问:

 ...
NSLog(@"Succesful.");

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {

       if(granted) {
            NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
            etc...
        }
    });
}
于 2013-09-20T17:16:51.267 回答
1

我认为您没有发送访问地址簿的请求。另一件事如果您不使用它,那么它可以在模拟器中正常工作,但不能在 iPhone 中工作

在 ViewDidLoad 中使用此代码

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
        accessGranted = granted;
        dispatch_semaphore_signal(sema);
    });
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    //  dispatch_release(sema); 
}
else { // we're on iOS 5 or older
    accessGranted = YES;
}

if (accessGranted) {
    // Do whatever you want here.
}
于 2013-09-21T06:29:58.383 回答