-1

In my app I want to take the phone number of my user but I have a restriction of how the phone number starts, this is for Greece but I don't want my users outside of Greece to have this restriction.

So is there any way to accomplish this?

Thanks in advance.

mike vorisis
  • 2,786
  • 6
  • 40
  • 74
  • you can use a geolocation database to look up the user's location. It is not 100% reliable, but probably good enough for most applications/situations. – john elemans Dec 15 '16 at 17:16
  • Thanks for your comment @johnelemans but if the user doesn't accept the dialog for user location then I couldn't take his location – mike vorisis Dec 15 '16 at 17:18
  • You can with the ip address. it doesn't use location services. – john elemans Dec 15 '16 at 17:19
  • Oh do you know any resource I can check it out – mike vorisis Dec 15 '16 at 17:22
  • here's one; http://lite.ip2location.com/ – john elemans Dec 15 '16 at 17:23
  • But what happens when the user is traveling? A user with a Greek phone number still has a Greek phone number if they're in Miami. – BSMP Dec 15 '16 at 17:29
  • You are right @BSMP but its no so important to don't have a restriction to some users (outside of greece there will not be a restriction) so a greek phone number will be also can be entered – mike vorisis Dec 15 '16 at 17:31
  • @BSMP of course if you re thinking another way I ll be glad if you share it – mike vorisis Dec 15 '16 at 17:32
  • See if this helps [Get Language Code](http://stackoverflow.com/questions/29193284/check-language-in-ios-app) – MwcsMac Dec 15 '16 at 17:37
  • @MwcsMac thanks for your comment Is this returns the language or the region that the user have registered to his device – mike vorisis Dec 15 '16 at 17:48
  • One option is to ask the user to enter their phone number and send a verification code to check that they actually owns that number. [Apple does not allow you to get the user phone's number](http://stackoverflow.com/questions/193182/programmatically-get-own-phone-number-in-ios) – Code Different Dec 15 '16 at 22:13
  • After i search a little i found something (how to get the region that the user selected when he activated his phone(not the system lang)) i will post it tommorrow. @CodeDifferent do i have to pay for the verification? – mike vorisis Dec 15 '16 at 22:17
  • You can use the user's region code but it can be easily changed. Sending text messages will incur a small cost. Check around for one that suits you best. Examples: [Twilio](https://www.twilio.com), [Authy](https://www.authy.com/developers/), [Amazon](https://aws.amazon.com/iam/details/mfa/), etc. – Code Different Dec 15 '16 at 22:25

2 Answers2

0

After considering what we said in comments I found a way to do it. I use the country that the user registered when he first open his phone like that:

var localeArray = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    localeArray = NSLocale.currentLocale().localeIdentifier.componentsSeparatedByString("_")


        }


func takeResult() {

   if localeArray[1] == "GR" {

       //do what you want
   }else {

      //handle if the user isn't in the country you want
   }
}

The first result of localArray (localArray[0]) is the system language that the user has.

EDIT FOR SWIFT 3

This is how you get the same result with swift 3 found it here

let locale = Locale.current
print(locale.regionCode)
mike vorisis
  • 2,786
  • 6
  • 40
  • 74
0

NSLocale is just a setting about currently used regional settings, it doesn't mean the actual country you're in.

Use CLLocationManager to get current location & CLGeocoder to perform reverse-geocoding. You can get country name from there.

Jatin Chauhan
  • 1,107
  • 1
  • 8
  • 21
  • Thanks for the answer but first If the user don't accept the location privacy I will not have his country, and then If the user has set the country Greece then I know he will have a greek number so If someone is for vacation but has greek number I want the restriction – mike vorisis Dec 16 '16 at 13:43