Domain=NSCocoaErrorDomain Code=513 only when rename folder through UITests target for UI Autoamtion - objective-c

I am developing a product like DropBox and Box application, and I need to verify Rename/Add/Delete I/O operation using automation.
I am writing UI Automation Test Cases using XCUITest in Xcode.
In that I need to use I/O operation for rename file/folder but It gives the below error while using moveItemAtPath function to rename folder:
Error Domain=NSCocoaErrorDomain Code=513 "“Folder” couldn’t be moved because you don’t have permission to access “Folder1”." UserInfo={NSSourceFilePathErrorKey=*path, NSUserStringVariant=(
Move), NSDestinationFilePath=*destiPath, NSFilePath=*path, NSUnderlyingError=0x6000002507d0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
Its working fine when running the app in main target and also its working in UnitTest target but its not working while running UITest automation target.
NOTE: App Groups are also enabled.
Any help is appreciated!

Try changing the folder you writing to or in your case i dont know why you're renaming the folder. If you've to do something to your folder then perhaps having that folder in one of these locations Documents, Temp and Cache. Most likely, you will want to write to the Documents folder.
It seems to me like a codesign error. Refer this post NSFileManager creating folder (Cocoa error 513.)

Try this below approach. I'm quoting apple documentation - https://developer.apple.com/library/archive/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html#//apple_ref/doc/uid/TP40011183-CH3-SW22
Scenario 1:
Your app needs to be able to save a file with a different extension than that of the original file. For example, when you paste an image into an RTF file in TextEdit and save it, TextEdit changes the file’s extension from .rtf to .rtfd (and it becomes a directory).
To handle this situation, you must use an NSFileCoordinator object to coordinate access to the file. Before you rename the file, call the itemAtURL:willMoveToURL: method. After you rename the file, call the itemAtURL:didMoveToURL: method.
Scenario 2:
Your app needs to be able to open or save multiple related files with the same name and different extensions (for example, to automatically open a subtitle file with the same name as a movie file, or to allow for a SQLite journal file).
To gain access to that secondary file, create a class that conforms to the NSFilePresenter protocol. This object should provide the main file’s URL as its primaryPresentedItemURL property, and should provide the secondary file’s URL as its presentedItemURL property.
After the user opens the main file, your file presenter object should call the addFilePresenter: class method on the NSFileCoordinator class to register itself.
In both scenarios, you must make a small change to the application’s Info.plist file. Your app should already declare a Document Types (CFBundleDocumentTypes) array that declares the file types your app can open.
For each file type dictionary in that array, if that file type should be treated as a potentially related type for open and save purposes, add the key NSIsRelatedItemType with a boolean value of YES.
To learn more about file presenters and file coordinators, read File System Programming Guide.

Related

I want to copy the file in sandbox without NSOpenPanel

I want to copy some files to specified folder by NSOpemPanel.
The source file is reading from XML and show to list in NSTable.
I can copy file by copyItemAtPath.
But now my app will turn to sandbox, then I can’t copy the file by copyItemAtURL.
How do I copy the file in sandbox mode?
I was looked over a lot of post. And I think the Security-scoped Bookmark may be a solution for this.
But I can’t create "Security-scoped Bookmark" from XML inside the path (the path was convert to NSURL ready).
I was setting to sandbox.entitlements but it's not clear this problem.
Is there any way for this?
Develop in macOS10.12 and Xcode8.3.3
Thanks
How do I copy the file in sandbox mode?
It is unclear what your current code is doing, but the rules under the sandbox are simple: To read or write a file located outside of an application's own container (which is hidden away under the Library folder) your application must either:
Use NSOpenPanel to obtain a URL from the user for the file path; or
Use NSOpenPanel to obtain a URL from the user for one of the ancestor folders of the file.
The second option gives access to a whole folder, including any sub-folders; i.e. the whole file/folder subtree rooted in the folder.
As you want to copy "some files" it sounds like asking the user for permission for the folder is appropriate. You can customise the NSOpenPanel to be a "request permission" dialog. If you are requesting a specific folder you can also have the dialog open in it's containing folder and only have the specific folder enabled for selection by the user.
Once you have the URL for the folder from NSOpenPanel you can create a security scoped bookmark for it and save that in your app's preferences or other configuration file (stored within the app's container). Doing this enables your app to regain access to the folder on subsequent executions without asking the user again.
If after investigating this issue and writing some code you hit an issue ask a new question, showing your code, and explaining the problem. Someone will undoubtedly help you with the next step.
HTH

Problems overwriting file in application folder

I have a file called PolicyLookup.sql that sits in my application's root folder. My app loads this file into a text box, so that users can edit it and overwrite the original file by pressing a save button. This all worked perfectly during test, however after deployment users are unable to save the file due to write issues within C:\Program Files.
Is there a way around this - or is there a better way to implement this type of thing? One solution that springs to mind for me is placing the contents of the PolicyLookup.sql file within a User Setting - however it intrinsically feels wrong to me to put the entire contents of a file within a settings variable.
Ordinary users do not have write permissions on %ProgramFiles%. If you need to save a configuration file then put it in a subfolder of %APPDATA% (which for me is C:\Users\Gord\AppData\Roaming) or some other place where a regular user is allowed to write.

Cannot Overwrite User Selected File from App Sandbox

I have a Mac App that generates some data and write it to a file then asks the user where they want to save it and moves that saved file from the sandbox to the location they specify. This works fine unless the user selects a file that already exists. If the user presses "Replace" when OSX tells them that a file already exists, the NSError becomes non-nil with the following localized description.
"testfile" couldn’t be copied to “Desktop” because an item with the
same name already exists.
How can I get this sandboxed app to overwrite the pre-existing file?
Thank you,
Stateful
This question provides the answer to my question:
Replace a file using the NSSavePanel in a sandboxed application
Thank you, Merlin!

Are Objective-C function names stored in text?

I don't remember where I heard about it, (I think I was searching up on how selectors worked and it ended up not being exactly the same as a callback function) and I can't confirm it. But more importantly than that, is there a way I could get a list of the function names from another application?
See class-dump.
You can sometimes see a list of the method names in an application. Find the .ipa file for the app (possibly in the Music->iTunes->Mobile Applications directory on a Mac). Make a copy of one of the ipa files and change it from .ipa to .zip. Unzip the file and in the Payload folder, there's a file with the same name as the app. Right click on it and "Show Package Contents". In there, you'll find another file with the same name as the app (with no extension). This is the executable file for the app. If you open it in a text editor like BBEdit, you'll sometimes be able to see some method names, as well as a list of the frameworks the app is built against. You can find the path name where things were kept too, often with the developer's name.
For example, looking into the Japanese dicitonary Daijirin, I can see these methods:
-[HMWebSupport openMONOKAKIDOSupportWithSafariForView:style:]
-[HistoryViewController viewWillAppear:]
I can also see that one of the developers was called Norihito, and he was using SVN: /Users/norihito/Developer/SVN/Mobile/DAIJIRIN/Other
Other applications (like Weightbot) don't show as much information. I don't know the reason it shows up some times and doesn't other times.

Associate file type/extension to non document based cocoa application

This is for some reason really hard to find and solve. Cannot find any documentation on it.
I've got a non-document based application which I would like to open all specific file types, but I do not want to use NSDocuments but let my code handle the opening of file and processing of it. I've added the CFBundleTypeExtensions and all, settings to the info.plist.
Now the application will open the filetype but display an error like: "The document xxxx could not be opened. Application cannot open files in the yyyyy format."
Have you implemented -application:openFile: in an app delegate? If not, your application has no way of knowing how to open some random file in some random format. It's up to you to add that functionality and respond with success or failure.