简体   繁体   English

不推荐使用:'sendAsynchronousRequest:queue:completionHandler:' 在 iOS9 中

[英]Deprecated: 'sendAsynchronousRequest:queue:completionHandler:' in iOS9

I have my own class to make http calls but now in iOS9 this method is deprecated:我有自己的类来进行 http 调用,但现在在 iOS9 中此方法已弃用:

[NSURLConnetion sendAsynchronousRequest:queue:completionHandler:]

I'm trying to test the new one [NSURLSession dataTaskWithRequest:completionHandler:]我正在尝试测试新的[NSURLSession dataTaskWithRequest:completionHandler:]

but Xcode give an error because it doesn't found this method.但是 Xcode 给出了一个错误,因为它没有找到这个方法。

Xcode compiler warning with deprecated line:带有弃用行的 Xcode 编译器警告:

 'sendAsynchronousRequest:queue:completionHandler:' is deprecated: first deprecated in iOS 9.0 - Use [NSURLSession dataTaskWithRequest:completionHandler:] (see NSURLSession.h

Error with new method:新方法错误:

No known class method for selector 'dataTaskWithRequest:completionHandler:'

Method:方法:

-(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];
    [NSURLSession dataTaskWithRequest:request completionHandler:ourBlock];
}

Any idea?任何的想法?

dataTaskWithRequest:completionHandler: is an instance method, not a class method. dataTaskWithRequest:completionHandler:是一个实例方法,而不是一个类方法。 You have to either configure a new session or use the shared one:您必须配置新会话或使用共享会话:

[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:ourBlock] resume];
[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data,NSURLResponse *response,NSError *error)
 {
 // Block Body 
 }];

If you are using the AFNetworking library, you can use a session and then set up to support requests in the background.如果您使用的是AFNetworking库,则可以使用会话,然后设置为在后台支持请求。 Using this approach I solved two problems:使用这种方法我解决了两个问题:

1) AFNetworking method is not deprecated 1) 不推荐使用 AFNetworking 方法

2) the request processing will be done even if the application between the state background 2) 即使应用程序在状态后台之间也会进行请求处理

I hope it helps to similar problems.我希望它有助于解决类似的问题。 This is an alternative.这是一个替代方案。

-(void) pingSomeWebSite {

    NSString* url = URL_WEBSITE; // defines your URL to PING

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setHTTPMethod:@"GET"];
    [request setURL:[NSURL URLWithString:url]];
    request.timeoutInterval = DEFAULT_TIMEOUT; // defines your time in seconds

    NSTimeInterval  today = [[NSDate date] timeIntervalSince1970];
    NSString *identifier = [NSString stringWithFormat:@"%f", today];

    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:identifier];
    sessionConfig.timeoutIntervalForResource = DEFAULT_TIMEOUT_INTERVAL; // interval max to background 

    __block AFURLSessionManager *manager = [[AFURLSessionManager alloc]
                                            initWithSessionConfiguration:
                                            sessionConfig];

    NSURLSessionTask *checkTask = [manager dataTaskWithRequest:request
                                             completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {

        NSLog(@"- Ping to - %@", URL_WEBSITE);

        NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
        dispatch_async(dispatch_get_main_queue(), ^(){

            [manager invalidateSessionCancelingTasks:YES];

            // LOGIC FOR RESPONSE CODE HERE 
        });
    }];

    [checkTask resume];
}
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in

// Code

}
task.resume()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NSURLConnection sendAsynchronousRequest:queue:completionHandler在iOS 4.3中不起作用 - NSURLConnection sendAsynchronousRequest:queue:completionHandler not working in iOS 4.3 iOS4的实现 - [NSURLConnection sendAsynchronousRequest:queue:completionHandler:]? - iOS4 Implementation of -[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]? 进行多个NSURLConnections并使用sendAsynchronousRequest:queue:completionHandler:iOS 5方法 - Make multiple NSURLConnections and using sendAsynchronousRequest:queue:completionHandler: iOS 5 method 如何使用sendAsynchronousRequest:queue:completionHandler: - how to use sendAsynchronousRequest:queue:completionHandler: sendAsynchronousRequest:queue:completionHandler:具有委托方法 - sendAsynchronousRequest:queue:completionHandler: With Delegate Methods 使用sendAsynchronousRequest:queue:completionHandler:进行URL请求 - Using sendAsynchronousRequest:queue:completionHandler: for URL request 在iOS 9中不推荐使用sendAsynchronousRequest - sendAsynchronousRequest deprecated in iOS 9 使用sendAsynchronousRequest处理401响应:queue:completionHandler: - Handling a 401 response with sendAsynchronousRequest:queue:completionHandler: NSURLConnection sendAsynchronousRequest:queue:completionHandler:HTTP或HTTPS - NSURLConnection sendAsynchronousRequest:queue:completionHandler: HTTP or HTTPS 使用sendAsynchronousRequest时跟踪重定向:queue:completionHandler:? - Track redirects when using sendAsynchronousRequest:queue:completionHandler:?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM