Is it possible to develop a hybrid mobile app using Phonegap / Cordova and access the device phone number for both Android and iOS?
iOS:
You can retrieve the phone number using the CoreTelephony framework, you will need to add the following Entitlement: com.apple.coretelephony.Identity.get.
However apple might reject your app once you upload it to the AppStore.
But if you plan to distribute using the "Enterprise Distribution" plan, you should have no problem at all, see answers by Igor Fedorchuk and Dylan here, and another elaborated answer here.
Android:
Yes, you could use this plugin, or write a cordova plugin that bridges the following code:
TelephonyManager tm = (TelephonyManager)appContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tm.getLine1Number();
The required permission is android.permission.READ_PHONE_STATE"
Android:I know i am late. After Searching a lot i found this plugin which works for me perfectly...
add these plugin in your project
Put these code in your index.js here
$(document).ready(function () {
document.addEventListener("deviceready", onDeviceReady, false);
});
function onDeviceReady() {
window.plugins.phonenumber.get(success, failed);
document.addEventListener("backbutton", onBackKeyDown, false);
}
function success(phonenumber) {
console.log("My number is " + phonenumber);
}
function failed(phonenumber) {
console.log("Error " + phonenumber);
}
If your phone do not allow to show phonenumber then it will go to failed block.
But from That : Some vendors don't publish the phone number to the SIM card.
You can check it in Settings-> About Phone-> Status-> SIM Status.
Related
I need to get a unique number for every device for my cordova app (iOS/Android) The ID must be the same when user closes the app or install a new version
I cannot used the UUID from the "cordova device plugin" because the UUID changes when the user upgrade to a new version so it wouldn't work for me.
I have tried generating a unique number and storing it on the phone. However, I run into an issue where the information is not persisted.
This happens when the user install the app and then remove the app from the background. Otherwise , the data is saved
any ideas?
You can use the serial number of the phone as a unique identifier
One possibility is to use cordova-plugin-cloud-settings which allows you to persist user settings in cloud storage across devices and installs.
Install it:
cordova plugin add cordova-plugin-cloud-settings
Use it to store a GUID:
var settings = {
GUID: 12345678
}
cordova.plugin.cloudsettings.save(settings);
The nice thing about this solution is it doesn't need any special user permissions.
If you really want a unique device ID (vs unique user ID) then you could use the UniqueDeviceID plugin.
However, note that on Android this uses the SIM ID to obtain an identifier so requires READ_PHONE_STATE permission which displays a user permissions prompt "Allow to make and manage phone calls", which is not great for the trust of your users.
Use the MAC address:
cordova plugin add cordova-plugin-ip-mac-address
var params = {};
addressimpl.request("getMACAddress", JSON.stringify(params), function(message) {
alert("mac address "+message);
}, function() {
alert("failed on get mac address");
});
From here
I am building a mobile application and want to access the IMEI number.
I have gone through the following plugins.
https://github.com/vliesaputra/DeviceInformationPlugin
https://github.com/hygieiasoft/cordova-plugin-uid
https://github.com/aquto/cordova-plugin-imei
And all of them are giving me 404 error.
Finally I got one plugin that I could install
https://www.npmjs.com/package/imeiplugin
and below is the code I wrote to access the IMEI
The below code is written inside my home constructor.
platform.ready().then(() => {
window["plugins"].imeiplugin.getImei(function (imei) {
console.log(imei);
});
});
But the control never goes to the console.log();
Am I doing anything wrong here??
Or if anyone has integrated the IMEI number with some other method please let me know.
Or as per suggested in the below link do I have to build my own custom plugin??
How to get IMEI number in PhoneGap?
I am stuck here and I am looking for an efficient way any help is appreciated.
Thanks in advance
Actually this plugin works:
https://github.com/hygieiasoft/cordova-plugin-uid
(Using: cordova plugin add org.hygieiasoft.cordova.uid returns 404)
So try this instead:
cordova plugin add https://github.com/hygieiasoft/cordova-plugin-uid
platform.ready().then(() => {
alert(cordova.plugins.uid.IMEI);
});
I'm trying to add the SMS cordova plugin to my ionic application but I'm failing miserably.
I've no idea how to inject the plugin or use it the documentation seems to be more phonegapp orientated.
I've got the following:
Installed
cordova plugin add
https://github.com/cordova-sms/cordova-sms-plugin.git
My view
<button ng-click="send()">Test</button>
My Controller
angular.module('starter.controllers', [])
.controller('DashCtrl', function($scope, $http, $state, $cordovaSms) {
$scope.send = function(){
$cordovaSms
.send('phonenumber', 'SMS content')
.then(function() {
// Success! SMS was sent
}, function(error) {
// An error occurred
});
};
})
I get the following error:
ionic.bundle.js:26794 Error: [$injector:unpr] Unknown provider:
$cordovaSmsProvider <- $cordovaSms <- DashCtrl
I think it's a casing issue. Are you using ionic-native yet? If not, start there.
bower install ionic-native --save
bower uninstall ngCordova --save
Then replace ngCordova with ionic.native in your module dependencies.
Here is working code from one of my projects...
.controller('RosterCtrl', function ($cordovaSMS, ...
// snip ....
$cordovaSMS.send(phone, message)
.then(function () {
$log.log('Message sent.');
});
Google changed the policy regarding to SMS access, so the direct reading of incoming SMS is no longer allowed and the associated permissions will be removed (SMS_READ).
Now, it is necessary to use the Android SMS Retriever API (Android SMS Retriever API) and your SMS message needs to comply a specific format in order to be intercepted by your app.
In Cordova use this plugin to easily read incoming SMS:
cordova plugin add cordova-plugin-android-sms-retriever
Github: https://github.com/diegosiao/cordova-plugin-android-sms-retriever
On IONIC 2 , the cordova-sms-plugin its the solution choose for the native SMS plugin. So far so good, except the case you want to send messages to multiple recipients.
At this moment, cordova-sms-plugin does not send to multiple recipients, even if we use string array. It will successfully send to first recipient, but not to the rest of array.
So after I searched a while I got another cordova plugin, which sends messages to all numbers in array. The only problem is, if among those numbers are invalid or incomplete phone numbers, it simply ignores them and pass to the next number to send.
Would be nice to have a callback or at least a trace, when certain numbers are invalid or incomplete, but at least it sends to multimple numbers.
Cheers
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
I want to create an phonegap app using this plugin. I have done this steps to create and add plugin :
1:I am creatin this app using phonegap cli :-
phonegap create GetPhone --id "com.phone" --name "GetPhoneApp"
2: Going into the project and buid
cd GetPhone
phonegap local build android
3:Installing plugin
phonegap local plugin add https://github.com/macdonst/TelephoneNumberPlugin
4:moving telephonenumber.js to www folder
5: adding
<script type="text/javascript" charset="utf-8" src="telephonenumber.js"></script>
into index.html then added below ondeviceready :-
var telephoneNumber = cordova.require("cordova/plugin/telephonenumber");
telephoneNumber.get(function(result) {
alert("result = " + result);
}, function(error) {
alert("error = " + error.code);
});
6: Added
<plugin name="TelephoneNumber" value="com.simonmacdonald.cordova.plugins.TelephoneNumber"/>
into config.xml in www directory.
7: Build
phonegap local build android
8: Uploaded into build.phonegap.com and created .apk file.
Then when i am running this app on android device it does not alert anything. Is there any wrong step i have taken. I dont know much more about phone gap. After googling i have created this. I have seen this tutorial also to add this plugin. When i am build it locally it does not show any error. But it is also not running.
Please help me find the error.
First of all I would suggest you get acquainted with standard tools, especially adb. These will allow you to provide more information, find out what really is going on.
Skimming through the guide is also helpful, so you can be sure you know what you're doing and why you're doing it. Look at step-by-step debugging of Android code as well.
I would also encourage you to build a Cordova application from sources without using the fancy tools to get the hang of what goes where and how all the components come together. Your source tree is confusing and you seem to have used a mixture of Cordova 3.1 and Cordova 2.9, and even Phonegap? You only need to include cordova.js and your script (js/script.js in this case), nothing else at the moment.
Having said that, on to the major issue.
Your call to get returns an error. Why? Take a quick look at the source code of the plugin:
https://github.com/macdonst/TelephoneNumberPlugin/blob/master/src/com/simonmacdonald/cordova/plugins/TelephoneNumber.java
The plugin retrieves the TelephonyManager and tries to call the getLine1Number. And as you can see result != null is false which drops through to returning a PluginResult.Status.ERROR, which is why you get your error alert box.
This is the core issue at hand. TelephonyManager returning a null for your request. Why? After searching around you might stumble on:
Retrieve phone Number in android
TelephonyManager.getLine1Number() failing?
Reading device phone number throws NULLPointerException
So, navigate over to Settings > About Phone > Status on your device and look at the "My phone number" field. Is it "Unknown"? Tough luck then. My SIM doesn't store the number, so I got the same results as you did.
Another thing to note, is that at least on Android 4.4, the method returns an empty string "" instead of a null, so when testing on my Android 2.3.7 device I got a null and thus got the error branch. But on my Android 4.4.2 device I got a "" and got the success branch, but the number was empty, obviously.
You can set the number on the emulator by using this pretty advanced guide if you'd like to try. So what can you do? Probably nothing by using the API, not even the Android system itself can get your number if it's not stored on the SIM card. Asking the user or sending an SMS to discover the number via a web service may be an option as well.
Step number 5 above will not work unless the code is called after the 'deviceready' event is triggered.
you will need some sort of call to know when the document is loaded, you can do either:
<body onload="onLoad()">
<script>
function onLoad() {
document.addEventListener('deviceready', deviceReady, false);
}
</script>
OR (w/ jquery):
<script>
$(document).ready(function () {
document.addEventListener('deviceready', deviceReady, false);
}
</script>
and then create this function:
function deviceReady() {
var telephoneNumber = cordova.require("cordova/plugin/telephonenumber");
telephoneNumber.get(function(result) {
alert("result = " + result);
}, function(error) {
alert("error = " + error.code);
});
}
This will ensure that your document has loaded and that cordova is ready to call to.