ios - iOS 9.0 中不推荐使用 ConnectionWithRequest
<p><p>我想从我的主机下载一些项目,
但现在我收到警告:
<strong>'connectionWithRequest:delegate:'在 iOS9.0 中已弃用 - 使用 NSURLSession'</strong></p>
<p>我到处搜索,但不幸的是我找不到任何解决方案。</p>
<p>你能帮帮我吗?</p>
<p>我的代码如下所示:</p>
<pre><code>- (void)downloadItems
{
// Download the json file
NSURL *jsonFileUrl = ;
// Create the request
NSURLRequest *urlRequest = [ initWithURL:jsonFileUrl];
// Create the NSURLConnection
;
}
#pragma mark NSURLConnectionDataProtocol Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// Initialize the data object
_downloadedData = [ init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the newly downloaded data
;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Create an array to store the locations
NSMutableArray *_locations = [ init];
// Parse the JSON that came in
NSError *error;
NSArray *jsonArray = ;
// Loop through Json objects, create question objects and add them to our questions array
for (int i = 0; i < jsonArray.count; i++)
{
NSDictionary *jsonElement = jsonArray;
// Create a new location object and set its props to JsonElement properties
Location *newLocation = [ init];
newLocation.idS = jsonElement[@"idStatistic"];
newLocation.temp = jsonElement[@"temp"];
newLocation.hum = jsonElement[@"hum"];
newLocation.date_time = jsonElement[@"date_time"];
// Add this question to the locations array
;
}
// Ready to notify delegate that data is ready and pass back items
if (self.delegate)
{
;
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>将 <code>NSURLConnection</code> 行替换为:</p>
<pre><code> NSURLSession *session = ;
[[session dataTaskWithURL:jsonFileUrl
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
// handle response
}] resume];
</code></pre></p>
<p style="font-size: 20px;">关于ios - iOS 9.0 中不推荐使用 ConnectionWithRequest,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/32838337/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/32838337/
</a>
</p>
页:
[1]