How to read GB2312 encoded text files using Swift 3.0 - ios

My app needs to read text files encoded in GB2312. Here is the current code.
SGFString = try String(contentsOf:path)
It throws an exception.
couldn’t be opened because the text encoding of its contents can’t be
determined.
While looking into the String.Encoding values, there is no GB2312. What would be the best way to read the GB2312 text files?
Thanks
Ray

Thanks for OOPer's help. I copied some of the code from the links, now it works.
Here is the code:
extension String.Encoding {
static let gb_18030_2000 = String.Encoding(rawValue: CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(CFStringEncodings.GB_18030_2000.rawValue)))
}
Gb2312String = try String(contentsOf:path, encoding:String.Encoding.gb_18030_2000)

Related

Swift - Create CSV file with text/csv MIME type

I'm using the following line in Swift to create a CSV file:
filePathToCSV.write(to: url, atomically: true, encoding: .utf8)
Unfortunately, the file has a MIME type of text/plain. How can I change the MIME type to text/csv?
I've searched high and low for a solution, but haven't found one yet.
Any help is much appreciated. Thank you!
Thank you everyone for your input!
I received some help from the Apple Developer Forums:
https://developers.apple.com/forums/thread/710757?page=1#721303022
Changing the line-break characters from \n to \r\n did the trick! The file command now shows the file's MIME type as text\csv.

How to replace these extended ascii codes?

I am opening up .txt files but when they are loaded on Xojo weird characters like these (’ , â€ک) show up.
I've tried DefineEncoding and ConvertEncoding but it still doesn't seem to work.
output.text = output.text.DefineEncoding(Encodings.WindowsANSI)
output.text = output.text.ConvertEncoding(Encodings.UTF8)
You may have to define the encoding already at time of loading, not afterwards, or you'll get UTF8 chara from loading that you will then mess up with your posted code. So, pass the encoding to the Read function or load the data as a binary file, not as a text file.

Is it possible to add a password to generated PDF in Swift?

I found how to add a password while you are writing the data into the disk,
pdfDocument.write(to: encryptedFileURL, withOptions: [PDFDocumentWriteOption.userPasswordOption : "pwd",
PDFDocumentWriteOption.ownerPasswordOption : "pwd"])
but in my case, I'm generating the PDF file differently. I'm generating it from data and directly in the MailComposer convert and send it like:
if let pdfData = printPageRenderer.drawPDFUsingPrintPageRenderer(printPageRenderer: printPageRenderer) {
mailComposer.addAttachmentData(pdfData as Data, mimeType: "application/pdf", fileName: "PDF")
}
But I cannot find a way how can I add a password to my PDF file if I'm not saving the file on my FileManager and I'm not using PDFKit.
Are there any options to do that? It would be great to know that. Any tips or help are appreciated!
Thanks in advance!
I assume you use UIGraphicsPDFRenderer in iOS to obtain the PDF data.
When initializing UIGraphicsPDFRenderer, you may have to give an UIGraphicsPDFRendererFormat.
In this UIGraphicsPDFRendererFormat, you can set format.documentInfo.
This documentInfo has
const CFStringRef kCGPDFContextUserPassword
const CFStringRef kCGPDFContextOwnerPassword;
Hopefully, you can get what you like.

swift - Unprintable ASCII character found in source file

This code:
var str = "ぴ" //Japanese: pi
xcode report compilation error:
Unprintable ASCII character found in source file
Have you tried this?
func convertString(string: String) -> String {
var data = string.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)
return NSString(data: data!, encoding: NSASCIIStringEncoding) as! String
}
From How to deal with a user input string that gives an "unprintable ascii character found in source file" error when pasted into Xcode?
This answer is for the people who are using windows keyboard with mac system.
While typing you might have unknowingly clicked the right-click key in your keyboard, which will create the Unprintable ASCII character that is causing the Unprintable ASCII character found in the source file error.
To check this go to find(cmd+F) and click the right-click key and you can find them in your class and delete those characters and the error will be gone.
Thanks
SWIFT 4:
func convertString(string: String) -> String {
let data = string.data(using: String.Encoding.ascii, allowLossyConversion: true)
return NSString(data: data!, encoding: String.Encoding.ascii.rawValue)! as String
}
In a Playground your line won't give you an error as it is, since it uses UTF-8 encoding by default. In a regular project your trusty File inspector will allow you to define the Text Encoding in the Text Settings section. I recommend to use some version of Unicode, probably UTF-8 if your app will deal primarily with western languages and UTF-16 if you have a lot of text constants in asian languages.
This just for the record since this is an old question and you probably found a working solution years ago.

How to get details from QR code?

I got a string from a qr generated image. But how can I get URL out of it. The string I got is the following.
aHR0cDovL2R1Yml6emxlLWludGVydmlldy5zMy5hbWF6b25hd3MuY29tLzg3M2FhMTA5LnR4dA==
Can anybody help me to get all the information out of it?
Thanks
That string is encoded in Base64. The decoded version of your string is:
http://dubizzle-interview.s3.amazonaws.com/873aa109.txt
If you need to integrate this into your software, find a library that has a Base64 decoder to decode such strings.

Resources