plist attachment from email in swift (NS Cocoa Error Domain Code 260) - ios

I am sending some ios file data via an email contained in a plist. No issues attaching it, I can check the file and the contents. It's all there and I can open it as a plist. Clicking on it (I have associated it with my app) it opens and I get a valid path:
file:///private/var/mobile/Containers/Data/Application/C5454580-2BEB-4515-9BDE-FED85FF54F76/Documents/Inbox/ShareStrength-11.bps
that I pass but when I try and read back the NSDictionary (plist) I get nil content.
let sourceFile = NSDictionary(contentsOfFile: URLString)
Any ideas on what is going wrong. Having difficulty debugging.
EDIT: I found some error code:
var error: NSError?
let content = NSString(contentsOfFile: URLString, encoding:NSUTF8StringEncoding, error: &error)
if content != nil
{
println("content: \(content)")
}
else
{
println("error: \(error)")
}
And I get the error:
error: Optional(Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x17eceeb0 {NSFilePath=file:///private/var/mobile/Containers/Data/Application/571DB0FF-6C5A-4BEB-9FA8-6E4DFE10E850/Documents/Inbox/ShareStrength-13.bps, NSUnderlyingError=0x17ee9810 "The operation couldn’t be completed. No such file or directory"})
Is this a sandbox issue? How can I copy the attachment being passed as a URL?

I was using:
let URLString: String = url.absoluteString!
instead of:
let URLString: String = url.path!
Hope this helps someone else

Related

"The file “command.cgi” couldn’t be opened."

FlashAir W-04 Fourth Generation SD memory card
In my iOS app directory listing api not working.
Response :
Task .<0> HTTP load failed (error code: -1003 [12:8])
2019-05-21 23:32:40.267572+0530 Razzo[10489:87069] NSURLConnection finished with error - code -1003
Error Domain=NSCocoaErrorDomain Code=256 "The file “command.cgi” couldn’t be opened." UserInfo={NSURL=http://flashair/command.cgi?op=100&DIR=/DCIM}
Please help me what was wrong
below code snippet
private func getdata() {
let url100 = URL(string: "http://flashair/command.cgi?op=100&DIR=/DCIM")
var dirStr: String? = nil
do {
if let url100 = url100 {
dirStr = try String(contentsOf: url100, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
if let dir = dirStr {
arrayfiles = dir.components(separatedBy: "\n")
}
tblContent.reloadData()
}
} catch {
print(error)
self.displayAlert(message: error.localizedDescription)
}
}
Config file below
[Vendor]
CIPATH=/DCIM/100__TSB/FA000001.JPG
APPMODE=4
APPNETWORKKEY=12345678
VERSION=F15DBW3BW4.00.03
CID=02544d535733324755e3c6dc7b012301
PRODUCT=FlashAir
VENDOR=TOSHIBA
MASTERCODE=f437b71e0e4f
LOCK=1
contentsOf: url100 would be for a local file URL. You are not providing a valid file URL.
If your file is remote you need to download the file as data with URLSession. If it is local you need to work out its file URL.

What is solution for "The file couldn’t be opened." UserInfo={NSURL=ftp:/xxxx:xxx#2011#ftp.xxx.com:21/}?

I am trying to call FTP server api and want to get file name when api is success.
Below code I tried for call api,
let host = "ftp.xxx.com"
let user = "xxx"
let password = "xxx#2011"
let port = "21"
let url = URL(string: "ftp://"+user+":"+password+"#"+host+":"+port+"/")
var data: Data? = nil
do {
if let anUrl = url {
data = try Data(contentsOf: anUrl)
print(data!)
}
} catch {
print("Unexpected error: \(error).")
}
When run this code I am getting error like
Error Domain=NSCocoaErrorDomain Code=256 "The file couldn’t be opened." UserInfo={NSURL=ftp:/xxxx:xxx#2011#ftp.xxx.com:21/}.
Please give me any solution to solve this.
The problem is the plain # inside the password. This means ftp://xxxx:xxx#2011#ftp.xxx.com:21/ gets interpreted as connecting to server 2011#ftp.xxx.com with username xxxx and password xxx, which is obviously wrong.
The solution should be to URL-encode the special character # to %40, i.e. use the URL
ftp://xxx:xxx%402011#ftp.xxx.com:21/

Don't have permission to access video file path in swift

Getting this error when uploading file to server
Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_0011.MOV” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/var/mobile/Media/DCIM/100APPLE/IMG_0011.MOV, NSUnderlyingError=0x13b157510 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
My code for converting path to NSDATA is:
let videoURL = User.sharedInstance.arrRoomGalleryVideos.objectAtIndex(index) as? NSURL
var movieData: NSData?
do {
let video = try NSData(contentsOfURL: videoURL!, options: .DataReadingMappedIfSafe)
// print("video", video)
multipartFormData.appendBodyPart(data: video, name: "video_path[]", fileName: strVidName, mimeType: "mp4/.mov")
} catch {
print(error)
return
}
You can try this -
Navigate to Build Settings -> Go to Build Options and change the value of the "Compiler for C/C++/Objective-C" to Default Compiler.
For more solutions check this

Upload picture using alamofire

so my doubt is on how to upload an image using Alamofire, from what researched people uses the solutions here https://github.com/Alamofire/Alamofire/issues/110. However my curl request should look like this:
curl -X POST "api.local.app.com:9000/1/media/upload" -F “picture=#filename.png” -H “Authorization: Alpha ahudhasiadoaidjiajdiudaiusdhuiahdu” -v
I'm trying to do that this way:
Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = ["Authorization" : "Alpha \(userToken)" ]
var fileManager = NSFileManager()
var tmpDir = NSTemporaryDirectory()
let filename = "tempPicture.png"
let path = tmpDir.stringByAppendingPathComponent(filename)
var error: NSError?
let imageData = UIImagePNGRepresentation(image)
fileManager.removeItemAtPath(path, error: nil)
println(NSURL(fileURLWithPath: path))
if(imageData.writeToFile(path,atomically: true)){
println("Image saved")
}else{
println("Image not saved")
}
Alamofire.upload(.POST, databaseURL + "/media/upload", NSURL(fileURLWithPath: path)!).progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
println( String(totalBytesWritten) + "/" + String(totalBytesExpectedToWrite))
}
.responseJSON { (request, response, data, error) in
From what I understand this should work however the Alamo returns
Error: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "The
operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value
around character 2.) UserInfo=0x7e16d9e0 {NSDebugDescription=Invalid
value around character 2.}).
I have already provided a detail answer here. Most likely your server expects the image to be multipart form data encoded which is why it is getting rejected. There are many details provided in that link, along with next steps.

How can I get source code from URL with SSL using Swift?

I am trying to read a source code of a site from its URL.
Below is my code.
let myURLString = "https://theUrlOfTheSite"
let myURL = NSURL(string: myURLString)
var error: NSError?
let myHTMLString = String(contentsOfURL: myURL!, encoding: NSUTF8StringEncoding, error: &error)
if let error = error {
println("Error : \(error)")
} else {
println(myHTMLString)
}
It works for some site, but not for others. I get the following error
2014-12-21 19:11:54.032 MyPlayground[2912:345862] NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9807)
Error : Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)"
I think it's because the site is https, not http. (I don't really know about this stuff but..SSL something??)
So my question is, what should I do to make my code work??
I searched for hours but I'm new to iOS and Swift, so no progress.
Do I have to make NSURLConnection or something? Because all I want to do is get the source code. Must be a simple job I guess..
Any help will be appreciated. Thanks in advance.

Resources