Generate CRC32 checksum of file swift - ios

I have a .zip file inside the project and I am transferring it to the external ESP32 via Bluetooth. But I need to send the CRC-32 checksum at the end of the sending file. I don't have any idea how to generate it.
Can anyone help me with how to do it in the right way?
Thanks in advance.

So, I have found one amazing framework called CryptoSwift. This made my day. Here is how
Get file Data from the file path.
I have a function which returns Data from file name and type.
And CryptoSwift has the extension of Data which converts the crc32 by calling simply .crc32()
and than call crc32()
let crcChecksum = Utils.sharedInstance.getFileData(forResource: "firmware", withExtension: "bin").crc32()
For mote detail.
https://github.com/krzyzanowskim/CryptoSwift

Related

Saving stream of bytes to file in Swift on iOS

I'm using FileHandle to write stream of bytes to the mp4 file.
My data source allows me to input bytesOffset which is basically the current size of the file (if 0 it starts from the beginning, if more then it continue to save until reaches end).
I want to implement restart functionallity, but when write gets interrupted, the file exist, but size is always 0 KB.
Do you know any way to solve this or know any library which could help me with implementing this?
You can use Shamik framework for that.

Flutter can't load File() from path on iOS

Hey I am using flutter_sound package for voice recording.
But when I made a File() object from recorder file path, I have got not expected error on iOS when I am trying call length() or readBytes() functions..
on Android everything working fine, so I tried compare the made files on both platforms, and they are little bit different
ANDROID File path = /storage/emulated/0/...
iOS File path = file:///Users/kleinpetr/Library/Developer/......
I think the problem is in the different paths.
The error on iOS looks like this
Cannot retrieve length of file, path =
'file:///Users/kleinpetr/Library/Developer/CoreSimulator/Devices/62B6742D-0FA4-4B92-B138
0D9E717DB91B/data/Containers/Data/Application/8587EB31-CF5D-4045-950B
6C011AE3EA96/Library/Caches/voice-message.m4a' (OS Error: No such file or directory, errno = 2)
So using File.fromUri(Uri.parse(fullPath)) worked for you. What you're providing to File class isn't path actually, but URI string (it has protocol specified file:// in the beginning). What you have to do is to parse URI and create a File from it.

UIActivityViewController and UIDocument package

Summary:
Mail appears to flatten document packages when sending them.
Background:
I'm using subclassed UIDocument and FileWrappers to provide a document package for storing metadata, data and a thumbnail preview for my documents. I've made the necessary UTI incantations to get the document packages working locally.
I've added sharing support via UIActivityViewController. I'm sending the document using its fileURL. In testing with round-tripping my document package everything seems to be working well: export and import work great... almost.
Problem:
With Mail attachments, the document package arrives in a semi-flattened state (import no longer sees it as the document package). On examination of the document (on my Mac in TextWrangler) there is still directory structure and I can see the metadata, data, thumbnail but FileManager doesn't see it.
I've tried sharing the Mail attachment back to my app (via Copy to App) before the email is sent and that works fine.
Question:
Is Mail doing something unusual when moving the document package across a mail server?
I'm hoping to eventually support Open in Place and Files so I don't really want to get into wrapping shared documents in a Data blob etc. Does anyone know what's happening here and how to handle it while keeping my document a package?
Update:
I've confirmed that Apple Mail is zipping the package but not changing the extension. So the file received is a zip of the original. How can I make sure Apple Mail sees this as a package and doesn't zip? And, if I can't, how can I recognize the file being imported is a zip without the zip extension?
Answer:
Mail is indeed zipping the document package.
According to my research, we don't need to alter the document-package approach (i.e. not have a flattened special export type). On import, we test if it's a directory. If it's not a directory then we try unzipping it from its source url instead of just copying it. It should be noted that unzipping needs to be handled through a third-party library.
I wish there was a built-in API for handling this but this seems to be the way to deal with it.
(Inelegant) example code:
// Check if directory
if let _ = try? FileManager.default.contentsOfDirectory(atPath: url.path) {
// It is - try copying to the documents url
do {
try FileManager.default.copyItem(at: url, to: destination)
} catch {
self.delegate?.documentFileManagerDidFail(error: NSLocalizedString("Failed to copy file: \(error)", comment: "Document failure message"))
return
}
} else {
// It's not - try unzipping to the documents url
do {
try FileManager.default.unzipItem(at: url, to: destination)
} catch {
self.delegate?.documentFileManagerDidFail(error: NSLocalizedString("Failed to unzip file: \(error)", comment: "Document failure message"))
return
}
}

Play downloaded Widevine Classic files from app document folder in iOS?

I am trying to play wvm files from the app documents folder since 3 days but without success...
I removed "file://" from my path but I still get 1013 error (as discussed here), does someone have some sample code or at least the procedure to follow to make it works properly?
WV_RegisterAsset(myFilePath) always return WViOsApiStatus(rawValue: 1013)
The file should exists because when I try this:
let fileManager = NSFileManager()
print(fileManager.fileExistsAtPath(myFilePath))
It returns true
Thanks in advance for your help!
Widevine Classic on iOS uses an "asset root" directory. When you initialize the library, you pass it in the settings dictionary using the key WVAssetRootKey.
After that, all local asset pathnames you pass to the library are relative to the AssetRoot.
One simple option is to use NSHomeDirectory() as the AssetRoot, and then trim it from the beginning of absolute pathnames.

Steps to get the list of pdf files from FTP in IOS

I'm new to IOS Development. This is the first time i'm hearing about FTP in IOS .I'm not getting, How to proceed further . I got this code https://developer.apple.com/library/ios/samplecode/SimpleFTPSample/Introduction/Intro.html .
But i didn't understand from this code .
I need to list the pdf files and folders from FTP . First i don't know how to establish the connection to FTP . Can any one please help me with steps.
I fyou look in ListController.m, listOrCancelAction is called when you click on List button.
In turn if you put a break point in startReceive you would see that
CFReadStreamCreateWithFTPURL is opening a "Open a CFFTPStream for the URL" with a specified URL.
I suggest you put a couple more break points and step through the code?
NSStreamDelegate is being used as callbacks when something is happening in particular
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
is called when connection is open or data is received.
Hope this is enough to get you started.
entryToAdd = [self entryByReencodingNameInEntry:(__bridge NSDictionary *) thisEntry encoding:NSUTF8StringEncoding];
in parseListData will have file name in kCFFTPResourceName which of course you can parse to find out if it is a PDF file. You may look at kCFFTPResourceType to see if you can use to determine file type, not sure.

Resources