使用NSURLSession检查连接响应

时间:2015-12-06 19:21:19

标签: ios

由于NSURLConnection已在IOS 9.0中弃用,因此它说我必须从现在开始使用NSURLSession。我需要一些帮助。我将NSURLConnection与委托一起用于检查URL是否给出了响应。我如何使用NSURLSession执行此操作?

我的代码:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"HEAD"];
    NSURLConnection *connection;
    connection = [NSURLConnection connectionWithRequest:request delegate:self];

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    if ([(NSHTTPURLResponse *)response statusCode] == 200) {
        //Do Things
    }
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    //Do other things
}

谢谢!

1 个答案:

答案 0 :(得分:0)

最简单的方法是使用<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script> <div id="locationField"> <input id="autocomplete" placeholder="Enter your address" onFocus="geolocate()" type="text" /> </div> <table id="address"> <tr> <td class="label">Street address</td> <td class="slimField"> <input class="field" id="street_number" disabled="true" /> </td> <td class="wideField" colspan="2"> <input class="field" id="route" disabled="true" /> </td> </tr> <tr> <td class="label">Full Street address</td> <td class="doubleField" colspan="3" disabled="true"> <input class="field" id="fullAddr" disabled="true" /> </td> </tr> <tr> <td class="label">City</td> <td class="wideField" colspan="3"> <input class="field" id="locality" disabled="true" /> </td> </tr> <tr> <td class="label">State</td> <td class="slimField"> <input class="field" id="administrative_area_level_1" disabled="true" /> </td> <td class="label">Zip code</td> <td class="wideField"> <input class="field" id="postal_code" disabled="true" /> </td> </tr> <tr> <td class="label">Country</td> <td class="wideField" colspan="3"> <input class="field" id="country" disabled="true" /> </td> </tr> </table>的完成块再现。这样您就可以享受可取消的异步请求,但无需编写任何委托方法。如果您不需要会话中的任何特殊内容(例如自定义缓存等),您可以使用NSURLSession来保存您创建和配置sharedSession

NSURLSession