[英]How to resolve 'could not initialize an instance of the type uikit.uiimage : The native initwith contentsofFile' errors in icons tabbed page
[英]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.
IDN域名如:
必須轉換(基於punycode)才能與NSUrl
一起使用:
IDN: https : //en.wikipedia.org/wiki/Internationalized_domain_name
Punycode: https : //en.wikipedia.org/wiki/Punycode
您可以使用System.Globalization.IdnMapping
將您的域名轉換為Punycode
,此命名空間/類可在Xamarin.iOS
和Xamarin.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 : IdnMapping.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.