rahulg
rahulg

Reputation: 2249

Why aren't required NSURLConnection delegate methods declared in the NSURLConnectionDelegate protocol

I was going through the NSURLConnectionDelegate class reference on the apple.developer.com site. There I realized that a few functions like connectionDidFinishLoading were not present in the IOS class but were there in MAC library.

Then again when I try to use those functions in XCODE for an IOS app, the auto-complete won't complete it but the functions do work as expected.

Can any one please explain how these work and will this cause any issues when deploying the app. Thanks!!

Upvotes: 4

Views: 831

Answers (2)

Tobias Hieta
Tobias Hieta

Reputation: 189

Remember that if you find inconsistency in the APIs you need to file bug report with RADAR as the friendly guys at Black Pixel explains it: RADAR or GTFO

Upvotes: 1

mttrb
mttrb

Reputation: 8345

This is a little bit odd. The documentation for NSURLConnectionDelegate says:

The NSURLConnectionDelegate protocol defines the optional methods implemented by delegates of NSURLConnection objects.

The operative word there being optional.

However, the NSURLConnection class reference says:

NSURLConnection’s delegate methods—defined by the NSURLConnectionDelegate protocol—allow an object to receive informational callbacks about the asynchronous load of a URL request. Other delegate methods provide facilities that allow the delegate to customize the process of performing an asynchronous URL load. These delegate methods are called on the thread that started the asynchronous load operation for the associated NSURLConnection object.

These two seem to contradict themselves.

Finally, the "URL Loading System Programming Guide" says:

In order to download the contents of a URL, an application needs to provide a delegate object that, at a minimum, implements the following delegate methods: connection:didReceiveResponse:, connection:didReceiveData:, connection:didFailWithError: and connectionDidFinishLoading:.

Which is what you would expect as NSURLConnection is pretty much useless without these delegate methods.

Looking at the header file for NSURLConnection it appears there is an NSURLConnectionDataDelegate protocol which does list the required delegate methods. However, they are marked as @optional in the header. According to the definition of NSURLConnectionDataDelegate it must implement NSURLConnectionDelegate.

If you declare your delegate class to implement the NSURLConnectionDataDelegate protocol Xcode will autocomplete the delegate methods.

This all feels a bit messy to me. The Cocoa/Cocoa Touch APIs are usually much neater than this.

I certainly don't see that Apple could reject an app for using these delegate methods as NSURLConnection really doesn't work without them.

Upvotes: 4

Related Questions