Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

petermod

macrumors newbie
Original poster
Jul 28, 2015
3
0
I'm trying to develop an iPhone app and implementing on it a functionality similar to snapchat's and so many other apps: i want to ask for the user's mobile phone number and add it to my database. I then want to ask for permission to access my address book and based on my mobile phone contacts (friends mobile number) i can suggest the profiles of my friends who have installed the app. I developed some functions in which I was able to get all the user's contacts as a string. But i have some issues. People can write the mobile numbers in different ways (putting the country code in first place, etc). Since i am matching an exact string it becomes kind of hard.

Is there any solution for this problem? I know that different countries have different ways of writing the mobile numbers. F.e. in Portugal we can write 00351 911 111 111 or +351 911 111 111 or 911 111 111 . This will be easy to solve in portugal since we all have the last 9 digits and it is easy to compare. But in the US i have seen (555) XXX-XXXX or XXX-XXX-XXXX and many other formats.. I can't determine how the user has saved the contact and the same with other countries.

ALL HAPPENS AFTER ASKING FOR AN AUTHORIZATION

In the viewDidLoad i search in Parse the users of my mobile phone

Code:
override func viewDidAppear(animated: Bool) {

        var query = PFUser.query()

        query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

            if let users = objects {

                self.contactsUsernameArray.removeAll(keepCapacity: false)
                self.contactsNameArray.removeAll(keepCapacity: false)
                self.contactsImageFiles.removeAll(keepCapacity: false)
                self.contactsNumberArray.removeAll(keepCapacity: false)

                for object in users {

                            for var i = 0; i < self.phoneNumbers.count - 1; i++ {

                                if object["phoneNumber"] as! String == self.phoneNumbers[i] {

                                    self.contactsUsernameArray.append(object.username!!)
                                    self.contactsNameArray.append(object["name"] as! String)
                                    self.contactsNumberArray.append(object["phoneNumber"] as! String)
                                    self.contactsImageFiles.append(object["photo"] as! PFFile)

                        }

                    }

                }

            }

            self.contactsTable.reloadData()

        })

    }
 
I do not see a big problem. Once you know how many digits occur after the country code you can separate two blocks: country code and digits. Ditch all non numbers as spaces, plus, brackets etc. Ditching could be done with a simple if clause. Something like

Code:
var newDigitString: String =""
for digit in digits {
if digit in 0...9 { newDigitString.append(digit) }
}

Could be you only need two points between 0 and 9, I always forget which includes the 9 too.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.