2017-06-01 94 views
0

我想通過我的xamarin iOS項目上的http傳遞憑據,但它不工作。Xamarin iOS:通過http傳遞憑證

public partial class WebView : UIViewController 
{ 

     protected override void ViewDidLoad() 
     { 
     webView.LoadRequest(new NSUrlRequest(new NSUrl("https:example.com"))); 
     webView.AllowsBackForwardNavigationGestures = true; 
     webView.NavigationDelegate = new WebViewDelegate(this); 
     } 
} 

public class WebViewDelegate : WKNavigationDelegate, INSUrlConnectionDataDelegate 
    { 

     public override void DidReceiveAuthenticationChallenge(WKWebView webView, NSUrlAuthenticationChallenge challenge, Action<NSUrlSessionAuthChallengeDisposition, NSUrlCredential> completionHandler) 
     { 
      //base.DidReceiveAuthenticationChallenge(webView, challenge, completionHandler); 
      completionHandler(NSUrlSessionAuthChallengeDisposition.PerformDefaultHandling, new NSUrlCredential("username", "password", NSUrlCredentialPersistence.ForSession)); 
      Console.WriteLine("We are authenticated"); 
      return; 
     } 
} 

任何人都可以建議如何解決這個問題?

更新:

DidReceiveAuthenticationChallenge被調用,並正在以不斷循環調用。不但得不到預期的頁面,我得到一個空白頁面,它只是說,「驗證」

+0

請界定 「不工作」。它會崩潰嗎?它是否給出錯誤?它是否加載任何東西? DidReceiveAuthenticationChallenge是否被調用? – Jason

+0

更新了我的問題 – TheDeveloper

回答

1

從蘋果的doc上PerformDefaultHandling

使用默認處理的挑戰,因爲雖然此委託方法不實現。提供的憑證參數被忽略。

嘗試UseCredential代替:

var crendential = new NSUrlCredential("user", "pass", NSUrlCredentialPersistence.ForSession); 

completionHandler(NSUrlSessionAuthChallengeDisposition.UseCredential, crendential); 
+0

'DidReceiveAuthenticationChallenge'正在循環中被無限次地調用,現在web視圖只是空白的白色屏幕。 – TheDeveloper

+0

沒關係。有效。 – TheDeveloper