mirror of
https://github.com/OAuthSwift/OAuthSwift.git
synced 2026-04-26 12:45:52 +03:00
[GH-ISSUE #308] Add support for background URLSessions #187
Labels
No labels
bug
cocoapod
duplicate
enhancement
feature-request
help wanted
help wanted
invalid
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/OAuthSwift#187
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @alazaro on GitHub (Nov 10, 2016).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/308
Description:
The App Programming Guide for watchOS recommends the use of background sessions when making network-based operations.
The library uses
URLSessionConfiguration.defaultby default, but it would be good to be able to useURLSessionConfiguration.background(withIdentifier:)when needed.@phimage commented on GitHub (Nov 10, 2016):
I add a global configuration object
OAuthSwift.sessionwhere you can change theconfiguration(URLSessionConfiguration)@alazaro commented on GitHub (Nov 10, 2016):
That was fast! Thanks!!
@antwerpenR commented on GitHub (Nov 15, 2016):
Thanks for adding this - just what I was looking for too. Can you help me to know how I should configure background session? I am trying with
let config = URLSessionConfiguration.background(withIdentifier: "bgConfig") OAuthSwift.session.configuration = configbut this will not compile - it gives me error " 'configuration' is inaccessible due to 'internal' protection level.
From a bit of research, I think it might be related to this: http://taylorfranklin.me/2016/07/10/the-default-memberwise-initializers-headache/
After a bit of experimentation, I was able to proceed by setting the structure variables also to "public" in your code for OAuthSwift (so maybe this is a bug - or I am doing it wrong?)
`extension OAuthSwift {
}
`
...which lead to my next question. I do not want all my sessions to be background - only those for which I am doing multi-part/form upload (photos). How to do this since I think this is global? I guess that I need to create another OAuth2Swift instance and set the session only on it....is that correct?
@phimage commented on GitHub (Nov 15, 2016):
I do it to fast maybe. Not tested yet. So maybe there is an access problem.
Then I do it in a static way so there is no way to configure by OAuthSwift object or even by request
I will edit to be editable by OAuthSwift
@antwerpenR commented on GitHub (Nov 15, 2016):
@phimage - yes - I think there is an access problem. It does seem that my fix works for me...when I set the elements of the struct as public I can set them OK from my code - so do not be too quick to implement it some other way!
Still - it is important for me to have some sessions working with default settings (so that I can use callbacks) and others with background settings (which need to work with delegate methods)....so it is important that any implementation allows this configuration. Hope this helps and I am happy to help test anything going forward!
@phimage commented on GitHub (Nov 22, 2016):
an other big change has been done to be able to update it by
OAuthSwiftinstance,more precisely in
OAuthSwiftClientinstanceoauthSwift.client.sessionFactoryYou can create a new one or assigning one instance of your choice
or you can update it
N.B.: This
URLSessionFactory(from client instance) is passed to requestConfigobject@antwerpenR commented on GitHub (Nov 25, 2016):
Thanks for this update....but I am having problems using it - I get runtime error:
2016-11-25 10:47:25.746 API-Sandbox[66217:5638450] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Completion handler blocks are not supported in background sessions. Use a delegate instead.'My calling code is:
`....
backgroundOauthswift.client.sessionFactory.configuration = URLSessionConfiguration.background(withIdentifier: "meetupApp")
backgroundOauthswift.client.sessionFactory.delegate = self
backgroundOauthswift.client.postMultiPartRequest(
requiredURL,
method: .POST,
parameters: ["sign":"true", "photo-host": "secure"],
headers: [:],
multiparts: [multipart],
checkTokenExpiration: true,
success: nil,
failure: nil
)
dismiss(animated: true, completion: nil)
}
}
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
print("DID FINISH: urlSessionDidFinishEvents, (session.description)")
}
`
I think I am NOT passing closures since I set
successandfailureto nil insteadI have set my calling class as URLSessionDelegate and provided the three optional methods...
What am I doing wrong?
@phimage commented on GitHub (Nov 25, 2016):
OAuthSwift use the builded session with a completion handler, maybe not authorized in background session...
"self.session.dataTask(with: usedRequest) { (data, resp, error) in"
@antwerpenR commented on GitHub (Nov 25, 2016):
So...indeed, this looks like a bug?
data task convenience methods. These methods create tasks that * **bypass the normal delegate calls for response and data delivery**, * and provide a simple cancelable asynchronous interface to receiving * data. Errors will be returned in the NSURLErrorDomain, * see <Foundation/NSURLError.h>. The delegate, if any, will still be * called for authentication challenges..Can you do a fix or point me to how to avoid the problem?
@phimage commented on GitHub (Nov 25, 2016):
That's a lot of change and I have not time to do it now
@antwerpenR commented on GitHub (Nov 25, 2016):
Thanks - I think I need to wait till you have time to do this properly - it goes a bit beyond my current skill and knowledge level:
I tried modifying your code:
if self.session.delegate == nil { //do all the existing code as normal with callbacks } else { self.task = self.session.dataTask(with: usedRequest) }But I get then called with the delegate call
didReceive challenge: completionHandlerand I am not sure how to handle that properly...although it does work for the other foreground calls...@phimage commented on GitHub (Jun 25, 2018):
After a very long time (sorry) I add
useDataTaskClosureinURLSessionFactoryIf false, then we call
self.task = self.session.dataTask(with: usedRequest)without the closureIt is not sufficient because in
delegateyou have to do a lot of work, same as OAutSwift framework doA first step maybe is to call
OAuthSwiftHTTPRequest.completionHandlerbut success and failure handler are overriden internally ( to extract token before calling the handler passed by developper)@CuriousDev21 commented on GitHub (May 12, 2021):
Any update on this matter? I also have to keep requests running in background. Looking for an example with OAuthSwift.
Thanks in advance.
@phimage commented on GitHub (May 12, 2021):
no
if you really need it and have code to test background session, you could make PR