I want to upload an NSString to a text file (.txt) on an ftp server.
I use the following code:
- (IBAction)Blau:(id)sender {
BlauS = #"1";
NSURL *BlauURL= [NSURL URLWithString:#"ftp://username:password#myftpserverurl/blau.txt"];
NSError *error;
BOOL proof=[BlauS writeToURL:BlauURL atomically:NO encoding:NSUTF8StringEncoding error:&Error];
if (!proof) {
NSLog(#"Error!: %#", [error localizedFailureReason]);
}
}
My log shows me the following error:
The specified URL type isn’t supported.
What's wrong? How can I fix this problem?
Related
I am trying to download an XML file from this URL www.overpass-api.de/api/xapi?[maxspeed=][bbox=5.6283473,50.5348043,5.6285261,50.534884]
but it returns nil to NSData. My code is:
NSURL *url = [NSURL URLWithString:#"www.overpass-api.de/api/xapi?*[maxspeed=*][bbox=5.6283473,50.5348043,5.6285261,50.534884]"];
NSError *error;
NSData *urlData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
if (error) {
NSLog(#"%#", [error localizedDescription]);
} else {
NSLog(#"Data has loaded successfully.");
}
The error says "The file “xapi” couldn’t be opened because there is no such file."
Please help how can I download the XML returned from the URL and parse it?
Got it working! Just added http:// to the beginning of the link and it returns proper data. Wow!
I have a scenario where app reads file from server (dropbox) and checks version. If new version is available then download the app.
I'm trying to read file from link but getting null after JSON parsing.
NSError *error;
NSString *strFileContent = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"https://www.dropbox.com/s/22mm417fxdqdn8c/FileStructure.txt?dl=0"]
encoding:NSUTF8StringEncoding
error:&error];
if(!error) { //Handle error
// NSLog(#"strFileContent: %#",strFileContent);
NSData *data = [strFileContent dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(#"json : %#",[json description]);
}
else
NSLog(#"Error in reading!");
}
Any alternative to DropBox?
There is a way, I find it quite ugly, but still.
First issue, as mentioned by other: yourURLEtcdl=1.
Second big issue, the file is a rtf file, not a JSON object.
So, since there is a way going with RTF => NSAttributedString => NSString => "JSON" => Get your wanted value, here is what you could do:
NSError *error;
NSURL *url = [NSURL URLWithString:#"https://www.dropbox.com/s/22mm417fxdqdn8c/FileStructure.txt?dl=1"];
NSString *strFileContent = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:&error];
NSLog(#"strFileContent: %#", strFileContent);
if(!error)
{
NSError *rtfError;
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[strFileContent dataUsingEncoding:NSUTF8StringEncoding]
options:#{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil
error:&rtfError];
if (rtfError)
{
NSLog(#"RTFError: %#", [rtfError localizedDescription]);
}
else
{
NSLog(#"string: %#", [attributedString string]);
NSData *data = [[attributedString string] dataUsingEncoding:NSUTF8StringEncoding];
NSError *jsonError;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (jsonError)
{
NSLog(#"JSON Error: %#", [jsonError localizedDescription]);
}
else
{
NSLog(#"JSON: %#", jsonDict);
NSInteger version = [[jsonDict objectForKey:#"Version"] integerValue];
NSLog(#"Version: %ld", (long)version);
}
}
}
else
{
NSLog(#"Error: %#", error);
}
As I said, I don't find these very great, it works, but this solution seems to me quite messy (passing twice with NSData).
That's not a text file containing JSON; it's an RTF file containing JSON. That won't work with NSJSONSerialization:
{\rtf1\ansi\ansicpg1252\cocoartf1347\cocoasubrtf570
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural
\f0\fs24 \cf0 \{\
"Version": 1.0,\
"ImageList": [\
"Nature.png",\
"Nature-1.png",\
"Ballon.png"\
]\
\}}
You've saved that file using an editor that uses RTF (the default Mac TextEdit ). you'll need to re-save as plain text.
Provided URL is incorrect too. If you need only file contents you have to set dl=1 (last component of url). Like so -
dropbox.com/s/22mm417fxdqdn8c/FileStructure.txt?dl=1
I have a video document which is uploaded on server . The name of that document does not contain any extension.
I have to download that document (which is actually a video file) from the server and play it.
I need to know how do I get the extension of that file. I do not have it specified in my URL.
its just like /var/mobile/Applications/B18D9BE8-6E1D-43F0-8240-A909B9A27F7C/Documents/XXX/docdata/ABC/XYZ
XYZ is the document which I want to play. It is actally a video file which is saved on the server.
In Android we do something like parse URI. DO we have anything similar like this in iOS.
a general approach. Have a look at the meta-data of the file to determine which vidoe type it is. This should be provided at the file itself.
Perhaps this helps: https://stackoverflow.com/a/5815629/1933185
The following solution worked for me. Hope it help others as well.
NSString* fullPath = [yourpath stringByExpandingTildeInPath];
NSURL* fileUrl = [NSURL fileURLWithPath:fullPath];
NSURLRequest* fileUrlRequest = [[NSURLRequest alloc] initWithURL:fileUrl cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:.1];
NSError* error = nil;
NSURLResponse* response = nil;
NSData* fileData = [NSURLConnection sendSynchronousRequest:fileUrlRequest returningResponse:&response error:&error];
NSString* mimeType = [response MIMEType];
NSArray *components = [mimeType componentsSeparatedByString:#"/"];
NSString *query = [components lastObject]; //gives the extension
This query is the file extension. Your can put an if/else or switch to perform operation based on your extension.
if ([query isEqualToString:#"mp4"]) {
yourpath = [yourpath stringByAppendingPathExtension:#"mp4"];
}
// likewise for all extensions
NSData* video = [NSData dataWithContentsOfURL:fileUrl options:NSDataReadingUncached error:&error];
if (error) {
NSLog(#"%#", [error localizedDescription]);
[error release];
} else {
NSLog(#"Data has loaded successfully.");
}
BOOL success = [video writeToFile:yourpath atomically:NO];
[fileUrlRequest release];
if (success) {
//open your video the way you want
}
This question already has answers here:
Cocoa - NSFileManager removeItemAtPath Not Working
(6 answers)
Closed 9 years ago.
i am trying to delete a file at a particular URL,
my code iS :
NSString *str= [outputFieldURL absoluteString];
NSError *error;
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:str error:&error];
if (!success) {
NSLog(#"Error removing file at path: %#", error.localizedDescription);
}
else
{
NSLog(#"File removed at path: %#", error.localizedDescription);
}
}
and am getting output :
Error removing file at path: The operation couldn’t be completed. (Cocoa error 4.)
where outputFieldURL is showing following value when i convert it into a string :
file:///var/mobile/Applications/A55A56FA-478D-4996-807D-12F0E968F969/Documents/301013125211w.m4a
this is the path where my audio with a format of .m4a is saved
Your path is incorrect
Use the following
NSString *str= [outputFieldURL path];
in place of
NSString *str= [outputFieldURL absoluteString];
The method "removeItemAtPath:" need the local path of file,
If you want to remove using url, you should use "removeItemAtURL:"
There may be case that file path which you provide is not correct. If its correct then try following with URL, it might solve your issue
NSString *str= [outputFieldURL absoluteString];
NSError *error;
NSURL *url = [NSURL URLWithString:str];
BOOL success = [[NSFileManager defaultManager] removeItemAtURL:url error:&error];
if (!success) {
NSLog(#"Error removing file at path: %#", error.localizedDescription);
}
else
{
NSLog(#"File removed at path: %#", error.localizedDescription);
}
}
I'm having trouble synching a simple textfile, I get this error when trying to open it:
{NSFilePath=/private/var/mobile/Library/Mobile Documents/4C224W52W5~com~piso13~opusDomini/currentLogPath, NSUnderlyingError=0xde9b460 "The operation couldn’t be completed. Bad file descriptor"}
This is how I create it
-(BOOL)createLogFolderFile{
NSString *uuid = nil;
CFUUIDRef uuidRef = CFUUIDCreate(nil);
uuid = (NSString*)CFUUIDCreateString(nil, uuidRef);
CFRelease(uuidRef);
NSError *error = nil;
[uuid writeToFile:[self filePath] atomically:NO encoding:NSUTF8StringEncoding error:&error];
if (error) {
NSLog(#"Error trying to create log file %#", error);
return FALSE;
}
else{
return TRUE;
}
}
-(NSString*)filePath{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *iCloudPath = [[fileManager URLForUbiquityContainerIdentifier:nil] path];
return [iCloudPath stringByAppendingPathComponent:LOG_FOLDER_FILE_NAME];
}
This is how I read it:
-(NSString*)readLogFolderFromFile{
NSError *error = nil;
NSString *logFolder = [NSString stringWithContentsOfFile:[self filePath] encoding:NSUTF8StringEncoding error:&error];
if (error) {
NSLog(#"Error when trying to read log folder from file: %#" ,error);
return nil;
}
else{
return logFolder;
}
}
I'm using NSMetadataQuery to search for the file,
The notification query finish gathering info results positive.
Help?
The file was not downloaded. It seems NSMetadataQuery notifies about the existence of the file in the cloud. To actually get the file, extra code is needed:
Inside queryDidFinishGathering notification:
NSMetadataItem *item = [query resultAtIndex:0];
self.metadataItem = item;
BOOL isDownloaded = [[item valueForAttribute:NSMetadataUbiquitousItemIsDownloadedKey]boolValue];
if (!isDownloaded) {
NSError *error = nil;
[[NSFileManager defaultManager] startDownloadingUbiquitousItemAtURL: [item valueForAttribute:NSMetadataItemURLKey] error:&error];
NSLog(#"Start downloading file");
if (error) {
NSLog(#"Error trying to download file: %#", error);
}
else{
[self lookForLogFolderFile];
return;
}
}
The lookForLogFolderFile simply starts the query again.
After several calls my item gets downloaded. You can also use a timer to between each call to start a NSMetadataQuery. In my case, is just a text file with one line.