gpt4 book ai didi

ios - ios7 弹出 View 中的异步 nsurlconnection

转载 作者:行者123 更新时间:2023-11-29 03:11:36 26 4
gpt4 key购买 nike

我正在 ios 的弹出 View 中创建异步 NSURLconnection。为了实现异步 NSURLconnection,我实现了 NSURLDelegate 的方法。当用户点击弹出 View 外部并且 View 被关闭时,就会出现问题。使 View 中的 nsurlconnection 回调和其他操作不完整。尽管取消了 View ,我如何确保弹出窗口中的操作完成?我尝试在弹出 View 内放置一个事件指示器,直到操作完成,但即便如此,在弹出 View 外点击也会关闭该 View 。我不希望用户在操作完成之前留下一个不活动的应用程序,而是希望操作在后台完成。

最佳答案

如果你想发送一个异步连接,你可以使用这个方法。

获取请求

-(void)placeGetRequest:(NSString *)action withHandler:(void (^)(NSURLResponse *response, NSData *data, NSError *error))ourBlock {

NSString *url = [NSString stringWithFormat:@"%@/%@", URL_API, action];

NSURL *urlUsers = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:urlUsers];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:ourBlock];
}

POST 请求

-(void)placePostRequest:(NSString *)action withData:(NSDictionary *)dataToSend withHandler:(void (^)(NSURLResponse *response, NSData *data, NSError *error))ourBlock {
NSString *urlString = [NSString stringWithFormat:@"%@/%@", URL_API, action];
NSLog(urlString);

NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

// Creamos el JSON desde el data
NSError *error;

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dataToSend options:0 error:&error];

NSString *jsonString;
if (! jsonData) {
NSLog(@"Got an error: %@", error);
} else {
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

NSData *requestData = [NSData dataWithBytes:[jsonString UTF8String] length:[jsonString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];

[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];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:ourBlock];
}
}

使用示例

- (void) getMyMethod:(NSString *)myParam1
myParam2:(NSString *)myParam2
myParam3:(NSString *)myParam3
calledBy:(id)calledBy
withSuccess:(SEL)successCallback
andFailure:(SEL)failureCallback{
[self placeGetRequest:[NSString stringWithFormat:@"api/myMethod?myParam1=%@&myParam2=%@&myParam3=%@",myParam1, myParam2, myParam3]
withHandler:^(NSURLResponse *response, NSData *rawData, NSError *error) {

NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
NSInteger code = [httpResponse statusCode];
NSLog(@"%ld", (long)code);

if (code == 0){
// error
} else if (!(code >= 200 && code < 300) && !(code == 500)) {
NSString *string = [[NSString alloc] initWithData:rawData
encoding:NSUTF8StringEncoding];
NSLog(@"ERROR (%ld): %@", (long)code, string);
[calledBy performSelector:failureCallback withObject:string];
} else {

// If you receive a JSON
NSMutableDictionary *result = [NSJSONSerialization JSONObjectWithData:rawData options:0 error:nil];
// If you receive an Array
// NSArray *result = [NSJSONSerialization JSONObjectWithData:rawData options:0 error:nil];

// If you receive a string
// NSString *result = [[NSString alloc] initWithData:rawData encoding:NSUTF8StringEncoding];
[calledBy performSelector:successCallback withObject:result];
}
}];

调用您必须在您的 View / Controller /等中执行的操作

(...)

[api getMyMethod:myParam1Value myParam2:myParam2Value myParam3:myParam3Value calledBy:self withSuccess:@selector(getMyMethodDidEnd:) andFailure:@selector(getMyMethodFailureFailure:)];

(...)

// Don't forget to set your callbacks functions or callbacks will do your app crash

-(void)getMyMethodDidEnd:(id)result{
// your actions with the result
// ...
}

-(void)getMyMethodFailure:(id)result{
// your actions with the result
// ...
}

关于ios - ios7 弹出 View 中的异步 nsurlconnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22188855/

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