0

I have got a address book data into the tableView in my application.This code is running in simulator successfully. i got the data of contacts in the tableView of my app in simulator but when i test this in my device then data of contacts didn't get. I used 6.1 simulator and iPhone device ios 6.1.3. Please suggest me what is the actual problem in this code ? This code is run in simulator successfully but it's code didn't run in iPhone device iOS 6.1.3

Thanks You

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.
}

Person.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

Person.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 ];


}
Rahul Saini
  • 921
  • 2
  • 10
  • 23
  • 1
    What do you mean by `but it's code didn't run in iPhone device`? Post the error message you're getting. – Sergey Grischyov Sep 20 '13 at 16:09
  • This code run on simulator successfully i got data in contacts in table view but when i test on device i see contact data does not get in the tableView. – Rahul Saini Sep 20 '13 at 16:13
  • Have you tried stepping through the method in the debugger and seeing at what point your implementation doesn't behave as you expect it to? – Abizern Sep 20 '13 at 16:21
  • thanks for kindly reply i check this on device to debug. – Rahul Saini Sep 20 '13 at 16:25
  • @Abizern i debug this code on device, I see class ClsAddressBookViewController.m cursor does not enter inside in the for loop "for (i = 0; i < [allContacts count]; i++)" but when i test this code on simulator then cursor is enter inside in the for loop. – Rahul Saini Sep 20 '13 at 16:41
  • Do you actually have any contacts on your device? Check that `allContacts` actually contains something. – Abizern Sep 20 '13 at 16:42
  • yes i have saved contacts information in my device. – Rahul Saini Sep 20 '13 at 16:45
  • Finally My Problem is sloved it's link is nice. http://stackoverflow.com/questions/12648244/programmatically-request-access-to-contacts-in-ios-6 – Rahul Saini Sep 21 '13 at 07:31
  • It's also a good link. http://stackoverflow.com/questions/12810638/app-crashed-in-ios-6-when-user-changes-contacts-access-permissions – Rahul Saini Sep 21 '13 at 07:40

3 Answers3

1

From IOS 6. it need to authorize your apps for to access system datas : In settings on your IPhone in confidentiality tab and contact section, verify if your app has acces is "on"

tdelepine
  • 1,986
  • 1
  • 13
  • 19
  • @tdelephine please explain me how i change my iPhone settings to access the contact data in my app. how to verify my app access is "on" ? can you explain me. Thanks you. – Rahul Saini Sep 21 '13 at 05:20
  • 1
    "ON" signifi is activate, and you can verify it by code with the others answer – tdelepine Sep 21 '13 at 07:09
1

To continue TDelepine's suggestion, perhaps in your getPersonOutOfAddressBook method, you can check if your app has AddressBook access. The code below will force the device to ask the user for access if access status has not been determined:

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

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

       if(granted) {
            NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
            etc...
        }
    });
}
Mark Semsel
  • 7,125
  • 3
  • 29
  • 27
1

I think you're not sending request for access Address Book. One other thing If you're not using this then It's work properly in simulator but not working in iPhone

Use this code in 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.
}
Flexo
  • 87,323
  • 22
  • 191
  • 272
Jogendra.Com
  • 6,394
  • 2
  • 28
  • 35