问题描述
我希望单击一个TextView中的电话号码来拨打该电话号码:
我面临的问题是,单击链接时,消息应用程序正在打开。我在iOS 12中尝试了以下代码,并且按预期方式工作,但是当我在iOS 13和14上尝试时,消息应用程序打开,而不是使用“电话拨号器应用程序”拨打电话。
暂定1:
textView.Editable = false;
textView.DataDetectorTypes = UIDataDetectorType.PhoneNumber;
暂定2:
var phoneNumberLink = new Dictionary<string,string>(){ { "(855) 757-7328","tel:8557577328" } }
textView.Editable = false;
textView.SetAttributedTextForLinks("Please call us at (855) 757-7328",phoneNumberLink);
解决方法
如上所述https://forums.xamarin.com/discussion/33798/open-phone-dialer-from-app
打开拨号程序的正确方法是
telprompt://0123456789
确保没有空格。
,实际上,telpromt://0123456789
也不起作用。我必须拦截与URL的交互,推荐的ShouldInteractWithUrl
无法正常工作(ShouldInteractWithUrl
未被触发或调用),所以我不得不改用AllowUrlInteraction
。代码看起来像这样:
var description = new KiteEmbeddedLinkTextView()
{
Editable = false,DataDetectorTypes = UIDataDetectorType.PhoneNumber,Text = message.MessageText
};
description.AllowUrlInteraction += AllowUrlInteraction;
private bool AllowUrlInteraction(UITextView textView,NSUrl url,NSRange characterRange,UITextItemInteraction interaction)
{
UIApplication.SharedApplication.OpenUrl(url);
return false;
}
由于某种原因,除了这个选项之外,以前的选项都不适合我。
请参阅: https://forums.xamarin.com/discussion/60345/uitextview-and-clickable-phonenumbers How to intercept click on link in UITextView?