Good articles to my personal technology blog: https://cainluo.github.io/14986211698053.html
preface
Although the previous two lectures covered some theoretical knowledge of NSURLSession, we now have at least a general understanding of NSURLSession. We are not as confused as we were at the beginning, wondering what the devil is this request, what the devil is that method, what the Task is, If you don’t go to see the friends can go to see the first two lectures: playing iOS development: NSURLSession explanation (1) and playing iOS development: NSURLSession explanation (2), big god at will.
This project is modeled after RayWenderlich’s URLSession Tutorial: Getting Started, if you like the original or Swift, this is the Objective-C version.
Note: this project is inXcode 9 beta 2
.iOS 11
Run in the environment of.
Build interface
I would like to explain here that the interface in the original text is constructed by Storyboard, while I would like to use code and navigation here. I would like to express my gratitude to the god again.
The built interface is also very simple, just a UITableView, UITableViewCell and UISearchBar, all done, the layout code is in the project, because I don’t have any artistic cells, so the interface is slightly ugly, we can make do with it.
The project architecture
In order to make it easier and clearer to read, I have used the MVVM architecture mode that I have always been used to. Although there are a few more files, the code of each file is guaranteed to be within 200 lines, which makes me feel more comfortable.
download
In the project, there is a QueryService and a DownloadService, which are responsible for managing downloads and some network requests. Because there is too much code, I will talk about the idea here.
QueryService: Responsible for the implementation of the search network request using UISearchBar. When UISearchBar enters the keyword, an effective search link is assembled through the specified URL and Query, and the network request is made through NSURLSessionDataTask. After success, the data will be analyzed. It is then assembled by the SearchModel and added to an NSMutableArray object, and finally the UITableView will use this NSMutableArray as the data source to refresh the entire UITableView.
DownloadService: Responsible for implementing UITableViewCell through DownloadModel click to start downloading, pause, resume downloading, cancel downloading and so on.
Here, of course, we also need to implement a called NSURLSessionDownloadDelegate and NSURLSessionDelegate two protocols, respectively:
// How to do after downloading
- (void)URLSession:(NSURLSession *)session
downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location;
// How to operate while downloading
- (void)URLSession:(NSURLSession *)session
downloadTask:(NSURLSessionDownloadTask *)downloadTask
didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite;
// The method after the background download is complete
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session;
Copy the code
The implementation of this can be seen in the SearchSessionDelegate, but I won’t go into details here.
The core content of the whole project is here. For more details, we can go to the project to have a look. We won’t waste your time here.
The end result:
conclusion
Finally nagging two words, because I this is just a Demo, there are bugs, there are not perfect package please forgive me, here incidentally push two excellent open source download manager HWIFileDownload, TCBlobDownload.
HJMURLDownloader is a universal download manager for ios. It is a popular download manager for ios. It is a popular download manager for ios.
Enjoy your study.
Project Address:
Interpretation (3) the address of the project: https://github.com/CainRun/iOS-NetWork/tree/master/NSURLSession