如果你有很多,不同的類別下下降的URLConnections(比如說10個連接,滿分5是用於音頻,3顯示圖像,2 FO r開放鏈接等),更好的選擇是子類NSURLConnection
並創建一個自定義NSURLConnection
類。您可以將自己的屬性添加到此類中。並將您自己的自定義標籤定義爲不同類型的連接。在您的UIViewController
和委託方法嘗試使用這個子類對象,並使用此tag
屬性不同NSURLConnections
之間進行區分。
對於如: -
創建CustomNSURLConnection
文件,並寫入,
#define kAudioConnectionTag 100
#define kDisplayConnectionTag 200
#define kOpenURLConnectionTag 300
@interface CustomNSURLConnection : NSURLConnection
@property (nonatomic) NSInteger tag;
在UIViewController
類,
CustomNSURLConnection *audioConnection = [CustomNSURLConnection ...];
audioConnection.tag = kAudioConnectionTag;
CustomNSURLConnection *displayConnection = [CustomNSURLConnection ...];
audioConnection.tag = kDisplayConnectionTag;
CustomNSURLConnection *openURLConnection = [CustomNSURLConnection ...];
audioConnection.tag = kOpenURLConnectionTag;
- (void)connectionDidFinishLoading:(CustomNSURLConnection *)connection{
if (connection.tag == kAudioConnectionTag) {
//code
} else if (connection.tag == kDisplayConnectionTag) {
//code
} else {
//code
}
}