Read file in document failed in Xcode 11.3 - objective-c

When I read file in document using stringWithContentsOfURL it failed
this is error in Xcode console:
Error Domain=NSCocoaErrorDomain Code=256 "The file “1.txt” couldn’t be opened." UserInfo={NSURL=/var/mobile/Containers/Data/Application/E026973D-11B6-4895-B8FE-7F9FBCC11C12/Documents/bbbb/1.txt}
this is my code:
//use objective-c
+(NSString * )loadDataFromDocumentDirectory:(NSString *)path andSubDirectory:(NSString *)subdirectory {
path = [self stripSlashIfNeeded:path];
subdirectory = [self stripSlashIfNeeded:subdirectory];
// Create generic beginning to file save path
NSMutableString *savePath = [[NSMutableString alloc] initWithFormat:#"%#/",[self applicationDocumentsDirectory].path];
[savePath appendString:subdirectory];
[savePath appendString:#"/"];
// Add requested save path
NSError *err = nil;
[savePath appendString:path];
NSURL *fileURL = [NSURL URLWithString:savePath];
NSString *loadStr = [NSString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:&err] ;
if (err) NSLog(#"load error : %#", err);
return loadStr;
}
//use swift
let loadData = FileSave.loadData(fromDocumentDirectory: "1.txt", andSubDirectory: "bbbb")

You are using the wrong API:
URLWithString is for URLs including the scheme (file:// or https://), for file system paths you have to use fileURLWithPath.
However it's highly recommended to use always the URL related API to build paths
+ (NSString * )loadDataFromDocumentDirectory:(NSString *)path andSubDirectory:(NSString *)subdirectory {
// path = [self stripSlashIfNeeded:path]; not needed
// subdirectory = [self stripSlashIfNeeded:subdirectory]; not needed
// Create generic beginning to file save path
NSURL *saveURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:subdirectory];
// Add requested save path
NSError *err = nil;
NSURL *fileURL = [saveURL URLByAppendingPathComponent:path];
NSString *loadStr = [NSString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:&err] ;
if (err) NSLog(#"load error : %#", err);
return loadStr;
}

Related

Unable to delete the contents of a Directory in my Home Directory using NSDirectoryEnumerator

I have been unable to delete the contents of a directory using NSDirectoryEnumerator.
Here is my method:
- (BOOL) deleteDirectoryContents:(NSString *)directoryPath
{
BOOL success = FALSE;
BOOL isDir;
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:directoryPath isDirectory:&isDir] && isDir)
{
NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtPath:directoryPath];
NSLog(#"dirEnum in deleteDirectoryContents is %#", dirEnum);
NSString *documentsName;
// NSLog(#"[dirEnum nextObject] is: %#", [dirEnum nextObject]);
while (documentsName = [dirEnum nextObject])
{
NSString *filePath = [directoryPath stringByAppendingString:documentsName];
NSLog(#"filePath is: %#", filePath);
BOOL isFileDeleted = [fileManager removeItemAtPath:filePath error:nil];
if(isFileDeleted == NO)
{
NSLog(#"All Contents not removed");
success = FALSE;
break;
}
success = TRUE;
}
if (success) NSLog(#"All Contents Removed");
}
return success;
}
And this is my code in my main program:
NSString *testDir = #"/Users/grinch/MyTestTemp";
//testDir = [NSHomeDirectory() stringByAppendingPathComponent: #"MyTestTemp"];
NSLog(#"testDir is: %#", testDir);
BOOL result = [self deleteDirectoryContents:testDir];
NSLog(#"result is: %d", result);
Here is my console output:
testDir is: /Users/grinch/MyTestTemp
dirEnum in deleteDirectoryContents is <NSAllDescendantPathsEnumerator: 0x60000008c300>
result is: 0
I also checked the value of [dirEnum nextObject] (by uncommenting the NSLog statement in my code). It returns null. And I never see the "filePath is" NSLog statement. So the inner while loop is NEVER executed.
And yes the directory (with files) does exist in my home directory. I created these files. I have permissions. I can easily delete the files in this directory using Finder
What am I missing?
P.S. I did some more testing. It looks like my simple program in Xcode does not have permissions to access files and folders in my home directory. Why? I have no idea.
Here is my additional test code:
NSError *errorMsg;
[[NSFileManager defaultManager] removeItemAtPath:#"/Users/grinch/myTestTemp/Hello.txt" error:&errorMsg];
if (errorMsg) NSLog(#"ERROR - File delete errorMsg is: %#", errorMsg);
success = [[NSFileManager defaultManager] removeItemAtPath:testDir error:&errorMsg];
if (errorMsg) NSLog(#"ERROR - Folder delete errorMsg is: %#", errorMsg);}
And here is my console output:
2022-09-10 09:56:48.958352-0400 NSAlert[97106:7511815] ERROR - File delete errorMsg is: Error Domain=NSCocoaErrorDomain Code=513 "“Hello.txt” couldn’t be removed because you don’t have permission to access it." UserInfo={NSFilePath=/Users/grinch/myTestTemp/Hello.txt, NSUserStringVariant=(
), NSUnderlyingError=0x6040006421f0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
2022-09-10 09:56:48.960560-0400 NSAlert[97106:7511815] ERROR - Folder delete errorMsg is: Error Domain=NSCocoaErrorDomain Code=513 "“MyTestTemp” couldn’t be removed because you don’t have permission to access it." UserInfo={NSFilePath=/Users/grinch/MyTestTemp, NSUserStringVariant=(
), NSUnderlyingError=0x60400045ff80 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
So my questions are:
Why didn't the directory enumerator work?
Why doesn't Xcode have the permissions to delete items in my home folder?
How can I give Xcode the the ability (or permissions) to delete items in my home directory?
I have found a bug in my deleteDirectoryContents: method above. And as a result, have answered most of my questions!
First, the statement NSString *filePath = [directoryPath stringByAppendingString:documentsName]; in the while loop will not work. One must insert a / before documentsname. So the line should be NSString *filePath = [directoryPath stringByAppendingFormat:#"/%#",documentsName]; OR even better use NSString *filePath = [directoryPath stringByAppendingPathComponent:documentsName];.
Now the method deleteDirectoryContents: will work as expected.
Here is the code to the fixed method:
- (BOOL) deleteDirectoryContents:(NSString *)directoryPath
{
BOOL success = FALSE;
BOOL isDir;
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:directoryPath isDirectory:&isDir] && isDir)
{
NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtPath:directoryPath];
NSLog(#"dirEnum in deleteDirectoryContents is %#", dirEnum);
NSString *documentsName;
// NSLog(#"[dirEnum nextObject] is: %#", [dirEnum nextObject]);
while (documentsName = [dirEnum nextObject])
{
//NSString *filePath = [directoryPath stringByAppendingFormat:#"/%#",documentsName];
NSString *filePath = [directoryPath stringByAppendingPathComponent:documentsName];
NSLog(#"filePath is: %#", filePath);
NSError *errorMsg = nil;
BOOL isFileDeleted = [fileManager removeItemAtPath:filePath error:&errorMsg];
if (errorMsg) NSLog(#"ERROR - Failed to delete file or folder at: %#. Error message is: %#", filePath, errorMsg);
if(isFileDeleted == NO)
{
NSLog(#"All Contents not removed");
success = FALSE;
break;
}
success = TRUE;
}
if (success) NSLog(#"All Contents Removed");
}
return success;
}
And I can confirm that if your program creates the directory and the files within it, the program can also delete these files using deleteDirectoryContents: while running in the Xcode sandbox DEPENDING on where the folder was originally created.
If the program creates the folder and files in a temporary directory (e.g. caches directory) NOT the users home folder, it works.
But due to Xcode sandboxing,
deleteDirectoryContents: does not appear to work on folder and files created by the program in the users Home directory when running within Xcode.
P.S. I used the following to obtain a temporary directory name:
- (NSString *) get_temporary_dir
{
NSString *path = nil;
NSString *bundleName = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleIdentifier"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
if ([paths count]) {
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:bundleName];
} else {
path = NSTemporaryDirectory();
path = [path stringByAppendingPathComponent:bundleName];
}
return [[[NSString alloc] initWithUTF8String:[path UTF8String]] autorelease];
}
Then the program created the temporary directory using that name. The program created some files in that temporary directory and then was able to delete them using the deleteDirectoryContents: method above.

Drop e-mail from mail.app into NSWindow object

I have an cocoa app where I would like to accept e-mails from mail.app dragged into the main window of the app. I have in my applicationDidFinishLaunching:
[_window registerForDraggedTypes:
[NSArray arrayWithObjects:
NSFilenamesPboardType,
(NSString *)kPasteboardTypeFileURLPromise, nil]]; //kUTTypeData
[_window setDelegate:(id) self];
This works fine, I can receive a document, in my performDragOperation: using
NSArray * files = [sender namesOfPromisedFilesDroppedAtDestination:url];
However, this only lets me drag the emails one-by-one. If I mark several emails, everything seems OK until I drop, then nothing happens. The performDragOperation is not even called.
I have tried to add kUTTypeData to the registerForDraggedTypes..., and then I get the performDragOperation... called, but then I cannot use the namesOfPromisedFilesDroppedAtDestination:url as it returns a nil pointer.
When I had the kUTTypeData included in the register... I included this in the performDragOperation to see what types in drag:
pboard = [sender draggingPasteboard];
NSLog(#"perform drag entered, %#", [pboard types]);
With the following result:
2013-07-25 15:09:50.771 BO2ICAL[1672:303] perform drag entered, (
"dyn.ah62d4rv4gu8y4zvanr41a3pwfz30n25wqz4ca5pfsr30c35feb4he2pssrxgn6vasbu1g7dfqm10c6xeeb4hw6df",
"MV Super-secret message transfer pasteboard type",
"dyn.ah62d4rv4gu8zg7puqz3c465fqr3gn7bakf41k55rqf4g86vasbu1g7dfqm10c6xeeb4hw6df",
"Super-secret Automator pasteboard type"
)
While the list for single e-mails are:
2013-07-25 15:14:30.096 BO2ICAL[1672:303] perform drag entered, (
"dyn.ah62d4rv4gu8y4zvanr41a3pwfz30n25wqz4ca5pfsr30c35feb4he2pssrxgn6vasbu1g7dfqm10c6xeeb4hw6df",
"MV Super-secret message transfer pasteboard type",
"dyn.ah62d4rv4gu8zg7puqz3c465fqr3gn7bakf41k55rqf4g86vasbu1g7dfqm10c6xeeb4hw6df",
"Super-secret Automator pasteboard type",
"dyn.ah62d4rv4gu8yc6durvwwa3xmrvw1gkdusm1044pxqyuha2pxsvw0e55bsmwca7d3sbwu",
"Apple files promise pasteboard type",
"public.url",
"CorePasteboardFlavorType 0x75726C20",
"dyn.ah62d4rv4gu8yc6durvwwaznwmuuha2pxsvw0e55bsmwca7d3sbwu",
"Apple URL pasteboard type",
"public.url-name",
"CorePasteboardFlavorType 0x75726C6E",
"com.apple.pasteboard.promised-file-content-type",
"com.apple.pasteboard.promised-file-url",
"dyn.ah62d4rv4gu8y6y4usm1044pxqzb085xyqz1hk64uqm10c6xenv61a3k",
NSPromiseContentsPboardType
)
Does anyone have any advice how to do this correctly in order to accept multiple e-mails?
I have found a solution to this. I found that the data provided in the mode "kUTTypeData" gave me enough data to grab the files directly from the mail.app mailbox.
In the mbox folder, there is a folder with a long sequence of numbers and dashes, there was no trace of this name anywhere in the mailbox hierarchy, but since this only contains this folder and an info.plist file, I used this function to grab that name: Update: implemented regexp check since the folder sometimes contains sub-mailboxes that can have a longer name...
-(NSString*)FindCodedFolderInMailbox:(NSString*)mailboxpath {
NSString *uuid_regexp = #"[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}";
NSPredicate *uuid_test = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", uuid_regexp];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *fileList = [fileManager contentsOfDirectoryAtPath:mailboxpath error:nil];
for (NSString * file in fileList) {
if ([uuid_test evaluateWithObject: file]){
return file;
}
}
return nil;
}
Then the section where I find there is no "NSPromiseContentsPboardType", but instead a "Super-secret Automator pasteboard type", I wrote the following section (There is some NSLog entries I intend to remove, but here it is:
} else if ( [[pboard types] containsObject:#"Super-secret Automator pasteboard type"] ) {
NSFileManager *fileManager = [NSFileManager defaultManager];
// Create the URL for the destination folder and ensure it exists.
NSURL *applicationFilesDirectory = [self applicationFilesDirectory];
NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:#"documents"];
BOOL isDir;
if (!([fileManager fileExistsAtPath:[url path] isDirectory:&isDir] && isDir)) {
NSError * error = nil;
[ fileManager createDirectoryAtURL:url withIntermediateDirectories: YES attributes:nil error:&error];
if (error) {
[[NSApplication sharedApplication] presentError:error];
}
}
BOOL ok = false;
// locate the mailbox path....
NSString *mailboxpath = [pboard stringForType:#"MV Super-secret message transfer pasteboard type"];
NSLog(#"Mailboxpath: %#", mailboxpath);
NSString * codedFolder = [self FindCodedFolderInMailbox:mailboxpath];
if (codedFolder) {
NSString * codedpath = [NSString stringWithFormat:#"file://%#/%#/Data", mailboxpath, codedFolder];
NSURL * mb1 = [NSURL URLWithString:codedpath];
NSLog(#"Directory:%#", mb1);
NSArray *msgArray = [pboard propertyListForType:#"Super-secret Automator pasteboard type"];
if (msgArray) {
for (NSDictionary *msg in msgArray) {
// Locate the message....
NSNumber * msgID = [msg valueForKey:#"id"];
NSLog(#"Melding(%#):%#", msgID, msg);
NSString * filename = [NSString stringWithFormat:#"%#.emlx", msgID];
// second and first letter of id
NSString * idSec = [[msgID stringValue]substringWithRange:(NSRange){1, 1}];
NSString * idFirst = [[msgID stringValue]substringWithRange:(NSRange){0, 1}];
NSString * subpath = [NSString stringWithFormat:#"%#/%#/Messages/%#",idSec, idFirst, filename];
NSURL * thisFilePath = [mb1 URLByAppendingPathComponent:subpath];
if ([fileManager fileExistsAtPath:[thisFilePath path]]) {
NSURL *destpath = [url URLByAppendingPathComponent:filename];
NSError * error = nil;
[fileManager copyItemAtURL:thisFilePath toURL:destpath error:&error];
if (error) {
[[NSApplication sharedApplication]presentError:error];
} else {
[self ParseEmlMessageforPath:[destpath path] filename:filename];
}
}
}
}
}
And here we go.... :-)

Writing to /tmp folder iPad

I´m writing certain values to a file. See Write Operations below.
This works fine when using iPad 6.1 Simulator.
When trying the same thing on my iPad it fails. I think it´s something with sandboxing. I haven´t found out yet which path is best on iOS Devices to write stuff for internal use.
Any ideas?
#pragma mark Write Operations to Tmp Folder
- (BOOL) psWriteFileWithName: (NSString*) fileName
withString:(NSString*) string {
NSString *fileName = #"artistNumber";
NSError * error = NULL;
NSString *filePath = [NSString stringWithFormat:#"/tmp/%#.txt",fileName];
[string writeToFile:filePath
atomically:YES
encoding: NSUTF8StringEncoding
error:&error];
return YES;
}
You cannot write to /tmp since this is outside of your app sandbox.
However your app also has a temp directory, which can be referenced with the NSTemporaryDirectory() function:
Which works like:
NSString *tempfilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
Here is you method with the correct NSTemporaryDirectory() implementation, also edit some error handling:
#pragma mark Write Operations to Tmp Folder
- (BOOL) psWriteFileWithName: (NSString*) fileName
withString:(NSString*) string {
NSString *fileName = #"artistNumber";
NSError *error = nil;
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
if (![string writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error] ) {
NSLog(#"Error writing file: %#", error);
return NO;
}
return YES;
}

Prevent app from backing up documents folder?

I'm trying to prevent my app backing up files to iCloud but have become completely confused and a little lost.
-EDIT-
I've updated this to reflect the changes I've made thanks to the posters below.
I want to prevent back up of files which are downloaded to the app's documents directory.
So far I have a class called PreventBackup with the following method:
+ (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
NSError *error = nil;
BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
forKey: NSURLIsExcludedFromBackupKey error: &error];
if(!success){
NSLog(#"Error excluding %# from backup %#", [URL lastPathComponent], error);
}
NSLog(#"prevent backup method called without error");
return success;
}
I'm then calling it with this code when the app starts:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURL *pathURL= [NSURL fileURLWithPath:documentsDirectory];
[PreventBackup addSkipBackupAttributeToItemAtURL:pathURL];
The cosole prints prevent backup method called without error but the app is still showing as having the same amount of data for back up with it did before.
Any idea where this is going wrong?
-EDIT 2-
OK, I think this is solved. The files are downloading to a sub folder called "downloads". changing the code above so that it reads as follows appears to have done the trick:
NSString *downloadsFolder = [documentsDirectory stringByAppendingPathComponent:(#"/downloads")];
NSURL *pathURL= [NSURL fileURLWithPath:downloadsFolder];
[PreventBackup addSkipBackupAttributeToItemAtURL:pathURL];
Thanks to all for your help.
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
NSError *error = nil;
BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
forKey: NSURLIsExcludedFromBackupKey error: &error];
if(!success){
NSLog(#"Error excluding %# from backup %#", [URL lastPathComponent], error);
}
return success;
}
NSURL *documentURLWithExtension = [documentURL URLByAppendingPathExtension:extensionType];
pass this "documentURLWithExtension" to this function
[self addSkipBackupAttributeToItemAtURL:documentURLWithExtension];
In Swift:
//Path of document directory
var docPathAry : NSArray! = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var docDirPathStr: AnyObject? = docPathAry.count > 0 ? docPathAry[0] : nil
self.addSkipBackupAttributeToItemAtURL(NSURL.fileURLWithPath(docDirPathStr as NSString))
and:
func addSkipBackupAttributeToItemAtURL(URL: NSURL!) -> Bool {
assert(NSFileManager.defaultManager().fileExistsAtPath(URL.path))
var err : NSError? = nil
var success : Bool! = URL.setResourceValue(NSNumber.numberWithBool(true), forKey: NSURLIsExcludedFromBackupKey, error: &err)
if(!success) {
println("Error excluding \(URL.lastPathComponent) from backup\(err) ")
}
return success
}

Appending NSString while using NSApplicationSupportDirectory to create a new directory

I have been trying to create a new file inside of my application support folder while using NSApplicationSupportDirectory; I can write a file to it, but I have been unable to create a folder inside of Application Support.
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *applicationDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the application support: (attempting to create the blasted directory inside of application support directory
NSString *fileName = [NSString stringWithFormat:#"%#/managersemail.txt",
applicationDirectory];
//create content - formats with the managersemail.txt location
NSString* content = [NSString stringWithFormat:#"%#",[nameField stringValue]];
//save content to the documents directory
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
NSDictionary* errorDict;
The code that I have listed above works great, except for the part about creating the folder in which I want to place the managersemail.txt. I tried to mimic the stringWithFormat that is listed in the NSString* content and then varying several ways however to no avail! Any thoughts?
NSAppleEventDescriptor* returnDescriptor = NULL;
Perhaps the solution provided on Cocoa with Love might be useful?
Excerpt:
- (NSString *)findOrCreateDirectory:(NSSearchPathDirectory)searchPathDirectory
inDomain:(NSSearchPathDomainMask)domainMask
appendPathComponent:(NSString *)appendComponent
error:(NSError **)errorOut
{
// Search for the path
NSArray* paths = NSSearchPathForDirectoriesInDomains(
searchPathDirectory,
domainMask,
YES);
if ([paths count] == 0)
{
// *** creation and return of error object omitted for space
return nil;
}
// Normally only need the first path
NSString *resolvedPath = [paths objectAtIndex:0];
if (appendComponent)
{
resolvedPath = [resolvedPath
stringByAppendingPathComponent:appendComponent];
}
// Check if the path exists
BOOL exists;
BOOL isDirectory;
exists = [self
fileExistsAtPath:resolvedPath
isDirectory:&isDirectory];
if (!exists || !isDirectory)
{
if (exists)
{
// *** creation and return of error object omitted for space
return nil;
}
// Create the path if it doesn't exist
NSError *error;
BOOL success = [self
createDirectoryAtPath:resolvedPath
withIntermediateDirectories:YES
attributes:nil
error:&error];
if (!success)
{
if (errorOut)
{
*errorOut = error;
}
return nil;
}
}
if (errorOut)
{
*errorOut = nil;
}
return resolvedPath;
}
Maybe you can try using the NSFileManager to create the folder, then write the file into the folder.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *applicationSupport = [[NSString stringWithString:#"~/Library/Application Support/'YOUR APP'] stringByExpandingTildeInPath];
if ([fileManager fileExistsAtPath:applicationSupport] == NO)
[fileManager createDirectoryAtPath:applicationSupport withIntermediateDirectories:YES attributes:nil error:nil];
NSString *fileName = [NSString stringWithFormat:#"%#/managersemail.txt", applicationSupport];
NSString* content = [NSString stringWithFormat:#"%#",[nameField stringValue]];
//save content to the documents directory
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
So something like that should work. Feel free to leave comments to ask questions.