nsurlsession

NSURLSession delegate vs. completionHandler

十年热恋 提交于 2019-11-30 10:39:41
问题 I've always used completion handlers. With NSURLConnection and now with NSURLSession . It's led to my code being really untidy, especially I have request within request within request. I wanted to try using delegates in NSURLSession to implement something I've done untidily with NSURLConnection . So I created a NSURLSession , and created a dataTask : NSURLSessionDataTask *dataTask = [overallSession dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)

Expand a short URL in Swift [closed]

谁说胖子不能爱 提交于 2019-11-30 10:35:00
Given a short URL https://itun.es/us/JB7h_ , How do you expand it into the full URL? Zelko Extension extension NSURL { func expandURLWithCompletionHandler(completionHandler: (NSURL?) -> Void) { let dataTask = NSURLSession.sharedSession().dataTaskWithURL(self, completionHandler: { _, response, _ in if let expandedURL = response?.URL { completionHandler(expandedURL) } }) dataTask.resume() } } Example let shortURL = NSURL(string: "https://itun.es/us/JB7h_") shortURL?.expandURLWithCompletionHandler({ expandedURL in print("ExpandedURL:\(expandedURL)") //https://itunes.apple.com/us/album/blackstar

Swift 3, URLSession dataTask completionHandler not called

六眼飞鱼酱① 提交于 2019-11-30 09:59:01
I am writing a library, So not using UIKit, Even in my iOS app same code works, but when i execute in command line in doesn't . In PlayGround also it seems working. For some reason callback is not getting triggered, so print statements are not executing. internal class func post(request: URLRequest, responseCallback: @escaping (Bool, AnyObject?) -> ()) { execTask(request: request, taskCallback: { (status, resp) -> Void in responseCallback(status, resp) }) } internal class func clientURLRequest(url: URL, path: String, method: RequestMethod.RawValue, params: Dictionary<String, Any>? = nil) ->

NSURLSession with invalid resume data

放肆的年华 提交于 2019-11-30 09:58:32
I use [NSURLSessionConfiguration defaultSessionConfiguration] to config my url session. I pause a task by calling cancelByProducingResumeData: to produce a resume data, and save it to the disk. When I want to restart the task, I call downloadTaskWithResumeData: . It works well until I restart the app. I kill the app after I pause a task. Then I start my app again, and call downloadTaskWithResumeData , I found that the resume data was invalid. I parse the resume data into NSDictionary and get the NSURLSessionResumeInfoLocalPath , which is "/private/var/mobile/Containers/Data/Application

WKURLSessionRefreshBackgroundTask isn't called when attempting to do background refreshes in watchOS

旧巷老猫 提交于 2019-11-30 09:36:55
I'm working on a complication to get scheduled data from a web service. Every 20-30 minutes (or manually), I am scheduling a WKRefreshBackgroundTask to do this. As suggested by Apple, I want the OS to handle the fetching of this data via a background NSURLSession . This is the function I use to download the data I need: func scheduleURLSession() { print("\nScheduling URL Session...") let backgroundSessionConfig:URLSessionConfiguration = URLSessionConfiguration.background(withIdentifier: NSUUID().uuidString) backgroundSessionConfig.sessionSendsLaunchEvents = true let backgroundSession =

Does NSUrlSession continue file transfer if the app is killed from task manager?

断了今生、忘了曾经 提交于 2019-11-30 09:21:02
I have tried various samples from the web (the last one being this one ) in order to get a better understanding of NSUrlSession . What I was hoping to see: file downloads will continue even if the app that triggered them gets killed (for instance by the user through the task manager). However this does not seem to happen. Is this a configuration issue or does background file transfer not work if the app gets terminated? I thought the whole idea was that iOS will restart the app. If the system kills your app and your background session has active downloads, your downloads will continue and the

My NSURLSessionDelegate methods are not getting called during a background download

混江龙づ霸主 提交于 2019-11-30 09:06:38
I'm trying to set up a download using NSURLSession that will continue in the background. I have a singleton class called DownloadManager which builds the NSURLSession and starts a download task like this: - (id)init { self = [super init]; if (self) { self.queue = [[NSOperationQueue alloc] init]; self.queue.maxConcurrentOperationCount = 1; // Initialize the background session. self.session = [self backgroundSession]; } return self; } - (NSURLSession *)backgroundSession { static NSURLSession *session = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSURLSessionConfiguration

How to make NSURLSession POST request in Swift

我们两清 提交于 2019-11-30 08:18:10
Hi I am very beginner for Swift and I am trying to make NSURLSession "Post" request sending some parameter like my below code According to my below code response not coming from server can some one help me please BackGroundClass:- import UIKit protocol sampleProtocal{ func getResponse(result:NSDictionary) func getErrorResponse(error:NSString) } class BackGroundClass: NSObject { var delegate:sampleProtocal? func callPostService(url:String,parameters:NSDictionary){ print("url is===>\(url)") let request = NSMutableURLRequest(URL: NSURL(string:url)!) let session = NSURLSession.sharedSession()

Average progress of all the NSURLSessionTasks in a NSURLSession

試著忘記壹切 提交于 2019-11-30 06:31:44
问题 An NSURLSession will allow you to add to it a large number of NSURLSessionTask to download in the background. If you want to check the progress of a single NSURLSessionTask , it’s as easy as double taskProgress = (double)task.countOfBytesReceived / (double)task.countOfBytesExpectedToReceive; But what is the best way to check the average progress of all the NSURLSessionTasks in a NSURLSession ? I thought I’d try averaging the progress of all tasks: [[self backgroundSession]

NSURLSession background download - resume over network failure

无人久伴 提交于 2019-11-30 06:23:20
问题 After reading the Apple documentation about the background download with the new iOS7 api (NSURLSession), I'm a bit disappointed. I was sure that Apple was managing the pause/resume over the network availability in the background (or provide an option to do so) but no… So reading the documentation, this is what we've got: https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/URLLoadingSystem/NSURLSessionConcepts/NSURLSessionConcepts.html When any task completes, the