In this tutorial, we will be creating a contacts app for iOS that uses
the Sinch SDK to make
app-to-phone calls.
To get the most out of this tutorial, you will need a basic
understanding of the following areas:

XCode and Storyboard

Objective-C

Address Book API

CocoaPods

You’ll also want a phone close by for testing later in the tutorial.

At the end of the tutorial, you will have something that looks a bit
like this:

Open up XCode and create a new project using the Master-Detail
Application template. This template will give us a nice base to start
with, as it already has some of the components we need. Make sure that
you choose Objective-C under language and iPhone under device.

We’ll want to set up CocoaPods to work with our app. Open the directory
for the project you just created in Terminal and type:

podinit

Then open the file named Podfile and add the following line:

pod ‘SinchRTC’

After saving the file, install all the tools you will need by simply
typing:

podinstall

After this, you’ll see an XCode workspace file with the extension
“.xcworkspace”. We’ll need to work out of that file from now on
instead of our project file. That way, all of our app’s components will
work together.

If you aren’t familiar with the default Master-Detail Application
template, build and run to play around with the app. You can see that we
populate a table using the plus button and can select an entry to see a
detailed view.

To get an idea of what we’re aiming for, we want our app to pre-populate
the table with contacts using the Address Book API. After that, we
should be able to select an entry to see that contact’s phone number(s),
which we can then select to make a call.

First, go to MasterViewController.h and add the Address Book import
statement:

#import <AddressBook/AddressBook.h>

Now, head over to MasterViewController.m. Delete the method
insertNewObject and add the following method:

-(void)fillContacts{}

Now go to viewDidLoad. We first want to check if we can access the
user’s contacts. If so, we can call fillContacts to populate our table.
Delete all the code in viewDidLoad except for
[superviewDidLoad] and add the following code at the end:

if(ABAddressBookGetAuthorizationStatus()==kABAuthorizationStatusDenied||ABAddressBookGetAuthorizationStatus()==kABAuthorizationStatusRestricted){UIAlertView*cantAddContactAlert=[[UIAlertViewalloc]initWithTitle:@"Cannot Add Contact"message:@"You must give the app permission to add the contact first."delegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil];[cantAddContactAlertshow];}elseif(ABAddressBookGetAuthorizationStatus()==kABAuthorizationStatusAuthorized){[selffillContacts];}else{ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL,nil),^(boolgranted,CFErrorReferror){dispatch_async(dispatch_get_main_queue(),^{if(!granted){UIAlertView*cantAddContactAlert=[[UIAlertViewalloc]initWithTitle:@"Cannot Add Contact"message:@"You must give the app permission to add the contact first."delegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil];[cantAddContactAlertshow];return;}[selffillContacts];});});}

Here we see if our app can use the user’s contacts. If not, we let the
user know. If so, we go ahead and call fillContacts. If the user
hasn’t specified whether or not our app has permission to use his or her
contacts, we ask them and respond appropriately.

Now we need a way to store contact information. To do this, go to
File>New>File… and create a new Cocoa Touch Class. I always prepend my
class names with a “C”, so I’ll name the class “CContact” since we
want a class to hold contact information. Make sure the subclass is
NSObject, the language Objective-C.

For this app, we’ll only need four pieces of information: first name,
last name, mobile number, and home number. Head over to CContact.h
and add these properties and this class method to the interface:

Here, we get all of the contacts, go through them in a for-loop, and
check if each one has more than one number. Now, replace the //1
comment with:

fName=(__bridgeNSString*)ABRecordCopyValue(thisContact,kABPersonFirstNameProperty);lName=(__bridgeNSString*)ABRecordCopyValue(thisContact,kABPersonLastNameProperty);for(inti=0;i<ABMultiValueGetCount(phonesRef);i++){CFStringRefphoneLabel=ABMultiValueCopyLabelAtIndex(phonesRef,i);CFStringRefphoneValue=ABMultiValueCopyValueAtIndex(phonesRef,i);NSString*phoneNumber=(__bridgeNSString*)phoneValue;// parse out unwanted characters in numberNSString*parsedPhoneNumber;for(inti=0;i<phoneNumber.length;i++){charcurrentChar=[phoneNumbercharacterAtIndex:i];if(currentChar>='0'&&currentChar<='9'){NSString*digitString=[NSStringstringWithFormat:@"%c",currentChar];if(parsedPhoneNumber.length==0){parsedPhoneNumber=[NSStringstringWithString:digitString];}else{NSString*newString=[parsedPhoneNumberstringByAppendingString:digitString];parsedPhoneNumber=[NSStringstringWithString:newString];}}}// check if number is foreignif((parsedPhoneNumber.length<11)||(parsedPhoneNumber.length==11&&[parsedPhoneNumberhasPrefix:@"1"])){numberCount++;if(CFStringCompare(phoneLabel,kABPersonPhoneMobileLabel,0)==kCFCompareEqualTo){mNumber=phoneNumber;}if(CFStringCompare(phoneLabel,kABHomeLabel,0)==kCFCompareEqualTo){hNumber=phoneNumber;}}CFRelease(phoneLabel);CFRelease(phoneValue);}// 2

Wow, that’s a lot! Let’s take a look at what we’ve just added.

First we get our contact’s first and last name. We have to do a bit of
work getting the values as strings, as they are given to us as
ABRecordRef objects. We then enter a for-loop to look at all of our
current contact’s phone numbers. We get the phone number and its label.

Next, we need to do a bit of manipulation to see if our number is a U.S.
number. The number is given to us as it appears on the phone, so we’ll
need to parse the number to get rid of any non-digit characters. We then
check to see if the number is a U.S. number. If it is, we can store it
depending on its label. Take note of the numberCount variable that we
increment once we’ve added a number.

This is where we add our current contact to the UITableView. We put this
code in an if-statement using the numberCount variable in the case that
a contact has more than one number, but none of them are U.S. numbers.

We’re done with MasterViewController.m. Build and run to see if you
get a list of contacts.

Now go to Main.storybaord. First, change the title label in the
Master view from “Master” to “Contacts”. Then, scroll to the right so
you can see the Detail view. Delete the Label that’s currently there and
add a Table View that fills the whole screen. We’re going to use Auto
Layout to make sure our Table View’s position and size proportions stay
constant between devices and phone orientations.

Select the Table View by clicking on it. Then go to Editor>Pin and
select “Bottom Space to Superview.”

Do this for “Top Space to Superview,” “Trailing Space to Superview,” and
“Leading Space to Superview.” Remember to select the Table View before
adding each pin. If done correctly, you’ll get a nice blue outline
around your Table View, indicating that your Auto Layout pins were
placed properly.

Then, add a Table View Cell to the Table View:

Now, switch to the Assistant Editor and have your right pane display
DetailViewController.h. Go ahead and delete the
detailDiscriptionLabel. Hold down control and click and drag from the
Table View to your interface to add a UITableView property. We’re going
to display the selected contact’s numbers here, so give the table an
appropriate name, such as numberTable.

Do the same with the Detail title, which will be a UINavigationItem.
Call it something descriptive like contactTitle.

We’ll want to make a call once we select a person’s phone number. If you
haven’t already, go to sinch.com and sign
up for free. Then, go to your dashboard and go to your apps. Create a
new sandbox app. Take note of your app’s unique key and secret.

You now have a fully functional
app-to-phone calling
app that uses the Sinch SDK. You can also add more features to your app;
Sinch offers a number of other services, such as SMS and app-to-app
calling, which are simple to implement in your app.