fix: swift stuff
This commit is contained in:
@ -54,19 +54,16 @@ class ShareViewController: SLComposeServiceViewController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override func didSelectPost() {
|
override func didSelectPost() {
|
||||||
refreshToken {
|
refreshToken { accessToken in
|
||||||
// This method is called when the user taps the "Post" button.
|
guard let token = accessToken else {
|
||||||
// Start the asynchronous operation here.
|
// Inform the user about the authentication failure
|
||||||
|
let error = NSError(domain: "ShareExtension", code: -1, userInfo: [NSLocalizedDescriptionKey: "Authentication failed. Please log in again."])
|
||||||
guard let provider = self.imageItemProvider else {
|
self.extensionContext!.cancelRequest(withError: error)
|
||||||
print("Error: No image item provider found when posting.")
|
|
||||||
// Inform the user or log an error
|
|
||||||
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let token = self.bearerToken else {
|
guard let provider = self.imageItemProvider else {
|
||||||
print("Error: Bearer token is missing when posting.")
|
print("Error: No image item provider found when posting.")
|
||||||
// Inform the user or log an error
|
// Inform the user or log an error
|
||||||
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
|
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
|
||||||
return
|
return
|
||||||
@ -116,9 +113,6 @@ class ShareViewController: SLComposeServiceViewController {
|
|||||||
// Now perform the upload asynchronously
|
// Now perform the upload asynchronously
|
||||||
self.uploadRawData(dataToUpload, imageName: finalImageName, bearerToken: token)
|
self.uploadRawData(dataToUpload, imageName: finalImageName, bearerToken: token)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do not complete the request here.
|
|
||||||
// The request will be completed in the uploadRawData completion handler.
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,7 +153,12 @@ class ShareViewController: SLComposeServiceViewController {
|
|||||||
task.resume()
|
task.resume()
|
||||||
}
|
}
|
||||||
|
|
||||||
func refreshToken(completion: @escaping () -> Void) {
|
func refreshToken(completion: @escaping (String?) -> Void) {
|
||||||
|
guard let refreshToken = self.refreshToken else {
|
||||||
|
completion(nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
let url = URL(string: "https://haystack.johncosta.tech/auth/refresh")!
|
let url = URL(string: "https://haystack.johncosta.tech/auth/refresh")!
|
||||||
var request = URLRequest(url: url)
|
var request = URLRequest(url: url)
|
||||||
request.httpMethod = "POST"
|
request.httpMethod = "POST"
|
||||||
@ -168,22 +167,16 @@ class ShareViewController: SLComposeServiceViewController {
|
|||||||
let body = ["refresh": refreshToken]
|
let body = ["refresh": refreshToken]
|
||||||
request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: [])
|
request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: [])
|
||||||
|
|
||||||
let task = URLSession.shared.dataTask(with: request) { [weak self] (data, response, error) in
|
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
|
||||||
guard let self = self else { return }
|
|
||||||
|
|
||||||
if let data = data,
|
if let data = data,
|
||||||
let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
|
let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
|
||||||
let accessToken = json["access"] as? String {
|
let accessToken = json["access"] as? String {
|
||||||
|
completion(accessToken)
|
||||||
if let sharedDefaults = UserDefaults(suiteName: self.appGroupName) {
|
} else {
|
||||||
sharedDefaults.set(accessToken, forKey: self.tokenKey)
|
completion(nil)
|
||||||
self.bearerToken = accessToken
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
completion()
|
|
||||||
}
|
|
||||||
|
|
||||||
task.resume()
|
task.resume()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user