I’m utilizing WhatsApp cloud API in my iOS (swift) app to add media to the WhatsApp server. I’m giving a hyperlink right here for reference https://builders.fb.com/docs/whatsapp/cloud-api/reference/media
Right here is my implementation of the API name in swift.
func uploadVideo(index: Int, previousProgress: Progress?) {
guard index < totalSegments else {
createMedia()
return
}
let parameters = [
[
"key": "messaging_product",
"value": "whatsapp",
"type": "text"
],
[
"key": "file",
"src": outputURLs[index].path,
"sort": "file"
]
] as [[String: Any]]
let headers: HTTPHeaders = [
"Authorization": "Bearer (wpToken)"
]
print("url: (uploadURL)")
print("para: (parameters)")
print("header: (headers)")
if uploadURL != "" {
var urlRequest = strive! URLRequest(url: uploadURL, methodology: .put up, headers: headers)
urlRequest.timeoutInterval = 120
uploadReq = AF.add(multipartFormData: { multipartFormData in
for param in parameters {
if param["type"] as? String == "textual content", let key = param["key"] as? String, let worth = param["value"] as? String {
multipartFormData.append(worth.information(utilizing: .utf8)!, withName: key)
} else if param["type"] as? String == "file", let key = param["key"] as? String, let src = param["src"] as? String {
let fileURL = URL(fileURLWithPath: src)
multipartFormData.append(fileURL, withName: key)
}
}
}, with: urlRequest)
.uploadProgress { progress in
self.totalProgress.completedUnitCount = previousProgress?.completedUnitCount ?? 0
self.totalProgress.totalUnitCount = previousProgress?.totalUnitCount ?? 0
self.totalProgress.completedUnitCount += progress.completedUnitCount
self.totalProgress.totalUnitCount += progress.totalUnitCount
}
.responseData { response in
if response.error != nil {
print("UPLOAD VIDEO RESPONSE ERROR (index) = (String(describing: response.error?.localizedDescription))")
if !self.isclosed {
self.view.makeToast("SomethingWrongKey".localizableString(loc: MyVariables.selectedLang), length: 3.0, place: .backside, title: nil, picture: UIImage(named: "AppLogo"))
DispatchQueue.essential.asyncAfter(deadline: .now() + 4.0) {
self.delegate?.updateUIOnDismiss()
if self.didOpenWP {
self.delegate?.updateUIOnDismiss()
for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: PhotoVideoViewController.self) {
self.navigationController!.popToViewController(controller, animated: true)
break
}
}
} else {
self.navigationController?.popViewController(animated: true)
}
}
}
} else {
if let information = response.information, let utf8Text = String(information: information, encoding: .utf8) {
print("Knowledge: (utf8Text)")
}
let information = response.information
let decoder = JSONDecoder()
do {
if let safeData = information {
let decodedData = strive decoder.decode(UploadData.self, from: safeData)
DispatchQueue.essential.async {
print("video id (index) = (decodedData.id)")
self.videoIds.append(decodedData.id)
self.uploadVideo(index: index + 1, previousProgress: self.totalProgress)
}
}
} catch {
print("add video Request Error = (String(describing: error))")
self.view.makeToast("SomethingWrongKey".localizableString(loc: MyVariables.selectedLang), length: 3.0, place: .backside, title: nil, picture: UIImage(named: "AppLogo"))
DispatchQueue.essential.asyncAfter(deadline: .now() + 4.0) {
self.delegate?.updateUIOnDismiss()
if self.didOpenWP {
self.delegate?.updateUIOnDismiss()
for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: PhotoVideoViewController.self) {
self.navigationController!.popToViewController(controller, animated: true)
break
}
}
} else {
self.navigationController?.popViewController(animated: true)
}
}
}
}
}
}
}
however this API is just not working and not using a VPN for some customers. If anybody will help me with this please inform…