Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libphonenumber for iOS or objective-c port

My goal is to use libphonenumber, google's phone number handling library for an iPhone project I'm working on.

After downloading it (and many many hours), I complied the C++ version of the library, and it built a number ".a" files and ".dylib" files, of which I assumed I must import into my xCodeProject in order to access those C++ functions.

So I imported "libphonenumber.a" into my project, updated my target, build settings, build phases, and Library Search Paths as needed.

Building the xCode project for Device & Simulator pass, however give me the following warning: "ld: warning: ignoring file ../XcodeProjects/libphonenumber/build/libphonenumber.dylib, file was built for unsupported file format which is not the architecture being linked (armv7)". (or i386 when compiling for simulator)

I understand from this that I must compile the libphonenumber for the correct i386 and/or armv7 architecture. So I tried to do that, but quickly realized this requires me to also rebuild libphonenumber's 3 dependent libraries as well, for the i386/armv7 architectures in order for libphonenumber's to now properly compile. Eventually, I gave up on that, it started to look like a big mess ahead of me.

After all my trials, I'm left with

3 Questions:

1) How to I compile libphonenumber C++ library for use with i386/armv6/armv7 architectures.

2) When using a c++ library, is my assumption correct? Is it a matter of simply importing the ".a" file that results from the compilation, and just point to it in the header of my xCode project files? What exactly are the steps for including and using c++ libraries and accessing their functions from objective-c inside xCode?

3) I did find LPNKit, an objective-c port for libphonenumber, however it is incomplete. Has anyone heard of it, and had any luck using it? Or does anyone have an objective-c port for libphonenumber that is complete, working, with instructions on how to compile and install it correctly?

Any help or advice on how to get this library working on iOS would be greatly appreciated.

Update:

I ended up using the javascript version of libphonenumber, adding all the files to my bundle, including all referenced javascript libraries and using UIWebview and stringByEvaluatingJavaScriptFromString to run functions. You could also have the UIWebview reference the javascript library online (I just preferred to have everything local as not to depend on an internet connection).

Here is a sample of what I did:

webView_ = [[UIWebView alloc] init];
[webView_ loadHTMLString:
 @"<script src='base.js'></script>" 
 "<script>"
 "goog.require('goog.dom');"
 "goog.require('goog.json');"
 "goog.require('goog.proto2.ObjectSerializer');"
 "goog.require('goog.string.StringBuffer');"
 "</script>"
 "<script src=\"phonemetadata.pb.js\"></script>"
 "<script src=\"phonenumber.pb.js\"></script>"
 "<script src=\"metadata.js\"></script>"
 "<script src=\"phonenumberutil.js\"></script>"
 "<script src=\"asyoutypeformatter.js\"></script>"
 "<script src=\"normalize.js\"></script>"
                 baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];


NSString *function = [[NSString alloc] initWithFormat: @"phoneNumberParser('%@','%@','')", phoneNumber, ISOCountryCode];
NSLog(@"function is: %@", function);

NSString *result =[webView_ stringByEvaluatingJavaScriptFromString:function];

The result variable gets me the formatted number.

I hope that helps anyone who ran into the same issue I did.

like image 802
cohen72 Avatar asked Jan 31 '12 10:01

cohen72


3 Answers

It has been ported by our team. You can find it https://github.com/me2day/libPhoneNumber-iOS

like image 127
hyukhur Avatar answered Nov 11 '22 04:11

hyukhur


Just note that libphonenumber Javascript library includes Google's closure library.

So you should consider, wrapping your Javascript call in a Javascript object and then compile it using closure builder in order to get an efficient and light weight script. (closure library before compilation : 18Mb, after compilation 300Kb !)

See above a sample of such a wrapper

goog.provide('sphone.phonenumber');

goog.require('goog.dom');
goog.require('goog.json');
goog.require('goog.proto2.ObjectSerializer');
goog.require('goog.array');
goog.require('goog.proto2.PbLiteSerializer');
goog.require('goog.string');
goog.require('goog.proto2.Message');
goog.require('goog.string.StringBuffer');

goog.require('i18n.phonenumbers.NumberFormat');
goog.require('i18n.phonenumbers.PhoneNumber');
goog.require('i18n.phonenumbers.PhoneNumberUtil');


sphone.phonenumber = function(phoneNumber, regionCode) {
    this.getCountryCallCode=phonenumberGetCountryCallCode;
};

function phonenumberGetCountryCallCode(phoneNumber, regionCode) {
   var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
    var number = phoneUtil.parseAndKeepRawInput(phoneNumber, regionCode);
    return number.getCountryCode();
};


// Ensures the symbol will be visible after compiler renaming.
goog.exportSymbol('sphone.phonenumber', sphone.phonenumber);
like image 32
Manitoba Avatar answered Nov 11 '22 04:11

Manitoba


1) Fix all the errors and re-compile all the dependencies for arm. As you said before.

2) Yes. According to kstenerud’s iOS-Universal-Framework

Using an iOS Framework iOS frameworks are basically the same as regular dynamic Mac OS X frameworks, except they are statically linked.

To add a framework to your project, simply drag it into your project. When including headers from your framework, remember to use angle bracket syntax rather than quotes.

For example, with framework "MyFramework":

#import <MyFramework/MyClass.h>

This question Importing an external library in xcode - C++ follows like this: Import C++ library, use it, get errors from its dependency on built-in frameworks, import those built-in frameworks, everything works.

3) I would invest in LPNKit instead of fighting your way through option 1. You can both contribute and benefit from LPNKit. GitHub is a wonderful place and the great boost of this over option 1 is that you have another person (or people!) who have the same goal and will all work together to achieve it.

like image 2
JoePasq Avatar answered Nov 11 '22 04:11

JoePasq