Upload Pdf, Docx and image file to server using Swift 4 - ios

I am new to swift, I have been trying to upload pdf, docx and image file from local storage of a Iphone. I have written a code but it doesn't work and I keep getting Status Code: 415 from the response. Here is my code:
func uploadfileToServer(completed: #escaping () -> ()){
let theTitle = labelTitle.text
guard let url = URL(string: "http://www.--------.com/assignment/post") else {return}
var request = URLRequest.init(url: url)
request.httpMethod = "POST"
request.addValue("cf7ab8c9d4efae82b575eabd6bec76cbb86c6108391e036387f3dd5356a582171519367747000", forHTTPHeaderField: "api_key")
let boundary = generateBoundaryString()
// Set Content-Type in HTTP header.
let boundaryConstant = boundary // This should be auto-generated.
let contentType = "multipart/form-data; boundary=" + boundaryConstant
let directory = NSTemporaryDirectory()
let fileName = NSUUID().uuidString
// This returns a URL? even though it is an NSURL class method
let fullURL = NSURL.fileURL(withPathComponents: [directory, fileName])
let fileNamee = fullURL?.path
let mimeType = "text/csv"
let fieldName = "uploadFile"
request.setValue(contentType, forHTTPHeaderField: "Content-Type")
var dataString = "--\(boundaryConstant)\r\n"
dataString += "\r\n"
dataString += "--\(boundaryConstant)--\r\n"
var theBody = Data()
let sectionID : String?
sectionID = nil
let str = "user_id=\(savedsesuid!)" + "&school_id=" + SCHOOL_ID + "&class_id=" + classID + "&section_id=\(sectionID)" + "&subject_id=\(id)"
if let b = str.data(using: .utf8) {
theBody.append(b)
}
let str1 = "&atitle=" + theTitle! + "&class_id=" + classID
if let c = str1.data(using: .utf8){
theBody.append(c)
}
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
var filePath = documentDirectory.appendingFormat("/")
filePath = filePath.appendingFormat("/Users/prashanna/Desktop/ebusiness_topic_1_handout.pdf")
let pdfData = NSData(contentsOfFile: filePath)
let file = "&afile=" + "\(pdfData)"
if let d = file.data(using: .utf8){
theBody.append(d)
}
print(theBody)
request.httpBody = theBody
URLSession.shared.dataTask(with: request) { (data, response, error) in
print(response)
if let httpResponse = response as? HTTPURLResponse {
let statuscode = httpResponse.statusCode
if statuscode == 401{
self.displayMessage(userMessage: "Sending Failed")
}else if statuscode == 200{
if error == nil{
do{
self.displayMessage(userMessage: "File Successfully Uploaded!")
DispatchQueue.main.async {
completed()
}
}
}
}
}
}.resume()
}
func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().uuidString)"
}
Some solutions tell me to convert the file into Data and then send it to server while some say to directly add the file path to your body.
Need Help!

One fundamental mistake is that you are using dataTask instead of an uploadTask on your URLSession instance, e.g. uploadTask(with:from:completionHandler:)
Construction of Body Data
Here's a generic example from my own code (as requested in the comments below) of how body data might be constructed:
// imagesURLS is an optional array of URLs, i.e. imageURLS:[URL]?
if let imgURLs = imagesURLS {
for f in imgURLs {
let filename = f.lastPathComponent
let splitName = filename.split(separator: ".")
let name = String(describing: splitName.first)
let filetype = String(describing: splitName.last)
let imgBoundary = "\r\n--\(boundary)\r\nContent-Type: image/\(filetype)\r\nContent-Disposition: form-data; filename=\(filename); name=\(name)\r\n\r\n"
if let d = imgBoundary.data(using: .utf8) {
bodyData.append(d)
}
do {
let imgData = try Data(contentsOf:f, options:[])
bodyData.append(imgData)
}
catch {
// can't load image data
}
}
}
let closingBoundary = "\r\n--\(boundary)--"
if let d = closingBoundary.data(using: .utf8) {
bodyData.append(d)
}
The loop means that every item of data (in this case an image) is preceded by a boundary string and after the very last item of data the closing boundary string is added (i.e. the one ending in a double hyphen).

This works for me, in Swift4:
func uploadFiles(_ urlPath: [URL]){
if let url = URL(string: "YourURL"){
var request = URLRequest(url: url)
let boundary:String = "Boundary-\(UUID().uuidString)"
request.httpMethod = "POST"
request.timeoutInterval = 10
request.allHTTPHeaderFields = ["Content-Type": "multipart/form-data; boundary=----\(boundary)"]
for path in urlPath{
do{
var data2: Data = Data()
var data: Data = Data()
data2 = try NSData.init(contentsOf: URL.init(fileURLWithPath: path.absoluteString, isDirectory: true)) as Data
/* Use this if you have to send a JSON too.
let dic:[String:Any] = [
"Key":Value,
"Key":Value
]
for (key,value) in dic{
data.append("------\(boundary)\r\n")
data.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
data.append("\(value)\r\n")
}
*/
data.append("------\(boundary)\r\n")
//Here you have to change the Content-Type
data.append("Content-Disposition: form-data; name=\"file\"; filename=\"YourFileName\"\r\n")
data.append("Content-Type: application/YourType\r\n\r\n")
data.append(data2)
data.append("\r\n")
data.append("------\(boundary)--")
request.httpBody = data
}catch let e{
//Your errors
}
DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated).sync {
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { (dataS, aResponse, error) in
if let erros = error{
//Your errors
}else{
do{
let responseObj = try JSONSerialization.jsonObject(with: dataS!, options: JSONSerialization.ReadingOptions(rawValue:0)) as! [String:Any]
}catch let e{
}
}
}).resume()
}
}
}
}
extension Data{
mutating func append(_ string: String, using encoding: String.Encoding = .utf8) {
if let data = string.data(using: encoding) {
append(data)
}
}
}

Sample Code for uploading image is:
func uploadImage(){
var imageToUpload:UIImage = UIImage()
let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
if let dirPath = paths.first
{
let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("Image2.png") //Your image name here
let image = UIImage(contentsOfFile: imageURL.path)
imageToUpload = image!
}
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(UIImageJPEGRepresentation(imageToUpload, 1)!, withName: "Prescription", fileName: "Profile_Image.jpeg", mimeType: "image/jpeg")
}, to:"you_URL_here")
{ (result) in
switch result {
case .success(let upload, _, _):
print(result)
upload.uploadProgress(closure: { (progress) in
print(progress)
})
upload.responseJSON { response in
//print response.result
print(response);
}
case .failure(let encodingError):
print(encodingError);
}
}
}

Related

Image upload with URLSession in Swift

Trying to create a simple script to upload an image with URLSession in Swift. In order to test the code I send a POST request to https://api.imagga.com/v2/uploads. I created an account to https://imagga.com in order to gain the credentials for uploading. The code for URLSession does not work.
It returns {"status":{"text":"No file for upload supplied, use the image or video keys.","type":"error"}}, with Status Code: 400.
The error with status code: 401 is for authorization issues, so I am doing something wrong with the format of the request.
When I use the Alamofire's implementation the image is uploaded successfully.
My credentials for imagga.com are stored as a struct:
import Foundation
struct ImaggaCredentials {
static let username = "myUsername"
static let password = "myPassword"
}
The code for URLSession upload task:
import SwiftUI
class URLSessionObserved: NSObject, ObservableObject {
#Published var uploadedAmount: Float = 0
lazy var urlSession: URLSession = {
let configuration = URLSessionConfiguration.default
return URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
}()
func upload() {
let urlString = "https://api.imagga.com/v2/uploads"
let url = URL(string: urlString)!
let fileURL = Bundle.main.url(forResource: "image", withExtension: "jpeg")
guard let fileURL = fileURL else {
return
}
let loginString = String(format: "%#:%#", ImaggaCredentials.username, ImaggaCredentials.password)
let loginData = loginString.data(using: String.Encoding.utf8)!
let base64LoginString = loginData.base64EncodedString()
let boundary = "Boundary-\(UUID().uuidString)"
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
let fileName = fileURL.lastPathComponent
let mimetype = "image/" + fileName.components(separatedBy: ".")[1]
let paramName = "file"
let fileData = try? Data(contentsOf: fileURL)
guard let fileData = fileData else {
return
}
var data = Data()
data.append("\r\n--\(boundary)\r\n".data(using: .utf8)!)
data.append("Content-Disposition: form-data; name=\"\(paramName)\"; filename=\"\(fileName)\"\r\n".data(using: .utf8)!)
data.append("Content-Type: \(mimetype)\r\n\r\n".data(using: .utf8)!)
data.append(fileData)
data.append("\r\n--\(boundary)--\r\n".data(using: .utf8)!)
request.setValue(String(data.count), forHTTPHeaderField: "Content-Length")
let task = urlSession.uploadTask(with: request,
from: data,
completionHandler: { data, response, error in
print(response)
if(error != nil){
print("1--\(error!.localizedDescription)")
}
guard let data = data else {
print("no response data")
return
}
if let responseString = String(data: data, encoding: .utf8) {
print("2--\(responseString)")
}
})
task.resume()
}
}
The code for Alamofire upload task that works fine:
import SwiftUI
import Alamofire
import UIKit
class AlamofireObserved: NSObject, ObservableObject {
#Published var uploadedAmount: Float = 0
func upload() {
print("Button pressed (AlamofireObserved)")
let urlString = "https://api.imagga.com/v2/uploads"
let fileURL = Bundle.main.url(forResource: "image", withExtension: "jpeg")
guard let fileURL = fileURL else {
return
}
let fileName = fileURL.lastPathComponent
let withName = fileName.components(separatedBy: ".")[0]
let mimeType = "image/" + fileName.components(separatedBy: ".")[1]
let fileData = try? Data(contentsOf: fileURL)
guard let fileData = fileData else {
return
}
AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(fileData, withName: withName, fileName: fileName, mimeType: mimeType)
}, to: urlString)
.authenticate(username: ImaggaCredentials.username, password: ImaggaCredentials.password)
.validate()
.uploadProgress { progress in
print(Float(progress.fractionCompleted))
self.uploadedAmount = Float(progress.fractionCompleted)
}
.responseDecodable(of: UploadImageResponse.self) { response in
switch response.result {
case .failure(let error):
print("Error uploading file: \(error)")
case .success(let uploadResponse):
let resultID = uploadResponse.result.uploadID
print("Content uploaded with ID: \(resultID)")
}
}
}
}
The response type of the request:
import Foundation
struct UploadImageResponse: Codable {
let result: UploadResponseResult
let status: UploadResponseStatus
struct UploadResponseResult: Codable {
let uploadID: String
enum CodingKeys: String, CodingKey {
case uploadID = "upload_id"
}
}
struct UploadResponseStatus: Codable {
let text: String
let type: String
}
}

Sending Http Post request returns 500 status code in IoS

I am trying to send Receipt data of NSData type(converted to string) and several other parameters of type as String in Http Post request.
func receiptValidation(productId:String)
{
let SUBSCRIPTION_SECRET = My_SecretKey
let defaults = UserDefaults.standard
let receiptPath = Bundle.main.appStoreReceiptURL?.path
if FileManager.default.fileExists(atPath: receiptPath!){
var receiptData:NSData?
do{
receiptData = try NSData(contentsOf: Bundle.main.appStoreReceiptURL!, options: NSData.ReadingOptions.alwaysMapped)
}
catch{
print("ERROR: " + error.localizedDescription)
}
let receiptString = receiptData?.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
// let base64encodedReceipt = receiptData?.base64EncodedString(options: NSData.Base64EncodingOptions.endLineWithCarriageReturn)
let requestDictionary = ["receipt-data":receiptString!,"password":SUBSCRIPTION_SECRET]
guard JSONSerialization.isValidJSONObject(requestDictionary) else { print("requestDictionary is not valid JSON"); return }
do {
let requestData = try JSONSerialization.data(withJSONObject: requestDictionary)
let requestDataString = NSString(data: requestData, encoding: String.Encoding.utf8.rawValue)
//https://<apiProxyServer>:<apiProxyServerPort>/api/validate-receipt-data
let URLForApplication:String = String(format:"%#/api/validate-receipt-data",opcodeDetails["apiProxyBaseUrl"]!) // this works but as noted above it's best to use your own trusted server
let url:URL! = URL.init(string: URLForApplication)
var request = URLRequest.init(url: url)
request.httpMethod = "POST"
let configure = URLSessionConfiguration.background(withIdentifier: Bundle.main.bundleIdentifier!)
var postString:[String:Any]=[
"receiptData": requestDataString
"deviceType":"IOS",
"subscriberId":encodeString(normalString: defaults.array(forKey: "userDetails")?.first as! String),
"password":encodeString(normalString: defaults.array(forKey: "userDetails")?.last as! String),
"productId":encodeString(normalString: productId),
"code":opcodeDetails["opCode"]!
]
do {
let receiptData = try JSONSerialization.data(withJSONObject: postString)
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.httpBody = receiptData
} catch let error {
print(error.localizedDescription)
}
let session = URLSession(configuration:configure,
delegate:applicationDelegate.application,
delegateQueue:OperationQueue.main)
session1 = session
let connection = session1?.dataTask(with: request)
connection?.resume()
} catch let error as NSError {
print("json serialization failed with error: \(error)")
}
}
}
Where encodeString() is a defined method
func encodeString(normalString:String) -> String {
let allowedCharacters = CharacterSet.letters
let encodedString:String!=normalString.addingPercentEncoding(withAllowedCharacters: allowedCharacters)
return encodedString
}
There were no problem on server side.But I could not get success response instead What I am getting is {"status":"failure","statusCode":500,"message":"Resource url not found!"}.What mistake am i doing with this code?.Anyone please help me.Thanks in advance.
Try appending '/' at the end of URL.
Eg:
google.com/api/post and google.com/api/post/ are different and trailing slash sometimes gives 500 error, in case of Django-Backend it does.

How to append Special character in iOS using swift?

I am new to iOS please consider.I want to post some data to server, but I am not able to send Mobile number +91 and bloodGroup A+..it was sending to firebase + is replace with space " " like this ( 91) and (A )
func addEmployees(){
let photoUrl = "https://firebasestorage.googleapis.com/v0/b/pickcel-1241.appspot.com/o/task.careGallery%2FGroup%2018aasa.png?alt=media&token=4e0ac8f6-134a-4807-9fef-f44eabe9f6a8";
let userID = Auth.auth().currentUser!.uid
let mobilenumber = Auth.auth().currentUser?.phoneNumber
var employeeDetails = [String : AnyObject]()
employeeDetails["OID"] = getOID() as AnyObject
employeeDetails["MID"] = userID as AnyObject
employeeDetails["email"] = "ssinth#gmail.com" as AnyObject
employeeDetails["firstName"] = "First Name" as AnyObject
employeeDetails["lastName"] = "last name" as AnyObject
employeeDetails["isManager"] = "true" as AnyObject
employeeDetails["regMedia"] = "mobile" as AnyObject
employeeDetails["shortDestination"] = "btm" as AnyObject
employeeDetails["address"] = "+btm" as AnyObject
employeeDetails["createdDate"] = getdateformat() as AnyObject
employeeDetails["orgName"] = "Test Org" as AnyObject
employeeDetails["photoUrl"] = photoUrl as AnyObject
employeeDetails["officeOpenTime"] = "09:00" as AnyObject
employeeDetails["officeCloseTime"] = "18:00" as AnyObject
employeeDetails["phoneNumber"] = labelmobile.text as AnyObject
employeeDetails["bloodGroup"] = "A+" as AnyObject
employeeDetails["empId"] = "abcd" as AnyObject
let convertedvalue : String = convertToParameters(employeeDetails)
print("convertedvalues : \(convertedvalue)")
let myUrl = URL(string: "https://us-central1-pickceltest.cloudfunctions.net/rebliss/createNewUser");
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"// Compose a query string
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let postString = convertedvalue;
print("start create employee")
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil
{
print("error=\(String(describing: error))")
return
}
print("start create employee =successfull")
print("response = \(String(describing: response))")
do {
print("start create employee =parsing problems ")
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
// print("resultjson=one : ",self.json)
print("resultjson=two : ",parseJSON)
}
} catch {
print(error)
}
}
task.resume()
}
function convertToParameters
func convertToParameters(_ params: [String: AnyObject?]) -> String {
var paramList: [String] = []
for (key, value) in params {
guard let value = value else {
continue
}
let scapedKey = key
let scapedValue = value
print("employee add status objects = ","\(scapedKey)=\(scapedValue as AnyObject)")
paramList.append("\(scapedKey)=\(scapedValue as AnyObject)")
}
return paramList.joined(separator: "&")
}
Json Error:
resultjson=two : {
error = {
code = "auth/invalid-phone-number";
message = "The phone number must be a non-empty E.164 standard compliant identifier string.";
};
success = 0;
}
Error in firebase console:
Actual issue with you is your parameters not getting properly url encoded.
Temporary Solution for your code:
In your convertToParams method, make following changes:
let scapedKeyEncoded = scapedKey.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed);
let scapedValueEncoded = scapedValue.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed);
paramList.append("\(scapedKeyEncoded)=\(scapedValueEncoded)"
Note: Your scapedValue must be String, so, make employeeDetails as [String:String].
Permanent Solution(Perfect way for encoding query parameters):
For this, you can change your code to something like this:
Remove convertToParameters method
Add following method in place of it
class func getURLRequestWith(urlStr:String, paramsDict:[String:String]?, isParamsAsQuery:Bool, isParamsAsBody:Bool) -> URLRequest? {
guard var components = URLComponents(string: urlStr) else { return nil }
if paramsDict != nil{
components.queryItems = paramsDict!.map({ (key, value) -> URLQueryItem in
return URLQueryItem(name: key, value: value)
})
}
if isParamsAsQuery{
let request = URLRequest(url: components.url!);
return request;
}
if isParamsAsBody{
let url = URL(string: urlStr);
var request = URLRequest(url: url!);
let bodyStr = components.percentEncodedQuery;
if bodyStr != nil{
request.httpBody = bodyStr!.data(using: String.Encoding.utf8);
}
return request;
}
let url = URL(string: urlStr);
let request = URLRequest(url: url!);
return request;
}
Remove below lines from your code:
let convertedvalue : String = convertToParameters(employeeDetails)
print("convertedvalues : \(convertedvalue)")
let myUrl = URL(string: "https://us-central1-pickceltest.cloudfunctions.net/rebliss/createNewUser");
var request = URLRequest(url:myUrl!)
request.httpBody = postString.data(using: String.Encoding.utf8);
Add following code instead of above removed code
let urlStr = "https://us-central1-pickceltest.cloudfunctions.net/rebliss/createNewUser"
var request = getURLRequestWith(urlStr: urlStr, paramsDict: employeeDetails, isParamsAsQuery: false, isParamsAsBody: true)
convert employeeDetails dictionary to type [String:String] instead of [String:AnyObject]
Now, try your code and it will surely work.
Please check if below solution works. Add percent encoding to you url.
// Create NSURL Ibject
let url : NSString = urlWithParams as NSString
let urlStr = url.addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)
let searchURL : NSURL = NSURL(string: urlStr! as String)!
// Creaste URL Request
let request = NSMutableURLRequest(url: searchURL as URL, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 120)
// Set request HTTP method to GET. It could be POST as well
request.httpMethod = "POST"

Converting iPhone video to mp4 and upload to PHP Server

I have an app that successfully records and uploads a file to my PHP server, unfortunately the file won't play. I received guidance that I needed to use AVAssetExportSession to convert the file to mp4 to get it to work but am having trouble incorporating this into my code correctly. I'm getting an error
Error Domain=AVFoundationErrorDomain Code=-11823 "Cannot Save"
UserInfo={NSLocalizedRecoverySuggestion=Try saving again.,
NSLocalizedDescription=Cannot Save, NSUnderlyingError=0x1d465ea50
{Error Domain=NSOSStatusErrorDomain Code=-12101 "(null)"}}
Here is my code:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("Got a video")
if let pickedVideo:URL = (info[UIImagePickerControllerMediaURL] as? URL) {
// Save video to the main photo album
let selectorToCall = #selector(CameraVideoViewController.videoWasSavedSuccessfully(_:didFinishSavingWithError:context:))
UISaveVideoAtPathToSavedPhotosAlbum(pickedVideo.relativePath, self, selectorToCall, nil)
imageSelected = true
uuid = UUID().uuidString
if imageSelected == true {
saveFileName = "video-\(uuid).mp4"
}
// Save the video to the app directory so we can play it later
let videoData = try? Data(contentsOf: pickedVideo)
let paths = NSSearchPathForDirectoriesInDomains(
FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let documentsDirectory: URL = URL(fileURLWithPath: paths[0])
let dataPath = documentsDirectory.appendingPathComponent(saveFileName)
try! videoData?.write(to: dataPath, options: [])
print("Saved to " + dataPath.absoluteString)
imagePicker.dismiss(animated: true, completion: {
// Anything you want to happen when the user saves an video
self.encodeVideo(dataPath: dataPath)
self.uploadVideo(videoData!)
})
} }
// custom body of HTTP request to upload image file
func createBodyWithParams(_ parameters: [String: String]?, filePathKey: String?, videoData: Data, boundary: String) -> Data {
let body = NSMutableData();
if parameters != nil {
for (key, value) in parameters! {
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString("\(value)\r\n")
}
}
// if file is not selected, it will not upload a file to server, because we did not declare a name file
var filename = ""
if imageSelected == true {
filename = "video-\(uuid).mp4"
}
let mimetype = "video/mp4"
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n")
body.appendString("Content-Type: \(mimetype)\r\n\r\n")
body.append(videoData)
body.appendString("\r\n")
body.appendString("--\(boundary)--\r\n")
return body as Data
}
// File Conversion
func encodeVideo(dataPath: URL){
let avAsset = AVURLAsset(url: dataPath)
let startDate = Date()
let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough)
let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let myDocPath = NSURL(fileURLWithPath: docDir).appendingPathComponent("temp.mp4")?.absoluteString
let docDir2 = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL
let filePath = docDir2.appendingPathComponent("rendered-Video.mp4")
//uploadVideo(filePath)
//deleteFile(filePath!)
if FileManager.default.fileExists(atPath: myDocPath!){
do{
try FileManager.default.removeItem(atPath: myDocPath!)
}catch let error{
print(error)
}
}
//self.uploadVideo((myDocPath as AnyObject) as! URL)
exportSession?.outputURL = filePath
exportSession?.outputFileType = AVFileType.mp4
exportSession?.shouldOptimizeForNetworkUse = true
let start = CMTimeMakeWithSeconds(0.0, 0)
let range = CMTimeRange(start: start, duration: avAsset.duration)
exportSession?.timeRange = range
exportSession!.exportAsynchronously{() -> Void in
switch exportSession!.status{
case .failed:
print("\(exportSession!.error!)")
case .cancelled:
print("Export cancelled")
case .completed:
let endDate = Date()
let time = endDate.timeIntervalSince(startDate)
print(time)
print("Successful")
print(exportSession?.outputURL ?? "")
default:
break
}
}
}
// File Upload
func uploadVideo(_ videoData: Data) {
func createBodyWithParams(_ parameters: [String: String]?, filePathKey: String?, videoData: Data, boundary: String) -> Data {
let body = NSMutableData();
if parameters != nil {
for (key, value) in parameters! {
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString("\(value)\r\n")
}
}
var filename = ""
if imageSelected == true {
filename = "video-\(uuid).mp4"
}
let mimetype = "video/mp4"
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.appendString("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n")
body.appendString(String(describing: "Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)))
body.append(videoData)
body.append(String(format: "\r\n").data(using: String.Encoding.utf8)!)
body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
return body as Data
}
let id = user!["id"] as! String
uuid = UUID().uuidString
let url = URL(string: "http://www.foo.com/videoposts.php")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
let param = [
"id" : id,
"uuid" : uuid
]
// body
let boundary = "Boundary-\(UUID().uuidString)"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
// if picture is selected, compress it by half
let imageData = Data()
// ... body
request.httpBody = createBodyWithParams(param, filePathKey: "file", videoData: imageData, boundary: boundary)
// launch session
URLSession.shared.dataTask(with: request) { data, response, error in
// get main queu to communicate back to user
DispatchQueue.main.async(execute: {
if error == nil {
do {
// json containes $returnArray from php
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
// declare new var to store json inf
guard let parseJSON = json else {
print("Error while parsing")
return
}
// get message from $returnArray["message"]
let message = parseJSON["message"]
// if there is some message - post is made
if message != nil {
// reset UI
self.postBtn.alpha = 0.4
self.imageSelected = false
// switch to another scene
self.tabBarController?.selectedIndex = 4
}
} catch {
// get main queue to communicate back to user
DispatchQueue.main.async(execute: {
let message = "\(error)"
appDelegate.infoView(message: message, color: colorSmoothRed)
})
return
}
} else {
// get main queue to communicate back to user
DispatchQueue.main.async(execute: {
let message = error!.localizedDescription
appDelegate.infoView(message: message, color: colorSmoothRed)
})
return
}
})
}.resume()
}
Try to check if there is already some file exists in the directory that you are trying to save at. You can add the following code.
do { // delete old video
try FileManager.default.removeItem(at: savePathUrl)
} catch { print(error.localizedDescription) }

swift Unescaped control character

I'm trying to get a json from a server and deserialize it, but I try probelas with unescaped control characters.
My code is as follows ...
let urlFinal = "http://000.0000.000.000:8080"
let jsonUrl = urlFinal
let session = NSURLSession.sharedSession()
let shotsUrl = NSURL(string: jsonUrl)
let task = session.dataTaskWithURL(shotsUrl!) {data, response, error in
guard data != nil else {
falha()
return
}
//let json = JSON(data: data!)
//print(json["ServicoCliente"][0]["id"])
do {
let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers )
let J = jsonData as! NSDictionary
print(jsonData)
let us = J["ServicoCliente"]
print(us)
dispatch_async(dispatch_get_main_queue(),{
sucesso()
});
} catch _ {
falha()
}
}
task.resume()
and I also tried this using Alamofire 3.0:
Alamofire.request(.GET, "http://000.000.000.000/", parameters: nil)
.responseJSON { response in
debugPrint(response) // prints detailed description of all response properties
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
And get this error:
Unescaped control character around character 263
How can I remove characters without escape?
I use X-Code 7.3.1 and Swift 2.3
UPDATE:
Json
{"ServicoCliente":[{"id":"195","cliente":"247","enderecoFavoritos":"48","servicoProfissional":"194","ind_estado_cliente":"A","ind_estado_profissional":"","profissional_id":"240","profissional_nome":"PetMax","servicotipo_nome":"Petshop","servicosubtipo_nome":"Tosa ","dta_inc_alt":"2016-11-05 22:56:19.333","ind_finalizado":"N"}]}
To fix this, you must convert the data for string, remove characters and then convert to dataonly then deserialize
let urlFinal = "http://000.0000.000.000:8080"
let jsonUrl = urlFinal
let session = NSURLSession.sharedSession()
let shotsUrl = NSURL(string: jsonUrl)
let task = session.dataTaskWithURL(shotsUrl!) {data, response, error in
guard data != nil else {
falha()
return
}
var dataToString = String(data: data!, encoding: NSUTF8StringEncoding)
dataToString = stringByRemovingControlCharacters2(dataToString!)
let ndata = dataToString!.dataUsingEncoding(NSUTF8StringEncoding)
do {
let jsonData = try NSJSONSerialization.JSONObjectWithData(ndata!, options: NSJSONReadingOptions.MutableContainers )
let J = jsonData as! NSDictionary
print(jsonData)
let us = J["ServicoCliente"]
print(us)
dispatch_async(dispatch_get_main_queue(),{
sucesso()
});
} catch _ {
falha()
}
}
task.resume()
and add the function
func stringByRemovingControlCharacters2(string: String) -> String {
let controlChars = NSCharacterSet.controlCharacterSet()
var range = string.rangeOfCharacterFromSet(controlChars)
var mutable = string
while let removeRange = range {
mutable.removeRange(removeRange)
range = mutable.rangeOfCharacterFromSet(controlChars)
}
return mutable
}
Swift 5
func string(byRemovingControlCharacters inputString: String) -> String {
let controlChars = CharacterSet.controlCharacters
var range = (inputString as NSString).rangeOfCharacter(from: controlChars)
if range.location != NSNotFound {
var mutable = inputString
while range.location != NSNotFound {
if let subRange = Range<String.Index>(range, in: mutable) { mutable.removeSubrange(subRange) }
range = (mutable as NSString).rangeOfCharacter(from: controlChars)
}
return mutable
}
return inputString
}

Resources