Converting Base64 data to .csv file

Hi,


I am facing issue in converting base64 data to .csv file. I am getting full .csv converted data from sever. I tried to decode it and save it as String and passed that string to UIActivityController and download it in iPhone using Save to Files, because I didn't find any other way to save it in my Files folder. But the file which is getting saved is in .txt file.


I would be very grateful, if anyone would help me regarding this issue, kindly let me know.


1. First how to download file in .csv instead of .txt.

2. Is there any other way to Save To Files in iPhone, except UIActivityController.

Answered by OOPer in 421620022

The requirement is that user can locate there .csv file in Files App, right?


Then, have you considered adopting File Sharing?

www.bignerdranch.com/blog/working-with-the-files-app-in-ios-11/

1. First how to download file in .csv instead of .txt.

Generally, downloaded file is just a binary data and you just need to save it with file extension `.csv`.



2. Is there any other way to Save To Files in iPhone, except UIActivityController.

I'm very curious why you have chosen UIActivityController to save a file.

You can save any sort of files into your app's data directory, using usual file operation.


If you need some concrete example, please show your current code.

To save a file:


        let fileURL = // the URL you want to save to
       
        do {
            let data = // The data to save, may be a text
            try data.write(to: fileURL)
        } catch {
            print(error)
            // You may need some better error handling...
        }

Hello,


Earlier I tried to save .csv file using

func getDownloadsDirectory() -> NSString {

let paths = NSSearchPathForDirectoriesInDomains(.downloadsDirectory, .userDomainMask, true)

let documentsDirectory = paths[0]

return documentsDirectory as NSString

}

let filePath = BaseViewController.getDownloadssDirectory().appendingPathComponent("WalletTransactions.csv")

decodeData.write(toFile: filePath, atomically: true)

let str = String(decoding: decodeData, as: UTF8.self)

let vc = UIActivityViewController(activityItems: [str], applicationActivities: [])

vc.excludedActivityTypes = [

UIActivity.ActivityType.assignToContact,

UIActivity.ActivityType.saveToCameraRoll,

UIActivity.ActivityType.postToFlickr,

UIActivity.ActivityType.postToVimeo,

UIActivity.ActivityType.postToTencentWeibo,

UIActivity.ActivityType.postToTwitter,

UIActivity.ActivityType.postToFacebook,

UIActivity.ActivityType.openInIBooks

]

self.present(vc, animated: true, completion: nil)


But I want to download file in user's iphone, so that user can locate there .csv file in Files App of iPhone.

Using above code file was not saved in Files App of iPhone.


So, can you please look into it.


Also for my first question How can I change the extension of file.

Using:

let str = String(decoding: decodeData, as: UTF8.self)

let vc = UIActivityViewController(activityItems: [str], applicationActivities: [])


File is getting downloaded in .txt file.

Is there any way to remove this str before opening UiActivityController.

Accepted Answer

The requirement is that user can locate there .csv file in Files App, right?


Then, have you considered adopting File Sharing?

www.bignerdranch.com/blog/working-with-the-files-app-in-ios-11/

Thank you so much.

This has worked for me..

Happy to hear it worked.


In case you need to share a .csv file using UIActivityViewController in the future, you should better know that you need to pass a file URL instead of the content String.

And I recommend to use URLs as far as possilble, not file path.

        do {
            let downloadUrl = try FileManager.default.url(for: .downloadsDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
            let fileUrl = downloadUrl.appendingPathComponent("WalletTransactions.csv")
            try decodeData.write(to: fileUrl, options: .atomic)
            
            let vc = UIActivityViewController(activityItems: [fileUrl], applicationActivities: [])
            
            vc.excludedActivityTypes = [
                UIActivity.ActivityType.assignToContact,
                UIActivity.ActivityType.saveToCameraRoll,
                UIActivity.ActivityType.postToFlickr,
                UIActivity.ActivityType.postToVimeo,
                UIActivity.ActivityType.postToTencentWeibo,
                UIActivity.ActivityType.postToTwitter,
                UIActivity.ActivityType.postToFacebook,
                UIActivity.ActivityType.openInIBooks
            ]
            
            self.present(vc, animated: true, completion: nil)
            
        } catch {
                print(error)
        }
Converting Base64 data to .csv file
 
 
Q