Flutter get User's Country without Manual Permission - android

I have a Blog App where I write Bussiness Content for Global as well as some country-specific content. Like Special posts for India, Indonesia, etc.
That's why I need a way to find from which country the user is to provide specific content.
But, I don't want to use the GeoLocator Plugin because It will ask the
User for location. I only want the user's country, not the specific
location but the permission dialog didn't specify that which can make
the user anxious about why this app needs Location Permission. That's
why I want a way to get user's Country without an implicit permission
dialog asking for their location.
I have tried the following ways but none of them gave the exact country name.
import 'dart:io' show Platform;
String localeName = Platform.localeName;
// this returns language device is using which mostly comes us en_US so this is of no use
Locale myLocale = Localizations.localeOf(context);
// This returned the US as the country despite the App being opened and used in India.

Well, I finally found an easy and simple way to get users' country.
http://ip-api.com/json
This link returns the Users' location in a JSON format. So All one has to do now, is to make a Get request to the above link/API and decode the JSON to Map.
For that First Install HTTP Package:
Pubspec.yaml:
dependencies:
http:
Code:
import 'package:http/http.dart' as http;
import 'dart:convert';
Response data = await http.get('http://ip-api.com/json');
Map data = jsonDecode(data.body);
String country = data['country'];
EDIT:
As mentioned by good people,
Free Version Limit is 45 HTTP requests per minute from an IP address.

Locale gives you the language that the device user has set. It does not necessarily indicate their country of birth. It definitely does not tell you their country of residence. It may be that in many cases the person IS in the country of the language code but that is not guaranteed. The only way to know where they are is by location.
Update
Regarding your comment. If you mean where to get the country name based on the language code you get from the device locale, then have a look at this post. I haven't tried it myself but it seems promising. https://dev.to/thealphamerc/get-search-filter-countries-data-using-countryprovider-flutter-plugin-1epc

try this
import 'dart:io' show Platform;
String localeName = Platform.localeName
or try this
please use package devicelocale
I have tested with real device, it works fine
code snippet
String locale = await Devicelocale.currentLocale;

Related

In web-based apps, how do I call a user's IMEI / UDID to the end of my web-based app's URL?

I'm making a pair of website-based apps for both Android and iOS interfaces, and I'm struggling with a part of it. Perhaps you guys could help me out!
I'm using Android Studio and Xcode, and launching the website through WebKit and WK WebView respectively. It's super simple, just an app which calls a website into it directly. No external navigation, nothing but a full-page website. And this part is working great!
But I do have one problem! I don't want my users to get consistently logged out if they close the app, or after a few hours of not using it. I'd like it to stay logged in for them, or to automatically log-in when they use it.
The maker of the website has given me a way to do this through the URL.
Basically, my URL currently is set up like "https://URL.com/x/y/z" and it goes to the website, and that is great, but I need to set it up to be "https://url.com/x/y/z/[insert user's IMEI or UDID here]". That unique ID from their Android device will keep them logged in. I've tested it using my own device with my own IMEI and it works great, but obviously using one specific identifier for everyone will not work. I just need it to call the specific user's IMEI or UDID into the URL, to complete it.
How should I go about this?
I am assuming that you are talking about an Android app that visualizes a website in an activity. On Android, you can retrieve the device's IMEI OR MEID string by calling:
android.telephony.TelephonyManager.getDeviceId().
But be warned, this requires that you add a permission to your manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
You should inform your users why you are requesting this permission.
For iOS/Xcode, you can get the device UUID via UIDevice:
// Swift 4
let uuid = UIDevice.current.identifierForVendor!.uuidString
If you are targeting iOS 11 or newer, Apple introduced Device Check to get a device specific token. I personally haven't used it, but it sounds like it's use case is similar to what you're looking for.
Update:
From your comment, here is how'd you include it in the url string.
let urlString = "url.com/x/y/z/" + uuid

Unique Identifier for Mobile Devices using Cordova and AngularJS

please don't make my post like as duplicate why because am totally confused with those post when i google it.
but i need your valuable statements and real time experience on How to get UNIQUE IDENTIFIER for iOS,Android and Windows Mobiles
i have a scenario that when user login with UserName and Password i send details to server at same time i need to send Device UUID. By using device UUID and User Credentials am going to Restrict second user login when first user is already logged in(Active). but am confused with getting iOS Device UUID but wheen i seen in many post iOS is killing apps in App store when app is Accessing any UUID values.
please suggest me better way to complete mytask.
Link-1
Link-2
sorry for my bad english....!!!
You can use the Unique Device ID plugin.
Install it:
cordova plugin add cordova-plugin-uniquedeviceid
And use it:
window.plugins.uniqueDeviceID.get(function(uuid){
console.log("Unique ID": +uuid);
}, function(error){
console.error(error);
});
This will give you a unique ID per device that persists between installs.
Note that on Android 6 it requires telephony run-time permission to access SIM ID. Also you may need to fork it and update it to get the Windows Phone 8 code working on Windows 10 Mobile.
in the case of angular
you gotta install ngx-device-detector via npm then import it on your module and in the import section add DeviceDetectorModule.forRoot().then in the component you're gonna use import it and add private deviceService: DeviceDetectorService this in the constructor it will generate u a method and add it on the constructor this.epicFunction(), if it doesnt paste this
epicFunction() {
console.log('hello `Home` component');
this.deviceId = this.deviceService.getDeviceInfo();
const isMobile = this.deviceService.isMobile();
const isTablet = this.deviceService.isTablet();
const isDesktopDevice = this.deviceService.isDesktop();
console.log(this.deviceId);
console.log(isMobile); // returns if the device is a mobile device (android / iPhone / windows-phone etc)
console.log(isTablet); // returns if the device us a tablet (iPad etc)
console.log(isDesktopDevice); // returns if the app is running on a Desktop browser.
}
note: the "this.deviceId" is a variable i created in the start of the class
in the case of android just import the security and add the permission on the manifest file

How to find user's country

I am building a Cordova app for android platform.
I need to get the user's country.
I know that the geolocalisation gives the GPS coordinates.
Is there a way to have the country without using any external API? if not possible what is the best solution?
There's important difference from UX and legal perspective on what country do you need:
Country of current location: use geolocation API and any mapping solution. This is the only reliable way to determine country of location, since detection by IP or SIM card may only identify the country of telecom provider. Note, that country of the current location is not the country of citizenship and it does not allow to determine user's language or locale preferences.
Country as the context for localization: use ECMAScript Internationalization API[1] and cordova-globalization-plugin[2] as a fallback. Note, that these APIs do not provide location information at all: language and locale information are not the same as user's country (e.g. user may choose french locale and en_US language, while living in Monaco and being a citizen of Russia).
Country of citizenship. In some cases, it is a critical information for your service (e.g. when personal data of the user collected by the client-server system has to be physically processed in the country of citizenship as required by laws). The only way to determine it is to ask the user and require the honest answer from him by your Terms of Service. Note, that country of citizenship does not determine localization settings or user's current location.
[1] http://www.ecma-international.org/ecma-402/1.0/
[2] https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-globalization/
I can think of a few options. The list is provided below and it is ordered by least effort needed and on the other hand how accurate will the information be.
Use user's locale string provided by Globalization plugin, it may contain the country code next to language code such as "en-US" or "en-GB".
Usage
function successCallback(obj) {
var locale = obj.value;
}
navigator.globalization.getPreferredLanguage(successCallback, errorCallback);
Only for Android: Use plugin such as Device Information plugin which allows you to access the Android's Telephony Manager's information. From this information you are able to get the
Country ISO of your phone network provider
according to the plugin author. To use the plugin your code would look something like this
deviceInfo.get(function(result) {
console.log("result = " + result);
}, function() {
console.log("error");
});
where the result will contain your netCountry called field as part of the string returned.
Figure the GPS to country conversion by yourself based on the country borders as GPS. One possible map (dataset) is available [here](World Borders Dataset). I would though recommend just using some external API to give that information for you.
If your app is only for phone or device with sim, you can use sim info plugin, https://github.com/dtmtec/cordova-plugin-carrier.
A simple solution is create a custom plugin and use Google geocoder, this code return location from latitude and longitude... This isn't for cordova, you need make some changes
try {
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0)
String cityname = addresses.get(0).getLocality();
String country = addresses.get(0).getCountryName();
} catch (IOException e) {
e.printStackTrace();
}

Verify user during signup by sending and receiving sms through Twilio

I am trying to verify user by sending sms through twilio (Android Application).
Here is the Detailed summary-
(As in Snapchat)
During signup i want to validate user by sending dynamic run-time code to user mobile.
and after sending i have the verification field.
so what should i do ?
Twilio employee here.
This is a really common use case for Twilio and plenty of apps do the "phone number verification" thing you're looking for.
Let me boil down the steps on how to do this in human form, as this is much easier to explain than writing a bunch of code:
A user will type their phone number into a field to be verified.
When the user has typed in their number, you can compute a unique code (4 - 6 digits is all you need) however you like, and then use our REST API to send the number they entered the code.
At this point, you should save the unique code so you can reference it later.
Prompt the user to enter the code into a field within your app.
Compare the entered code to unique number you stored them and viola!
If the code is the same: you know that they own the phone number that you sent the message to. A very similar process is described in this 2-factor authentication how-to.
I hope that makes sense.
If you have any questions, please ask.
Disclaimer: I'm the maintainer of Django-phone-verify
While phait's answer is apt. People had asked in comments of the relevant apps with which they could accomplish user verification. Most of this is from my previous answer at https://stackoverflow.com/a/57461296/3535547 I'm just pasting an updated answer in this thread so that it is easier for users to find it.
What you're looking to accomplish is very easy with django-phone-verify app. It comes with Twilio and Nexmo already integrated and few endpoints which you can extend as per your use case.
This package aims at verifying if a phone number requested by a particular client belongs to them. It also takes care of ensuring that the same device provides the verification of passcode which initially requested a passcode to be sent, saving you a few hours of work.
This package also doesn't mess up with your current user model at all. You're free to use this package exactly for one thing: verifying phone numbers. Whether you do it for users, companies etc. depends on your use-case.
It follows Unix philosophy of Do one thing; do it well
Installation
pip install django-phone-verify
Configuration
Add app to INSTALLED_APPS:
# In settings.py:
INSTALLED_APPS = [
...
'phone_verify',
]
Add settings in your settings.py file:
# Settings for phone_verify
PHONE_VERIFICATION = {
'BACKEND': 'phone_verify.backends.twilio.TwilioBackend',
'TWILIO_SANDBOX_TOKEN':'123456',
'OPTIONS': {
'SID': 'fake',
'SECRET': 'fake',
'FROM': '+14755292729'
},
'TOKEN_LENGTH': 6,
'MESSAGE': 'Welcome to {app}! Please use security code {otp} to proceed.',
'APP_NAME': 'Phone Verify',
'OTP_EXPIRATION_TIME': 3600 # In seconds only
}
Migrate the database:
python manage.py migrate
You get two endpoints (Check API docs), one for registration of phone number and other to verify the passcode. You may override verify endpoint to also create a user as described in the usage docs: https://github.com/CuriousLearner/django-phone-verify/blob/master/docs/usage.rst

how to get international dialing code based on SIM provider?

i am developing the application in which user mobile no and international country code should be set automatically on the basis of SIM card provider.
i.e if i install this application in first page country code and my phone no should be filled automatically.i want this result as my output.
i have done this using static code but i want to do dynamically.if user uses this application then country code and mobile no should be selected automatically.
i have searched on net but there is not a exact answer of this question.
I have seen the application in which they are doing this thing so how they are doing?
Thanks
There is also one library which will helpful to you for validate user entered Phone number
as
http://code.google.com/p/libphonenumber/
Here is the code snippet that will help you in getting the phone number:
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = tMgr.getLine1Number();
Don't forget to include READ_PHONE_STATE permission in manifest file.

Categories

Resources