Get device location (only country) in iOS
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.
Find current country from iPhone device
To find the country of the user's chosen language:
NSLocale *currentLocale = [NSLocale currentLocale]; // get the current locale.
NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
// get country code, e.g. ES (Spain), FR (France), etc.
In Swift:
let currentLocale = NSLocale.currentLocale()
let countryCode = currentLocale.objectForKey(NSLocaleCountryCode) as? String
If you want to find the country code of the current timezone, see @chings228's answer.
If you want to find the country code of the device's physical location, you will need CoreLocation with reverse geocoding. See this question for detail: How can I get current location from user in iOS
How to get current country of Device in React Native (iOS and Android)?
According to the documentation of react-native-device-info for latest version, they have moved some of their apis to react-native-localize to reduce duplication in the react-native-community modules. react-native-localize worked perfectly for me.
Setup:
$ npm install --save react-native-localize
# --- or ---
$ yarn add react-native-localize
Usage:
import * as RNLocalize from "react-native-localize";
console.log(RNLocalize.getLocales());
console.log(RNLocalize.getCurrencies());
console.log(RNLocalize.getCountry());
console.log(RNLocalize.getCalendar());
console.log(RNLocalize.getTemperatureUnit());
console.log(RNLocalize.getTimeZone());
console.log(RNLocalize.uses24HourClock());
and many more. For detailed description please visit their official documentation by the given link: react-native-localize
Determine Country associated with Apple ID logged into the iOS device
Answers to both questions is no (for good reasons). Apple does not provide programmatic access to the device owner's Apple ID. And certainly not any info associated to the credit card. I would go with asking the user permission to get their location and explaining why your app needs it (in iOS 6 you can provide a context for why location is requested).
Get country name of iOS user without using location
There are web API services your app could contact to obtain the rough location of the ip address. It's not at all guaranteed to be the correct country of the device, as your user may be using an out-of-country VPN service, but it's probably the only thing you can do without asking the device for location.
Here's an example service. I know nothing about the quality of this service (I picked this one at random for this answer). There seem to be many out there, so do your homework as to which would be best for your users and yourself.
There's another approach you could take: simply ask the user, and have faith that most of them will answer honestly. If their answer doesn't make sense per the timezone of the device, maybe suspect their answer is false.
Related Topics
Why Can't I Call the Default Super.Init() on Uiviewcontroller in Swift
Uitableview Reloaddata Automatically Calls Resignfirstresponder
iOS Getting Location Updates When App Terminated Without Using Significantchange
Multiple Locations on Map (Using Mkmapitem and Clgeocoder)
Can't Cast Value of Type Uiviewcontroller to Patterndetailviewcontroller
Will iOS Region Monitoring Call Didenterregion on My App If My App Is in a Terminated State
Add a Watermark on Video After Merging Video and Audio Asset into One in Swift3 iOS
Async Request Does Not Enter Completion Block
Are Headphones Plugged In? iOS7
Hide Keyboard When Scroll Uitableview
Dismissing Uialertviews When Entering Background State
Atos and Dwarfdump Won't Symbolicate My Address
iOS Nsattributedstring on Uibutton
Duplicate Symbols for Architecture Arm64
Uniquely Identifying an iOS User
How to Get HTML Source from Url with Swift