How to use a variable from urlSession from another class - ios

I am trying to download multiple files sequentially using urlSession.
I have this code that is in an extension in a different class. I would like to be able to use the variable progress in the viewcontroller class in order to use it to show the download progression on the screen.
How can I pass this variable from an extension from a different class into the viewcontroller class??
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
let progress = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite) * 100

Throw away your progress variable. The URLSessionDownloadTask itself vends a progress object (of the Progress class). It is made to solve exactly this sort of problem, because a Progress object is observable from anywhere (or it can be assigned to a UIProgressView's observedProgress directly). Just research the Progress class and it will be clear what to do.

Related

NSURLSession .waitsForConnectivity flag ignored when DataTask being run on OperationQueue

I have a handwritten class MyURLRequest, that implements Operation. Inside it creates URLSession, configures it
public init(shouldWaitForConnectivity: Bool, timeoutForResource: Double?) {
baseUrl = URL(string: Self.relevantServerUrl + "api/")
self.shouldWaitForConnectivity = shouldWaitForConnectivity
self.timeoutForResource = timeoutForResource
super.init()
localURLSession = URLSession(configuration: localConfig, delegate: self, delegateQueue: nil)
}
public var localConfig: URLSessionConfiguration {
let res = URLSessionConfiguration.default
res.allowsCellularAccess = true
if let shouldWaitForConnectivity = shouldWaitForConnectivity {
res.waitsForConnectivity = shouldWaitForConnectivity
if let timeoutForResource = timeoutForResource {
res.timeoutIntervalForResource = timeoutForResource
}
}
return res
}
creates URLRequest, dataTask, and then being run on OperationQueue. Operation's methods looks like this
override open func start() {
if isCancelled {
isFinished = true
return
}
startDate = Date()
sessionTask?.resume()
localURLSession.finishTasksAndInvalidate()
}
override open func cancel() {
super.cancel()
sessionTask?.cancel()
}
MyURLRequest also implements URLSessionDataDelegate and URLSessionTaskDelegate and the being delegate for it's own URLSession.
There is a problem with waitsForConnectivity NSURLSessionConfiguration's flag. In constructor I set it to true, but this flag is being ignored. In runtime, when network is turned off, request finishes immediately with error -1009. URLSessionTaskDelegate's method urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) is triggered immediately. func urlSession(_ session: URLSession, taskIsWaitingForConnectivity task: URLSessionTask) not being called at all.
The reason definitely not is that flag waitsForConnectivity wasn't correctly set: I've checked config in task received by urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?), and waitsForConnectivity == true.
I also tried to make request without operation queue, and that went fine - behaved such as expected. Maybe have something to do with OperationQueue. Would appreciate your help!
UPDATE:
Seems like root of the problem is that Operation being released too early (when request not complete yet). I've tried to synchronise them using DispatchGroup():
override open func start() {
if isCancelled {
isFinished = true
return
}
startDate = Date()
dispatchGroup.enter()
sessionTask?.resume()
dispatchGroup.wait()
localURLSession.finishTasksAndInvalidate()
}
where .leave() is called in URLSessionDelegate's methods. Nothing changed, still not waiting for connectivity.
UPDATE:
Here's the error I get in didCompleteWithError:
Error Domain=NSURLErrorDomain Code=-1009 "" UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x7fc319112de0 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <6388AD46-8497-40DF-8768-44FEBB84A8EC>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <6388AD46-8497-40DF-8768-44FEBB84A8EC>.<1>",
"LocalDataTask <26BCBD73-FC8B-4A48-8EA2-1172ABB8093C>.<1>"
), NSLocalizedDescription=., NSErrorFailingURLStringKey=}
I believe the issue is rooted in your use of finishTasksAndInvalidate. It looks like you are depending on that method synchronously waiting for all pending tasks to complete, but according to the documentation that isn't how it works.
Here's a more in-depth explanation of what I think is happening. By default an Operation is considered complete as soon as the start method returns. This is definitely not what you need, as the task has to be completed asynchronously. Operation is not capable of supporting this behavior out of the box.
Your start returns immediately, long before the session has any time to complete the task you have started. Then, with the operation complete, the queue removes it. This often ends up being the only owner of that instance. If that's true, it kicks off the operation deinit process, which ends up releasing the URLSession. At that point, the session looks like it might do some clean up and terminate any outstanding tasks, forwarding some calls to its delegate. I wasn't sure if URLSession does this, but based on what you are seeing, it sounds like it may.
To achieve what you want, I think you'll need to restructure your NSOperation subclass to be fully asynchronous and to only complete when the started task is done.
Building out a fully thread-safe async NSOperation subclass is a real pain. In case this isn't something you've tackled before, you can check out an implementation here: https://github.com/ChimeHQ/OperationPlus

SceneDelegate's "continue" not called when launching App Clip from Xcode

I am trying to test my app clip's url handler by running the app clip from Xcode. However the URL method handler (SceneDelegate's continue method) never gets called, contrary to Apple's documentation documentation, which states:
For a UIKit-based app clip and full app that support scene-based app
life-cycle events, implement the callbacks defined in UISceneDelegate.
For example, implement scene(_:continue:) callback to access the user
activity object.
For a UIKit-based app clip and full app that respond
to app-based life-cycle events, implement the callbacks defined in
UIApplicationDelegate. Be sure to implement the
application(:continue:restorationHandler:) callback, because you
don’t have access to the NSUserActivity object in
application(:didFinishLaunchingWithOptions:).
The app delegate does not implement the application(_:continue:restorationHandler:) method
The app clip's scheme has the _XCApplClipURL parameter enabled and set to https://fruits.com/check?fruit_name=bananas
The app clip's Associated Domain lists appclips:fruits.com
The app clip's SceneDelegate is as follows
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
// UGHH!!! Never gets called
print("AppClip invocation url is : \(incomingURL)")
}
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// OK!! This gets called
guard let _ = (scene as? UIWindowScene) else { return }
}
}
I have been banging my head on the walls for the last 2 days. What am I missing?
Note: I am using this sample app available in github, just modified the signing configuration to get the app clip to compile & run.
The continue method mentioned is only called when your app is invoked after it was previously opened. In order to get the value set in _XCApplClipURL the first time your app is launched, you need to use the second method you mentioned (scene willConnectTo session).
You can try something like this:
if let activity = connectionOptions.userActivities.filter({ $0.activityType == NSUserActivityTypeBrowsingWeb }).first {
if let url = activity.webpageURL {
print("incoming URL: \(url)")
}
}
Thanks for your using AppsFlyer's public demo of App Clip and for highlighting this issue. I really appreciate it.
I can confirm you were doing great, this functionality was broken sometime after Beta 5.
Please follow and comment on this issue here

simple URLSession uploadTask with progress bar

i struggle totally with the URLSession and uploadTask. Actually I just want to upload a json to a Webserver and while the upload is in progress a simple progressbar should be shown.
I implemented the approach given by apple: https://developer.apple.com/documentation/foundation/url_loading_system/uploading_data_to_a_website
the upload is working and i get a response.. so far everything is fine but i don't know how i can show a during the upload a progress bar. What I tried is to show a progressbar before i call the method that contains the upload task
startActivityIndicator()
let jsonPackage = JSONIncident(incident: incident)
jsonPackage.sendToBackend(completion: {
message, error in
//Handle the server response
})
self.activityIndicator.stopAnimating()
UIApplication.shared.endIgnoringInteractionEvents()
I realized that's stupid because the upload task works asynchronious in another thread.
I guess that i have to use Delegates but i don't know in which way. If i implement URLSessionTaskDelegate for example, i have to implement a bunch of functions like isProxy(), isKind(), isMember etc.
Could you please provide me a simple example how to show a progress bar during the uploadtask is working?
That would be so greate!
Thank you very much
regardsChris
You need to conform to the URLSessionTaskDelegate protocol and call this delegate method:
func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64)
{
let uploadProgress = Float(totalBytesSent) / Float(totalBytesExpectedToSend)
}
Then use the uploadProgress variable for your progress bar.
Create your session like this:
var session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: OperationQueue.main)
You can use alamofire(for api calling) with swiftyJSON(json parsing) library for uploading JSON. You just have to call a loader before the alamofire api get called and stop the loader when you will get the response.

NSURLSession in background didCompleteWithError

I'm fighting with NSURLSession upload & download, everything seems to work if not for the fact that, for example, at in the background of 100 files sent (upload) the :
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
not always invoked by the system, regardless of the file sent correctly.
More is almost always invoked at the end of the session ...
It is normal behavior or am I making mistakes ... ??
I also did some testing using AFNetworking but I always get the same behavior.
thanks !
In my case delegate method func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) was not called in Swift 3.0 because it still depends on Objective-C. So please use next code for delegate method:
#objc(URLSession:task:didCompleteWithError:)
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
//your code here
}

iOS: Perform upload task while app is in background

Is there really no way to run an UPLOAD task while an iOS app is in the background? This is ridiculous. Been looking at various stuff like NSURLSessionUploadTask, dispatch_after and even NSTimer, but nothing works for more than the meager 10 seconds the app lives after being put in the background.
How do other apps that have uploads work? Say, uploading an image to Facebook and putting the app in the background, will that cancel the upload?
Why cannot iOS have background services or agents like Android and Windows Phone has?
This is a critical feature of my app, and on the other platforms is works perfectly.
Any help is appreciated :(
You can continue uploads in the background with a “background session”. The basic process of creating a background URLSessionConfiguration with background(withIdentifier:) is outlined in Downloading Files in the Background. That document focuses on downloads, but the same basic process works for upload tasks, too.
Note:
you have to use the delegate-based URLSession;
you cannot use the completion handler renditions of the task factory methods with background sessions;
you also have to use uploadTask(with:fromFile:) method, not the Data rendition ... if you attempt to use uploadTask(with:from:), which uses Data for the payload, with background URLSession you will receive exception with a message that says, “Upload tasks from NSData are not supported in background sessions”; and
your app delegate must implement application(_:handleEventsForBackgroundURLSession:completionHandler:) and capture that completion handler which you can then call in your URLSessionDelegate method urlSessionDidFinishEvents(forBackgroundURLSession:) (or whenever you are done processing the response).
By the way, if you don't want to use background NSURLSession, but you want to continue running a finite-length task for more than a few seconds after the app leaves background, you can request more time with UIApplication method beginBackgroundTask. That will give you a little time (formerly 3 minutes, only 30 seconds in iOS 13 and later) complete any tasks you are working on even if the user leave the app.
See Extending Your App's Background Execution Time. Their code snippet is a bit out of date, but a contemporary rendition might look like:
func initiateBackgroundRequest(with data: Data) {
var backgroundTaskID: UIBackgroundTaskIdentifier = .invalid
// Request the task assertion and save the ID.
backgroundTaskID = UIApplication.shared.beginBackgroundTask(withName: "Finish Network Tasks") {
// End the task if time expires.
if backgroundTaskID != .invalid {
UIApplication.shared.endBackgroundTask(backgroundTaskID)
backgroundTaskID = .invalid
}
}
// Send the data asynchronously.
performNetworkRequest(with: data) { result in
// End the task assertion.
if backgroundTaskID != .invalid {
UIApplication.shared.endBackgroundTask(backgroundTaskID)
backgroundTaskID = .invalid
}
}
}
Please don’t get lost in the details here. Focus on the basic pattern:
begin the background task;
supply a timeout clause that cleans up the background task if you happen to run out of time;
initiate whatever you need to continue even if the user leaves the app; and
in the completion handler of the network request, end the background task.
class ViewController: UIViewController, URLSessionTaskDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://0.0.0.0")!
let data = "Secret Message".data(using: .utf8)!
let tempDir = FileManager.default.temporaryDirectory
let localURL = tempDir.appendingPathComponent("throwaway")
try? data.write(to: localURL)
let request = URLRequest(url: url)
let config = URLSessionConfiguration.background(withIdentifier: "uniqueId")
let session = URLSession(configuration: config, delegate: self, delegateQueue: nil)
let task = session.uploadTask(with: request, fromFile: localURL)
task.resume()
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
print("We're done here")
}

Resources