如果您有很多属于不同类别的 urlconnections(例如 5 个连接中有 10 个用于音频,3 个用于显示图像,2 个用于打开链接等),更好的选择是子类化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
}
}