NSUrlConnectionDelegate永远不会被单调调用?

时间:2011-04-07 10:41:58

标签: xamarin.ios

我最近使用NSUrlConnection从url下载图片异步,以便不阻止ui线程。 所以我写了下面的代码:

 NSMutableUrlRequest req=new NSMutableUrlRequest(new NSUrl(value),NSUrlRequestCachePolicy.ReloadRevalidatingCacheData,20);
req["User-Agent"]=_UserAgent;
USUrlConnection.FromRequest(req, new EscozUrlDelegate("picture",(result)=>
                           {
                                   Console.WriteLine(result);
                                   .....
                               });

但我不得不说代理中的代码永远不会被调用。实际上,没有调用EscozUrlDelegate内部的覆盖方法。在NSUrlConnection.FromRequest

之后没有任何事情发生

以下是EscozUrlDelegate的代码:

public class EscozUrlDelegate : NSUrlConnectionDelegate {
    Action<string> callback;
    Action _failure;
    NSMutableData data;
    string _name;

    public EscozUrlDelegate(string name, Action<string> success) {
        _name = name;
        callback = success;
        data = new NSMutableData();
    }

    public EscozUrlDelegate(string name, Action<string> success, Action failure) {
        _name = name;
        callback = success;
        _failure = failure;
        data = new NSMutableData();
    }

    public override void ReceivedData (NSUrlConnection connection, NSData d)
    {
        data.AppendData(d);
    }

    public override bool CanAuthenticateAgainstProtectionSpace (NSUrlConnection connection, NSUrlProtectionSpace protectionSpace)
    {
        return true;
    }

    bool showError = true;

    public override void ReceivedAuthenticationChallenge (NSUrlConnection connection, NSUrlAuthenticationChallenge challenge)
    {
        if (challenge.PreviousFailureCount>0){
            showError = false;
            challenge.Sender.CancelAuthenticationChallenge(challenge);
            Application.AuthenticationFailure();
            return;
        }

        if (challenge.ProtectionSpace.AuthenticationMethod=="NSURLAuthenticationMethodServerTrust")
            challenge.Sender.UseCredentials(NSUrlCredential.FromTrust(challenge.ProtectionSpace.ServerTrust), challenge);

        if (challenge.ProtectionSpace.AuthenticationMethod=="NSURLAuthenticationMethodDefault" &&
                Application.Account!=null && Application.Account.Login!=null && Application.Account.Password!=null) {
            challenge.Sender.UseCredentials(NSUrlCredential.FromUserPasswordPersistance(
                      Application.Account.Login, Application.Account.Password, NSUrlCredentialPersistence.None), challenge);

        }
    }

    public override void FailedWithError (NSUrlConnection connection, NSError error)
    {
        UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
        if (showError)
            Application.ShowNetworkError(error.LocalizedDescription);

        if (_failure!=null)
            _failure();
    }

    public override void FinishedLoading (NSUrlConnection connection)
    {
        EscozUrlConnection.ConnectionEnded(_name);
        callback(data.ToString());
    }
}

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:2)

要使用委托,您需要像这样初始化连接:

NSMutableUrlRequest request = new NSMutableUrlRequest(new NSUrl(value),NSUrlRequestCachePolicy.ReloadRevalidatingCacheData,20);
req["User-Agent"]=_UserAgent;
var mydelegate = new EscozUrlDelegate("picture",(result)=>
                           { Console.WriteLine(result); });

new NSUrlConnection(request, mydelegate, true);

如果它不起作用,请告诉我。

答案 1 :(得分:0)

我有同样的问题,当我移动NSUrlConnection.FromRequest(request, this)时 进入主线程然后相应的ReceivedData回调立即开始工作。

我的代码是NSUrlConnectionDataDelegate实现的一部分。