簡體   English   中英

Unicode URL =無法初始化“Foundation.NSUrl”類型的實例:本機“initWithString:”

[英]Unicode URL = Could not initialize an instance of the type 'Foundation.NSUrl': the native 'initWithString:'

我想在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域名如:

必須轉換(基於punycode)才能與NSUrl一起使用:

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.GetAsciihttpsIdnMapping.GetAscii = IdnMapping.GetAscii

注意:處理punycode轉換的System.Uri.IdnHost是.Net 4.6的一部分,目前在Xamarin的System名稱空間中不可用

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

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

簡單! ( - :

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM