如何以xamarin形式获取电话号码? [英] How to get phone number in xamarin forms?

查看:288
本文介绍了如何以xamarin形式获取电话号码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究xamarin.forms.我需要获取电话号码,如果设备是双SIM卡,那么我需要获取两个号码.我该如何实现?引用我来到这里后,有很多与此相关的问题.

I am working on xamarin.forms. I need to get the phone number, if the device is dual sim then I need to get both the numbers. How do I achieve this? There so many questions related to this after referring all I came here.

我在shard项目中创建了一个界面,并在android项目中实现了该界面. android.permission.READ_PHONE_NUMBERS

I created one interface in shard project and implemented the interface in an android project. android.permission.READ_PHONE_NUMBERS

在共享项目中创建的界面

created interface in shared project

public interface IDeviceInfo
{
 string GetMyPhoneNumber();
}

在android项目中实现了界面

Implemented the interface in android project

public string GetMyPhoneNumber()
{
   try
   {
      TelephonyManager mgr =     
  Android.App.Application.Context.GetSystemService(Context.TelephonyService) as TelephonyManager;
    return mgr.Line1Number;
   }
  catch (Exception ex)
  {
  throw;
  }
}

在Button之一上,单击共享项目

On one of Button click on shared project

 private void GetPhone_Clicked(object sender, EventArgs e)
 {  
    try
    {

        var data = DependencyService.Get<IDeviceInfo>().GetMyPhoneNumber();
    }
    catch (Exception ex)
    {
          throw;
    }
}

当我使用android 5时,它返回空字符串,如果我使用android 6+,则给出权限错误,例如Java.Lang.SecurityException:getLine1NumberForDisplay:用户10286或当前进程都没有android.permission.READ_PHONE_STATE,android .permission.READ_SMS或android.permission.READ_PHONE_NUMBERS

When I am using android 5 it is returning the empty string and if I am using android 6+ it is giving permission error like Java.Lang.SecurityException: getLine1NumberForDisplay: Neither user 10286 nor current process hasandroid.permission.READ_PHONE_STATE, android.permission.READ_SMS, or android.permission.READ_PHONE_NUMBERS

如何在所有Android版本中解决此问题?

How do resolve this in all android version?

推荐答案

对于iOS,这是不可能的,即使您以某种方式使用CoreTelephony来获取它,或者其他原因,您的应用程序也将由于以下原因而被拒绝用于苹果商店部署Dylan在此处中指出的问题:

For iOS, this is not possible and even if you somehow get it using CoreTelephony or something else your application would get rejected for apple store deployment with the below issue as stated by Dylan here:

出于安全原因,iPhone OS将应用程序(包括其首选项和数据)限制在文件系统中的唯一位置.该限制是被称为应用程序的沙箱"的安全性功能的一部分.沙箱是一组细粒度的控件,它们限制了应用程序对文件,首选项,网络资源,硬件等的访问."

"For security reasons, iPhone OS restricts an application (including its preferences and data) to a unique location in the file system. This restriction is part of the security feature known as the application's "sandbox." The sandbox is a set of fine-grained controls limiting an application's access to files, preferences, network resources, hardware, and so on."

该设备的电话号码在您应用程序的容器中不可用.您将需要修改您的应用程序以使其只能在目录容器中读取,然后将二进制文件重新提交给iTunes Connect,以便您的应用程序可以重新考虑进入App Store.

The device's phone number is not available within your application's container. You will need to revise your application to read only within your directory container and resubmit your binary to iTunes Connect in order for your application to be reconsidered for the App Store.

现在对于Android,您可以使用TelephonyManager方法来获取电话号码,但据我所知,这不是一种可靠的方法,因此它无法从sim详细信息或其他信息中获取电话号码它会从您在手机启动时输入的设备信息中获取信息,并在此处

Now for Android, you could use the TelephonyManager approach to get the phone number, but in my knowledge, it is not a reliable approach to do so it does not get the phone number from the sim details or something it picks it up from the device information that you enter at the phone's startup an explanation for it is present here

此外,这里有一条重要的评论似乎与相关

Also, there is an important comment here that seems to be relevant

实际上,并不是那么完美.上次尝试此方法时,它报告了我的手机原来拥有的电话号码,然后才将我的旧手机号码移植到该电话号码上.可能仍会这样做,因为设置"应用程序仍会显示该无效数字.此外,有报告说某些SIM卡导致此方法返回null.话虽这么说,我不知道有一个更好的答案.

Actually, not so perfect. Last time I tried this method, it reported the phone number that my phone originally had, before my old mobile number was ported over to it. It probably still does, as the Settings app still shows that defunct number. Also, there are reports that some SIMs cause this method to return null. That being said, I'm not aware of a better answer.

即使您对此感到满意,也只能使用该电话号码获得一个电话号码,而且如果您的用户未在手机的设置中配置,那么该电话号码在大多数情况下将为空.

Even if this somehow is fine with you, you can only get one phone number using this and this phone number would be null most of the times if your user is not configured in the mobile's settings.

解决这两个问题的方法是,根据您的方便创建类似弹出窗口或页面的屏幕,要求用户自己输入电话号码(如果必须的话)一起创建模式页面

The way in which both these issues could get solved is, creating a screen something like a pop-up or a page according to your convenience that asks the user himself to enter the phone number if mandatory make a modal page all together

祝你好运

在查询的情况下还原

这篇关于如何以xamarin形式获取电话号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆