gpt4 book ai didi

xamarin - Unicode URL =无法初始化 'Foundation.NSUrl' : the native 'initWithString:' 类型的实例

转载 作者:行者123 更新时间:2023-12-01 09:19:12 24 4
gpt4 key购买 nike

我试图在UIWebView中打开一个包含Unicode字符的URL:

var jrURL = new NSURL("http://δπθ.gr");

结果是:
Could not initialize an instance of the type 'Foundation.NSUrl': 
the native 'initWithString:' method returned nil.
It is possible to ignore this condition by setting
MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure to false.

https://Google.com这样的人效果很好

最佳答案

IDN域名,例如:

  • http://δπθ.gr

  • 为了与 NSUrl一起使用,必须进行转换(基于punycode):
  • http://xn--pxaix.gr

  • IDN: https://en.wikipedia.org/wiki/Internationalized_domain_name

    Punycode: https://en.wikipedia.org/wiki/Punycode

    您可以使用 System.Globalization.IdnMapping将您的域名转换为 Punycode
    此命名空间/类在 Xamarin.iOSXamarin.Android中可用。
    var idn = new System.Globalization.IdnMapping ();
    var dotnetURI = new System.Uri ("http://δπθ.gr);
    Console.WriteLine (dotnetURI.ToString());
    NSUrl nsURL = new NSUrl (dotnetURI.Scheme, idn.GetAscii (dotnetURI.DnsSafeHost), dotnetURI.PathAndQuery);
    Console.WriteLine (nsURL.ToString());
    WebView.LoadRequest(new NSUrlRequest(nsURL));

    MSDN IdnMapping.GetAscii: https://msdn.microsoft.com/en-us/library/system.globalization.idnmapping.getascii(v=vs.110).aspx

    注意:用于处理punycode转换的 System.Uri.IdnHost是.Net 4.6的一部分,目前在Xamarin的 System命名空间中不可用

    引用: https://msdn.microsoft.com/en-us/library/system.uri.idnhost(v=vs.110).aspx

    关于xamarin - Unicode URL =无法初始化 'Foundation.NSUrl' : the native 'initWithString:' 类型的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36685160/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com