Post

Replies

Boosts

Views

Activity

Need to upload multipart form data( video, image with params) in background until upload is not finished no matter app is in background or switched to other app or terminated.
This Code is not working. Any Help will be appreciated. func UPLOAD_VIDEO_WITH_DATA(_ video: [UploadImageInfo], param: [String: Any]) { if !APIManager.isConnectedToNetwork() { APIManager().networkErrorMsg() return } let url = URL(string: API.CAMPAIGN.create)! var request = URLRequest(url: url) let boundary = "Boundary-\(NSUUID().uuidString)" var body = Data() body.append(boundary) body.append("--\(boundary)\r\n") body.append("Content-Disposition: form-data; name=\"params\"\r\n") body.append("Content-Type: application/json\r\n\r\n") body.append("\(param)") body.append("\r\n") let tempDirectoryURL = FileManager.default.temporaryDirectory let videoURL = tempDirectoryURL.appendingPathComponent("video.mp4") for videoInfo in video { if videoInfo.video != nil { if let videoData = videoInfo.data { try? videoData.write(to: videoURL) body.append("--\(boundary)\r\n") body.append("Content-Disposition: form-data; name=\"video\"; filename=\"\(videoInfo.video?.absoluteString)\"\r\n") body.append("Content-Type: video/mp4\r\n\r\n") body.append(videoData) body.append("\r\n") } } } body.append("--\(boundary)--\r\n") request.httpMethod = "POST" request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") request.setValue("Bearer \(self.getMultipartHeaderWithToken())", forHTTPHeaderField: "Authorization") request.httpBody = body print(request) let session = URLSession(configuration: URLSessionConfiguration.background(withIdentifier: "com.app.Deloa.backgroundUpload")) DispatchQueue.global().async { let task = session.uploadTask(with: request, fromFile: videoURL) task.resume() } } public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { if let error = error { print("Error uploading data: \(error)") } else { print("Data uploaded successfully!") } } extension Data { mutating func append(_ string: String) { if let data = string.data(using: .utf8) { append(data) } } } Here is the data i am passing for video ` override func selectedVideo(choosenVideo: URL, thumbnailImage: UIImage) { var selectedVideo = UploadImageInfo() selectedVideo.name = "video" selectedVideo.video = choosenVideo selectedVideo.data = try? Data(contentsOf: choosenVideo) selectedVideo.url = choosenVideo.absoluteString self.media.append(selectedVideo) var selectedThumbnail = UploadImageInfo() selectedThumbnail.name = "thumbnail" selectedThumbnail.image = thumbnailImage selectedThumbnail.data = compressImage(image: thumbnailImage) self.media.append(selectedThumbnail) }` Upload Image struct struct UploadImageInfo { var id: Int var name: String var image: UIImage? var video : URL? var thumbnail: UIImage? var data: Data? var url: String? init() { name = "" image = nil video = nil id = 0 thumbnail = nil data = nil url = nil } }
0
0
1.3k
Jul ’23
Xcode 16 Predictive Code Completion Model Stuck in Downloading
Recently Upgraded my System to macOS 15 Sequoia Beta and Xcode 16 Beta. Everything downloaded and working fine but Predictive Code Completion Model Stuck in Downloading. System - MacBook Air 2020, M1, 16GB RAM, 256GB(114GB available) storage.
Replies
6
Boosts
4
Views
3.8k
Activity
Jun ’24
Need to upload multipart form data( video, image with params) in background until upload is not finished no matter app is in background or switched to other app or terminated.
This Code is not working. Any Help will be appreciated. func UPLOAD_VIDEO_WITH_DATA(_ video: [UploadImageInfo], param: [String: Any]) { if !APIManager.isConnectedToNetwork() { APIManager().networkErrorMsg() return } let url = URL(string: API.CAMPAIGN.create)! var request = URLRequest(url: url) let boundary = "Boundary-\(NSUUID().uuidString)" var body = Data() body.append(boundary) body.append("--\(boundary)\r\n") body.append("Content-Disposition: form-data; name=\"params\"\r\n") body.append("Content-Type: application/json\r\n\r\n") body.append("\(param)") body.append("\r\n") let tempDirectoryURL = FileManager.default.temporaryDirectory let videoURL = tempDirectoryURL.appendingPathComponent("video.mp4") for videoInfo in video { if videoInfo.video != nil { if let videoData = videoInfo.data { try? videoData.write(to: videoURL) body.append("--\(boundary)\r\n") body.append("Content-Disposition: form-data; name=\"video\"; filename=\"\(videoInfo.video?.absoluteString)\"\r\n") body.append("Content-Type: video/mp4\r\n\r\n") body.append(videoData) body.append("\r\n") } } } body.append("--\(boundary)--\r\n") request.httpMethod = "POST" request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") request.setValue("Bearer \(self.getMultipartHeaderWithToken())", forHTTPHeaderField: "Authorization") request.httpBody = body print(request) let session = URLSession(configuration: URLSessionConfiguration.background(withIdentifier: "com.app.Deloa.backgroundUpload")) DispatchQueue.global().async { let task = session.uploadTask(with: request, fromFile: videoURL) task.resume() } } public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { if let error = error { print("Error uploading data: \(error)") } else { print("Data uploaded successfully!") } } extension Data { mutating func append(_ string: String) { if let data = string.data(using: .utf8) { append(data) } } } Here is the data i am passing for video ` override func selectedVideo(choosenVideo: URL, thumbnailImage: UIImage) { var selectedVideo = UploadImageInfo() selectedVideo.name = "video" selectedVideo.video = choosenVideo selectedVideo.data = try? Data(contentsOf: choosenVideo) selectedVideo.url = choosenVideo.absoluteString self.media.append(selectedVideo) var selectedThumbnail = UploadImageInfo() selectedThumbnail.name = "thumbnail" selectedThumbnail.image = thumbnailImage selectedThumbnail.data = compressImage(image: thumbnailImage) self.media.append(selectedThumbnail) }` Upload Image struct struct UploadImageInfo { var id: Int var name: String var image: UIImage? var video : URL? var thumbnail: UIImage? var data: Data? var url: String? init() { name = "" image = nil video = nil id = 0 thumbnail = nil data = nil url = nil } }
Replies
0
Boosts
0
Views
1.3k
Activity
Jul ’23
How to send user activity data to server in background when user closed/killed the application.
I have an application, in which I want to send data of user activity to our server when the user killed/closed the application. If it is unsuccessful in sending data to the server in the background then I will send all data when the user relaunches the app again. Is there a method to do this?
Replies
0
Boosts
0
Views
586
Activity
Feb ’23
Getting this error in Playground when I am opening Xcode using Rosetta.
A Failed to launch process. Failed to attach to stub for playground execution: error: debugserver is x86_64 binary running in translation, attached failed.. Did anyone experience this? I want to run playground using Rosetta?
Replies
6
Boosts
2
Views
6.4k
Activity
Oct ’22
How to take multiple input separated by space in swift??
How to take multiple inputs separated by space in swift??
Replies
2
Boosts
0
Views
1.1k
Activity
Jun ’22
Xcode playground is not working when I open xcode using Rosetta??
Showing this message - Failed to launch process. Failed to attach to stub for playground execution: error: attach failed ((os/kern) invalid argument)
Replies
1
Boosts
0
Views
1.5k
Activity
Feb ’22