开发者

NSUrlConnectionDelegate never get called in monotouch?

I recently use NSUrlConnection to download a picture asynchron from an url, in order not to block the ui thread. So I wrote the following code:

 NSMutableUrlRequest req=new NSMutableUrlRequest(new NS开发者_JS百科Url(value),NSUrlRequestCachePolicy.ReloadRevalidatingCacheData,20);
req["User-Agent"]=_UserAgent;
USUrlConnection.FromRequest(req, new EscozUrlDelegate("picture",(result)=>
                           {
                                   Console.WriteLine(result);
                                   .....
                               });

but I have to say that the code inside the delegate is never called. actually, none of the overriden method of inside EscozUrlDelegate got called.nothing happens after NSUrlConnection.FromRequest

following is the code of 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());
    }
}

Please help me with this problem.


To use the delegate, you need to initialize the connection like this:

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);

Let me know if it doesn't work.


I had the same issue, and when I moved my NSUrlConnection.FromRequest(request, this) into the main thread then the corresponding ReceivedData callbacks started working right away.

My code is part of a NSUrlConnectionDataDelegate implementation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜