Unable to read contact sort order in iOS (address book API seems broken) - iphone

I'm trying to use ABPersonGetCompositeNameFormat() to read the sort order settings for the user's address book. It always returns kABPersonCompositeNameFormatFirstNameFirst regardless of how I configure the "Display Order" preference in the Settings application. The Address Book application changes appropriately when the display order is set to "Last First" but the API call always returns the same value that represents the "First Last" display order. Has anybody else had this problem? I'm running this on an iPhone4 with iOS 4.3.3.

I've just found a solution - ABAddressBookRef has to be obtained (by ABAddressBookCreate) at least once before calling to ABPersonGetCompositeNameFormat(), but there are no mentions about it in the documentation.

ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
people = (NSArray*)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByLastName);
It helps you.

Related

How to create a contact in the "local" addressbook?

I am wanting to create a contact programmatically into the "local" addressbook (so that it doesn't try to synch, which causes some compatibility issues with Exchange).
If a local addressbook already exists, I can find it using ABAddressBookCopyArrayOfAllSources to get all the sources in the Address Book, then look for the ABRecordRef with a sourceTypeRef of "kABSourceTypeLocal"- if I then pass that recordRef to ABPersonCreateInSource, I can add a record to the local directory.
Does anyone have any suggestions as to how I should best go about creating a record in the "local addressBook", if there ISN'T a local addressbook already?
(also, how could I do this pre-iOS4, as the above calls weren't available then?)
Thanks
Peter
You should take a look at this post:
Obtaining Specific ABSource from ABAddressBook in iOS 4+
that demonstrates how to identify and target specific sources (ABSource) within the ABAddressBook. While this code mentions the function, ABGroupCreateInSource(), there is a similar function, ABPersonCreateInSource() for working with persons.
I found a simple workaround.
Since you can't see/add local contacts when you don't have any pre-existing local contact, the following workaround will be safe to use:
Let's pretend you're using Gmail (Exchange)
Go to your Mail settings for Gmail and disable the Contact sync
When prompted whether you want to keep or remove the Gmail
contacts from your phone, choose to REMOVE them (don't worry, they
won't be removed from Gmail)
Go back to your contacts. You can now create one LOCAL contact
Finally, go back to Settings/Mail/Gmail and re-enable the
Contacts sync
Voila, your Gmail contacts are back into your phone and you now
have 1 local contact that enables you to access the Local contacts
group in addition to the Gmail contacts group.
Hope that helps (I just had the same problem and found this workaround that works great for me).
Ben.

iPhone link to map directions using string "current location" not lat and lng for saddr

I know there are numerous questions about how to construct a sharedApplication link to the maps app specifying the start and end address with coordinates. I've got that working no problem.
Has anyone found a way to link with a source address generically specified as "current location".
I ask because the scenario I'm working on having Core Location or a UIMapView would only be necessary in order to determine the user location prior to handing off to the map app where the user locating would seem to just happen again.
I've tried throwing UTF8'd "Current Location" and "[Current Location]" into the saddr parameter which amusingly starts from Current Montana. I've also tried simply excluding the saddr param.
I know this is often tread territory but this particular situation wasn't covered by anything I found searching here or on mapki. Before I add the core location code I just wanted to make sure there wasn't a more limited way to tackle this.
Thanks
This post is quite old, but recently I had to face a similar issue and I developed a solution.
Using "Current Location" (properly escaped) as source address works perfectly.
The downside is that "Current Location" is valid for English language only. If your iPhone is set to use another language, you need to know the exact translation that Apple chose for that particular language.
I wrote a class that can provide the exact translation for "Current Location" in any supported language. You can read my blog post about this.
Hope this helps.

iPhone sdk: How do I get a count of all the phone numbers in a address book?

Hi i am developing an app for my QA department. I need to programically get how many phone numbers are there in the entire address book. No user input. Just click a button and then get how many phonenumbers are there in the ENTIRE addressbook.
I think what you are looking for is ABGroupCopyArrayOfAllMembers. See the AddressBook Programming Guide and the QuickContacts example for more info on using the AB framework.
from what I can tell from a quick look at ABMultiValue in AddressBook.sqlitedb, you might get it like this:
1 - sqlite to /var/mobile/Library/AddressBook/AddressBook.sqlitedb
2 - SELECT value FROM ABMultiValue => into some array
3 - count everything in there that matches /^[0-9 +]+$/
look around for some more help doing these steps
Edit: there's probably also some way to do it with this method:
How do you get a persons phone number from the address book?

Discovering the user's first and last names?

I am trying to programmatically discover the first and last names of the iPhone user. Is this possible at all? Calling this ...
getpwuid( getuid() )->pw_gecos == "Mobile User"
..alas. Iterating over the address book finds all address book records, but doesn't distinguish between the device owner and anyone else (that I can tell).
Given that the Mac desktop address book decorates the owners icon with a 'me' overlay in the bottom-left, I'm hoping that sort of information is available in the iPhone version.
Martin.
As noted in the other thread, there is NSFullUserName() - this call does come up in the iPhone documentation so it should work on the device. Simply parse out the first and last name from that.
See similar question - there is no public API for that.

Is there any way to determine if the iphone is roaming?

I am working on an iPhone application and would really like to determine if the device is roaming so that I can intelligently avoid costing my users expensive connections if out of their home network.
The application I am writing is for jailbroken phones, however I would prefer to use standard SDKs if possible.
Here is what I've already found:
1. Apple SDKs:
In the apple documentation, I found the promise in Apple's SCNetworkReachability API. The API provides access to such things as whether you are on a WIFI or a cell phone network, whether a network connection is currently established, etc. However searching the SCNetworkReachability API reference pdf for 'roam' or 'roaming' both turn up nil. So does their sample code.
2. Grep of a Jailbroken iPhone FS:
The preferences -> general -> networking tab is where users can turn on or off their roaming. Looking in the plist file for this ("/Applications/Preferences/Network.plist") I was able to find the following references:
PostNotification = "com.apple.commcenter.InternationalRoamingEDGE.changed";
cell = PSSwitchCell;
default = 1;
defaults = "com.apple.commcenter";
key = InternationalRoamingEDGE;
label = "EDGE_ROAMING_TOGGLE";
requiredCapabilities = (
telephony
);
This is certainly a lead, as it appears I can sign up for notifications that the user has changed the InternationalRoaming settings. Still, I'm not certain how to turn this into the knowledge that they are in fact, presently roaming.
3. Check the class dumped sources of SpringBoard:
I've dumped the classes of SpringBoard using class-dump. I was unable to find any references to 'roam' or 'roaming'
4. Obviously I started by checking at SO for this:
Couldn't find anything related.
Further steps: Does anyone have any advice here?
I know this is possible. Apple clearly has made it very difficult to find however. I highly doubt this is possible without using a private framework. (such as CoreTelephony). As this is a jailbroken app, I may resort to screen-scraping the the carrier name with injected code in the SpringBoard, but I would really rather prefer not to go that route. Any advice much appreciated. Thanks.
There is! It's not documented at all, and I highly doubt this would work on a non-jailbroken phone (as it requires using files not in the sandbox). However, here is how it is done.
The iPhone file system keeps two softlinks:
static NSString *carrierPListSymLinkPath = #"/var/mobile/Library/Preferences/com.apple.carrier.plist";
static NSString *operatorPListSymLinkPath = #"/var/mobile/Library/Preferences/com.apple.operator.plist";
when these links are pointing at the same file, the telephone is not roaming. When pointing at different files, the telephone is romaing.
Simplified code (no error checking, etc):
- (BOOL)isRoaming
{
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error;
NSString *carrierPListPath = [fm destinationOfSymbolicLinkAtPath:carrierPListSymLinkPath error:&error];
NSString *operatorPListPath = [fm destinationOfSymbolicLinkAtPath:operatorPListSymLinkPath error:&error];
return (![operatorPListPath isEqualToString:carrierPListPath]);
}
the solution of the symlinks are not the only way to do it but definitely it is the best. As I just realized, the Strings returned contains the MCC and MNC codes of the operator and carrier!!! Even the core telephony framework is not able to retrieve those informations about the operator your iPhone is attached when in roaming.
Logs:
carrier: /System/Library/Carrier Bundles/iPhone/72410/carrier.plist
operator: /System/Library/Carrier Bundles/iPhone/20810/carrier.plist
As you can see, the carrier (original cellular provider) line contains the file inside the "folder" 72410, which means MCC 724 (Brazil) and MNC 10 (VIVO).
The operator (actually the one my cell phone is attached now - i'm in roaming) is inside the folder 20810, which means MCC 208 (France) and MNC 10 (SFR).
By the way, I'm using iPhone 4 with iOS5.
On a non-jailbreak device you can use third party services like http://ipinfo.io (my own service) to find out the current country of even carrier code based on the device's IP address, and you can then compare that to the CTCarrier details to determine if the device is roaming. Here's the standard ipinfo.io API response:
$ curl ipinfo.io/24.32.148.1
{
"ip": "24.32.148.1",
"hostname": "doc-24-32-148-1.pecos.tx.cebridge.net",
"city": "Pecos",
"region": "Texas",
"country": "US",
"loc": "31.3086,-103.5892",
"org": "AS7018 AT&T Services, Inc.",
"postal": "79772"
}
Custom packages are available that also include the mnc/mcc details of mobile IPs though. See http://ipinfo.io/developers for details.