How to send a txt file from Apple Watch app to Mac? - swift

My app has bugs that happen randomly and only happen on real device, so I made a func to log important things as a txt file on Apple Watch, I planned to email the log file to myself when needed, but just found out the watchOS doesn't support the MessageUI framework.
Is there any way to send my log file to Mac from Apple Watch? Or What's the best way to read the log file?
Thanks.
This is code for logging, in case it's needed:
let logFileName: String = "log.txt"
func myLog(dataToWrite: String) {
do {
let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! as URL
let url = dir.appendingPathComponent(logFileName)
try "Test \(Date())".appendLineToURL(fileURL: url as URL)
let result = try String(contentsOf: url as URL, encoding: String.Encoding.utf8)
}
catch {
print("Could not write to file")
}
}

Related

Swift Create file with integrity FileManager

I'm currently trying to store some files on my iOS device. The contents of the file are encrypted, but I was wondering if I can append some kind of integrity check to the file as well, preferably using the FileAttributeKey.
I tried the following, which doesn't work
extension FileAttributeKey {
static let integrity = FileAttributeKey("NSFileIntegrity")
}
let docs = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileName = "test"
let filePath = docs.appendingPathComponent(fileName).path
defer {
try! FileManager.default.removeItem(atPath: filePath)
}
let data = Data("Hello world".utf8)
// This line fails too
// FileManager.default.createFile(atPath: filePath, contents: data, attributes: [.integrity: "SHA256"])
FileManager.default.createFile(atPath: filePath, contents: data, attributes: [:])
do {
try FileManager.default.setAttributes([.integrity: "SHA256"], ofItemAtPath: filePath)
} catch {
print(error)
}
print(try FileManager.default.attributesOfItem(atPath: filePath))
So the questions are:
Is there a way to create and append a custom FileAttributeKey to a file.
Is there a (better) way to add integrity checks to a file?
If you use authenticated encryption then you get integrity checks for free. Every time you decrypt, integrity will be checked for you and in case of errors the decryption will fail. Just use a mode like GCM or OCB and you are done.

Can you perform file I/O in a REPL on Repl.it when using Swift?

There's an online site here called Repl.it that gives you an in-browser REPL environment for a ton of languages. It's great to prove out code that you post here on SO. (I think you can even include it here actually but I wasn't successful in embedding mine.)
Anyway, when using Swift, I'm wondering if it's possible to perform file read/write persistence up there. I haven't found any articles that say yes, but I have found some that show them talking about how much storage you have, and it is supposed to be the full Swift runtime with all features, so I'm not sure.
This code fails however, saying it can't be performed.
import Foundation
let file = "file.txt" //this is the file. we will write to and read from it
let text = "some text" //just a text
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let fileURL = dir.appendingPathComponent(file)
//writing
do {
try text.write(to: fileURL, atomically: false, encoding: .utf8)
}
catch {
print(error)
}
//reading
do {
let text2 = try String(contentsOf: fileURL, encoding: .utf8)
print("Read back in '\(text2)'")
}
catch {/* error handling here */}
}
else{
print("Couldn't get document directory")
}
You can open it here... Swift File Persistence REPL
I admit I'm 90% sure this isn't the right place for this, but since Repl.it does let you play with and execute Swift and this is a question about what Swift is needed to accomplish this, I figured I'd try!

FileManager and accessing app group error

I've been having problem with using share extension to get a file and unzip it into my app group folder.
Here is the code:
for attachment in content.attachments as! [NSItemProvider] {
if attachment.hasItemConformingToTypeIdentifier(contentType){
attachment.loadItem(forTypeIdentifier: contentType, options: nil) { (data, error) in
let fileManager = FileManager()
let url = data as! URL
let destinationURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.KakaoAnalyzer")?.appendingPathComponent("KakaoFilesTemp")
do {
try fileManager.createDirectory(at: destinationURL!, withIntermediateDirectories: true, attributes: nil)
try fileManager.unzipItem(at: url, to: destinationURL!)
self.mergeFiles()
print("SUCCESSFULLY UNZIPPED")
} catch {
print(error.localizedDescription)
print("UNZIP FAILED")
}
}
}
}
This is part of my didSelectPost()
I'm using share extension to get a ZIP file via share, and ZIPFoundation to unzip it into my destination folder which is my app group with this extension and my main project.
This code runs fine and unzips successfully in a simulator, but when I run it on my phone, I get an error:
The file "FILENAME" couldn't be opened because there is no such file
when there clearly should be one, and
Failed to determine whether URL "FILEURL" is managed by a file provider.
I've tried moving the file at the url to my documentDirectory, but it produces error:
"FILE" couldn't be moved because you don't have permission to access "appGroupFolder"
Does anyone how to solve this? Any help would be greatly appreciated!

Sending CSV file with SFTP in swift

I have a server hosted with webfaction that I would like to be able to send a csv file to from my app with FTP or SFTP. I have found many libraries that should help like ConnectionKit, NMSSH, DLSFPT, and LxFTPRequest. However, all of them are in objective-c and not swift which makes them hard to read, understand, and implement in Swift 4. I have tried to implement LXFTPRequest since I found a swift implementation for the upload and here is my code:
let fileName = "user-data.csv"
guard let path = FileManager.default.urls(for: .documentDirectory, in:.userDomainMask).first else {fatalError(ErrorMessageStrings.couldntAccessDocs.rawValue)}
let fileURL = path.appendingPathComponent(fileName)
let folderLocation = "/home/path/"
let uploadUrl = URL(string: "ftp://server-name.webfaction.com" + folderLocation)
let request = LxFTPRequest.upload()
request?.serverURL = uploadUrl
request?.localFileURL = fileURL
request?.username = "username"
request?.password = "password"
request?.successAction = { (resultClass, result) in
print("File uploaded")
}
request?.failAction = { (domain, error, errorMessage) in
print(error)
print(errorMessage?.description)
fatalError("Connection could not be made. Action was not completed.")
}
request?.progressAction = {(_ totalSize: Int, _ finishedSize: Int, _ finishedPercent: CGFloat) -> Void in
print(finishedPercent)
}
request?.start()`
Using this I almost get it to work but I end up with a 550 error "Requested action not taken. File unavailable (e.g., file not found, no access)." Looking through webfaction documentation I get the feeling that I can only send files through SFTP, which this framework doesnt support.
The doc says "To connect with FTP (for shell users only), substitute the connection type with FTP and the port number with 21." I am assuming since I am sending data from my app it does not count as a shell user and so FTP doesn't grant me access (I may be wrong here). If that is the case how would I go about using the other libraries to send my file over SFTP using Swift and not objective-c?
I ended up using NMSSH and using it in Swift it wasn't as complicated as I thought.
let session = NMSSHSession.init(host: serverHost, port: xx, andUsername: serverUsername)
session.connect()
if session.isConnected{
session.authenticate(byPassword: serverPasswordString)
if session.isAuthorized == true {
let sftpsession = NMSFTP(session: session)
sftpsession.connect()
if sftpsession.isConnected {
sftpsession.writeFile(atPath: csvFileURL.path, toFileAtPath: folderLocation)
}
}
}

How to Use SwiftyDropbox's "destination" with a Download

In reviewing the SwiftyDropbox tutorial in the v2 Dropbox API, it shows how to perform a download:
// Download a file
let destination : (NSURL, NSHTTPURLResponse) -> NSURL = { temporaryURL, response in
let fileManager = NSFileManager.defaultManager()
let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
// generate a unique name for this file in case we've seen it before
let UUID = NSUUID().UUIDString
let pathComponent = "\(UUID)-\(response.suggestedFilename!)"
return directoryURL.URLByAppendingPathComponent(pathComponent)
}
client.files.download(path: "/MyFile.db", destination: destination).response { response, error in
if let (metadata, url) = response {
print("*** Download file ***")
let data = NSData(contentsOfURL: url)
print("Downloaded file name: \(metadata.name)")
print("Downloaded file url: \(url)")
print("Downloaded file data: \(data)")
} else {
print(error!)
}
}
I'm unclear what's going on with the destination part. Why do I need to generate a random string for the filename?
When I try to specify my own filename, the download doesn't seem to work:
let destination : (NSURL, NSHTTPURLResponse) -> NSURL = { temporaryURL, response in
let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
return directoryURL.URLByAppendingPathComponent("MyFile.db")
}
I want to download a file from Dropbox named MyFile.db and I want to put it in my device's documents directory with the name MyFile.db and overwrite it if it's already there.
How can I do that?
When you say it doesn't seem to work, I expect you mean you get an error like this:
Error Domain=NSCocoaErrorDomain Code=516 "“CFNetworkDownload_bPYhu1.tmp” couldn’t be moved to “Documents” because an item with the same name already exists." UserInfo={NSSourceFilePathErrorKey=..., NSUserStringVariant=(
Move
), NSDestinationFilePath=..., NSUnderlyingError=0x7fda0a67cea0 {Error Domain=NSPOSIXErrorDomain Code=17 "File exists"}}
SwiftyDropbox, by virtue of using AlamoFire, doesn't currently let you overwrite files using the download function.
Specifically, SwiftyDropbox calls download in AlamoFire, and AlamoFire calls NSFileManager.moveItemAtURL. The documentation for NSFileManager.moveItemAtURL says:
If an item with the same name already exists at dstURL, this method aborts the move attempt and returns an appropriate error.
So, it seems like it's just being cautious, and making it hard for your app to accidentally overwrite (ad potentially lose) data. If you definitely know you want to overwrite a particular file, you'll need to do so explicitly, after the Dropbox API call. We'll consider this a feature request though.
Update: SwiftyDropbox now offers the ability to overwrite the files directly as of version 3.1.0, e.g., using download(path:rev:overwrite:destination:).