libphonenumber for iOS or objective-c port
Asked Answered
M

3

13

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.

Metamer answered 31/1, 2012 at 10:53 Comment(8)
how did you recompile it and what are the dependencies you have?Psychognosis
First I appened "i386" to "CMAKE_OSX_ARCHITECTURES:STRING=" in the "CMakeCache.txt" file for cmake process, and then recompile. The dependent libraries are boost, protobuf, and googletest.Metamer
Did you ever have any success porting the libphonenumber framework to Objective C? I am in need of exactly the same for my iOS app. If you would be so kind to share it with me, that would be great! Thanks in advance.Laconic
Check out my update above, I ended up using the libphonenumber javascript library in combination with UIWebview and stringByEvaluatingJavaScriptFromString to parse my numberMetamer
First of all, why do you need a C/C++ library, when you've got NSFormatter, automatic data detectors etc.? (No offense, just asking)Collocutor
Can you please go into more detail about what needs to be done to get the JavaScript solution working. I am trying to implement it myself and coming up with the result as an empty string. I could REALLY use your help to get this solution working myself. Your code sample simply does not work for me - along with many other variations I have tried here.Jael
@YoCoh : I have implemented this libphonenumber in my application. Which is a html file. In that there is function "validNumber" which takes two arguments phone number & country code. I have done the same thing as you have shown in your post, but some how I am getting empty string every time. Even if my number is valid its not showing any result. What should I do.Yordan
Can you provide a full example? I have added the compiled version 300k and the loaded it to the webview but when i call the methods it never answers anything. This script returns nothing: phoneNumberParser('NUMBER HERE','PT','')Emerald
B
6

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

Benson answered 19/2, 2013 at 17:2 Comment(2)
Does this include the very essential AsYouTypeFormatter?Iain
Why that iOS library is so slow? I'm using it to find duplicate contacts and it takes 23 sec to process 119 entries. In contrast, if I compare names, processing takes only 1 second.Amethist
G
3

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);
Gigi answered 21/5, 2012 at 9:13 Comment(1)
Can you provide a full example? I have added the compiled version 300k and the loaded it to the webview but when i call the methods it never answers anything. This script returns nothing: phoneNumberParser('NUMBER HERE','PT','')Emerald
E
2

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.

Ency answered 12/3, 2012 at 15:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.