You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin Forms项目中获取Android、iOS、Windows Phone设备手机号方法咨询

获取设备手机号的Xamarin.Forms跨平台实现指南

Hey there! Retrieving a user's phone number from their device in Xamarin.Forms requires platform-specific code since each OS has different privacy rules and APIs. Let's walk through each platform step by step, plus share some helpful NuGet packages to streamline the process.

一、Android平台实现

Android allows access to the phone number, but there are strict permissions and version-based restrictions:

1. 配置权限

First, add the required permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Note: For Android 10 (API 29+) and above, Google restricts direct access to the phone number. Even with the permission, you might only get null or a placeholder value unless your app is a system app or has carrier privileges.

2. 依赖服务实现

Create a shared interface in your Xamarin.Forms project:

public interface IPhoneNumberService
{
    string GetPhoneNumber();
}

Then implement this interface in your Android project:

[assembly: Dependency(typeof(PhoneNumberServiceAndroid))]
namespace YourApp.Droid
{
    public class PhoneNumberServiceAndroid : IPhoneNumberService
    {
        public string GetPhoneNumber()
        {
            // Request permission first (use Plugin.Permissions for easier handling)
            var status = CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Phone).Result;
            if (status != PermissionStatus.Granted)
            {
                var results = CrossPermissions.Current.RequestPermissionsAsync(Permission.Phone).Result;
                status = results[Permission.Phone];
            }

            if (status == PermissionStatus.Granted)
            {
                TelephonyManager telephonyManager = (TelephonyManager)Android.App.Application.Context.GetSystemService(Context.TelephonyService);
                return telephonyManager.Line1Number; // May return null on Android 10+
            }
            return null;
        }
    }
}

二、iOS平台实现

Apple enforces strict privacy rules, so there is no public API to directly retrieve the user's phone number from the device. The only reliable ways are:

  • Ask the user to manually enter their phone number and verify it (via SMS, email, etc.)
  • Use a third-party authentication service that handles phone number verification without requiring direct access to the device's number.

If you want to get carrier-related info (which doesn't include the phone number), you can use CoreTelephony:

using CoreTelephony;

public class PhoneNumberServiceiOS : IPhoneNumberService
{
    public string GetPhoneNumber()
    {
        // iOS doesn't allow direct access, return null or prompt user
        var networkInfo = new CTTelephonyNetworkInfo();
        var carrier = networkInfo.SubscriberCellularProvider;
        // You can get carrier name, country code, etc., but NOT the phone number
        return null;
    }
}

三、Windows Phone (UWP)平台实现

For UWP (Windows Phone 10+), you can use the PhoneLineManager API, but it requires specific capabilities:

1. 配置功能

Add the phoneCallHistory capability to your Package.appxmanifest:

<Capabilities>
    <DeviceCapability Name="phoneCallHistory" />
</Capabilities>

2. 依赖服务实现

Implement the shared interface in your UWP project:

[assembly: Dependency(typeof(PhoneNumberServiceUWP))]
namespace YourApp.UWP
{
    public class PhoneNumberServiceUWP : IPhoneNumberService
    {
        public async Task<string> GetPhoneNumberAsync()
        {
            var phoneLines = await PhoneLineManager.RequestStoreAsync(PhoneLineStoreAccessType.AllLinesReadOnly);
            var allLines = await phoneLines.GetPhoneLinesAsync();
            
            foreach (var line in allLines)
            {
                if (line.IsMobile && line.CanDial)
                {
                    return line.Number;
                }
            }
            return null;
        }
    }
}

Note: The user will be prompted to grant access to their phone lines.

推荐的NuGet包

  • Plugin.Permissions: Simplifies permission requests across all platforms (critical for Android and UWP). Install it via:
    Install-Package Plugin.Permissions
    
  • Xamarin.Essentials: While it doesn't directly retrieve phone numbers, it provides cross-platform utilities for permissions, device info, and more.
  • Firebase.Auth: If you need phone number verification (instead of just retrieving it), this package handles SMS-based verification seamlessly across platforms.

重要注意事项

  • Privacy is key: Always inform users why you need their phone number and only request it when necessary.
  • Platform restrictions: Android 10+ and iOS have strict limitations, so don't rely solely on device API retrieval—have a fallback (like manual entry) ready.

内容的提问来源于stack exchange,提问作者Dave Kapildev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:14:18