如何在xamarin ios应用程序中打开邮件应用程序窗口?

时间:2017-12-23 07:41:49

标签: xamarin xamarin.ios

我想通过使用带有mailto,主题和邮件正文的开放URL来打开电子邮件 你可以看到下面的代码

string email = "me@example.com";
            string subject = "MySubject";
            string body = "Myboday";

            var idn = new System.Globalization.IdnMapping();
            var dotnetURI = new System.Uri("mailto:" + email + "?subject:" + subject + "&body:" + body);
            Console.WriteLine(dotnetURI.ToString());
            NSUrl nsURL = new NSUrl(dotnetURI.Scheme, idn.GetAscii(dotnetURI.DnsSafeHost), dotnetURI.PathAndQuery);
            UIApplication.SharedApplication.OpenUrl(nsURL);

但我收到有关绝对路径的错误

你可以帮我吗? 提前谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用NSString.CreateStringByAddingPercentEscapes方法转义字符串:

iOS示例:

string email = "me@example.com";
string subject = "Hello StackOverflow";
string body = "Happy Holdays";
using (var encoded = new NSString($"mailto:{email}?subject={subject}&body={body}").CreateStringByAddingPercentEscapes(NSStringEncoding.UTF8))
using (var url = NSUrl.FromString(encoded))
{
    UIApplication.SharedApplication.OpenUrl(url);
}

macOS示例:

NSWorkspace.SharedWorkspace.OpenUrl(url);

注意:相同的代码用UIApplication.SharedApplication替换NSWorkspace.SharedWorkspace