gpt4 book ai didi

ios - 一个函数中的 NSURLConnectionDelegate 回调

转载 作者:行者123 更新时间:2023-11-29 10:48:56 25 4
gpt4 key购买 nike

我正在尝试创建我自己的 Request 类,我打算在我的整个应用程序中使用它。这是我到目前为止一直想出的代码。

-(IIWRequest *)initAndLaunchWithDictionnary:(NSDictionary *)dictionnary
{
self=[super init];
if (self) {
// Create the request.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://xxxxx.com/app/"]];

// Convert data
SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];
NSString *jsonData = [jsonWriter stringWithObject:dictionnary];
NSLog(@"jsonData : %@",jsonData);
NSData *requestData = [jsonData dataUsingEncoding: NSUTF8StringEncoding];
request.HTTPBody = requestData;

// This is how we set header fields
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

// Create url connection and fire request
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[self activateNetworkActivityIndicator];
if (connection) {
NSLog(@"Connection");
} else {
NSLog(@"No connection");
}
}
return self;
}

我已经包含了 NSURLConnectionDelegate。我想触发连接回调,例如 did finished 或 did fail back 到前面提到的函数。所有这些的目标是最终只调用一种方法,如下所示:

-(IIWRequest *)initAndLaunchWithDictionnary:(NSDictionary *)dictionary inBackgroundWithBlock:^(BOOL succeeded){}

有什么想法吗?谢谢!

最佳答案

使用 NSURLConnection 类的 block 方法也会降低你的功能 sendAsynchronousRequest:queue:completionHandler:

阅读此 doc .

关于ios - 一个函数中的 NSURLConnectionDelegate 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21262922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com