Desktop Sandboxing Issue with Swift macOS - swift

I've been trying to write a Mac app lately, and have run into an issue. This is not overly major but it would be great to have a solution.
I have to load an image at runtime, specified by the user (so no, I cannot embed the image into the bundle as many alternatives suggest), and embed it into an NSTextView using NSAttributedText. The image loads fine from the Pictures directory (/Users/USERNAME/Pictures/IMAGE) etc, but not from the Desktop. I have checked the Capabilities tab in Xcode, and Read/Write is enabled for all of the available options:image
Using FileManager, I have determined that the file definitely exists, but is not readable. Can I set this manually in the Entitlements file?
Thanks in advance.

No, you can't. The standard desktop is not accessible out of the box if the app is sandboxed. Only the standard Downloads, Pictures, Music and Movies folder are accessible with the corresponding entitlement.
There are two options to access the other folders but in all cases the user must choose them once
Open/Save dialog for temporary access if the appropriate entitlement is set.
A security scoped bookmark for permanent access but also in this case the user must first choose the folder once via Open/Save dialog and the security scoped bookmark must be created programmatically.

Related

flutter for macOS : How to access "user-selected" desktop files and folders

My setup
macOS Big Sur
Xcode 12.5.1
With macOS sandboxing, I've enabled access to
User selected file
Download folder
Picture folder
Music folder
Movie folder
all with Read/Write permissions.
However, when I use the official file_selector and path_provider. I still cannot reference any files and folders outside of the Downloads/Music/Movies/Pictures.
I always get
OS Error: Operation not permitted, errno = 1
whenever I tried to access a directory outside of those directories mentioned above.
Expectation
I expect that by giving User selected file permission, I should be able to allow file_selector to access say user Documents/Desktop folder. Otherwise, how could the user have "selected" files if it requires separate permission from those predefined folders?
I read from native dev posts that one would need to call the native function NSOpenPanel.
Can anyone let me know how? Is it that flutter does not support such User selected File feature?
Should I resort to the platform method channel?
This sounds a bit crazy.
Solved it myself based on other answers. The gist
App Sandboxing will forbid an app to access files/folders outside of the sandbox, except for these user folders: Downloads/Movies/Music/Pictures. For instance, you won't be able to access home folder by using
path.join(path_provider.getApplicationDocumentsDirectory(), '..');
which would just give you
OS Error: Operation not permitted, errno = 1
Under Sandboxing mode, only limited locations are accessible, which must be configured within the Xcode project, either through Xcode GUI or onto the entitlement files directly.
For internal tooling or dev purposes, disable Sandboxing is good enough. The ways to disable it are
Within Xcode project Signing & Capabilities panel, click the "X" button at the right end of the App Sandbox category.
Editing the plist files DebugProfile.entitlements and Release.entitlements by setting the field
<key>com.apple.security.app-sandbox</key>
<false/>
After this, the file_selector API will be able to access files outside of the app sandbox.

Can my app be distributed through the MacOS App Store (struggling with Sandbox)?

I'm new to MacOS app development, and just getting around to hopefully releasing an app. I have two options - the Mac App store or do it through my website.
I would have preferred to do via the App Store but realized it has a "sandbox" requirement, and I'm flummoxed on what I should do.
My app requires heavy file/folder access. Some of the basic requirements are
ability to open and save files
ability to create folders (without prompting, as part of a process run)
ability for a backend process to access those files and run on it (e.g., if I added a markdown file to the app, and then run pandoc on it)
I can live with a user giving access to a base folder (and its subfolders) as kind of the app folder and I can work inside it. But being able to invoke a command-line tool installed on the mac, to run on those files, is paramount.
Can I do this at all? Or do I need to go outside the app store? I do not need access to system files etc and my app does absolutely nothing with any file except the ones within a project folder chosen by the user.
Any pointers to more recent documentation greatly appreciated. Kind of lost.

Downloading files for UIWebView to specific folder on IPhone

I have made an app that displays my website from a UIWebView and it takes you to a store that you can download files. My question is that I need to download a '.zip'file from my website using the app to the IPhone. Heres is my problem. I don't want the file to goto the App's Document folder, I want it to goto a folder that is outside of my applications folder. Here is an example path I might want my file to goto EX:'var/mobile/Library/Downloads'. If there is not a way to download the files to a path outside of the app's folder, is there a way to transfer the specific file that I have downloaded from my website using the my app to another folder that is not located in my app's folders? I know this can be done because I have used apps that have done this, of course they were apps only available on Cydia. This is also an app that will be used for Jail-broken users just for your information!
Thanks for any help!
Any "regular" iphone app runs in a sandbox that prevents access to system wide directories. Practically, you only have access to the app Home Directory.
Unless you go for a jailbroken app, you have no other options than storing to the App docs folder.
Read "The Application Sandbox" and "The File System" [here] for more details1.
EDIT:
I am not an expert on cydia, but the general idea is gaining access as root and then write to where you need to. This can be done by replacing your app with a suid shell script that will in turn exec your app (which will have root privileges now and then be able to go out of the sandbox).
For more info look at this.

iPhone "Share" button

Is there anything like the Android's "Share" button in iPhone?
I am just starting out in iOS development and do not have an iPhone (yet). The idea is to be able to select documents or images saved on the phone and use them in my app. I realize there is an ImagePickerController but that is not what I am looking for. I am also not looking for a third party component, rather something integrated into the OS similar to Android.
Short answer: No.
You can register your app to handle certain file types, and files of those types can then be opened by your app via UIActionSheets that may appear, for example, when you tap and hold on a file in an email. You can also transfer files to your app via iTunes. There's no 'file explorer' type feature in iOS where you can pull up a view of files saved to your phone.
For images, as you mentioned already, you must use UIImagePickerController.
From documentation
Sharing Files with the User
Applications that want to make user data files accessible can do so using application file sharing. File sharing enables the application to expose the contents of its /Documents directory to the user through iTunes. The user can then move files back and forth between the device and a desktop computer. This feature does not allow your application to share files with other applications on the same device, though. To share data and files between applications, you must the pasteboard or a document interaction controller object.
You can read more here:
https://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/StandardBehaviors/StandardBehaviors.html%23//apple_ref/doc/uid/TP40007072-CH4-SW7

Sharing files between iPhones and PC

Im looking to see how I can share / export content from my app to allow the content to be imported in the app running on another device.
Something like if you get emailed a pdf file or word file it opens in the correct app when tapped.
I tried using the iTunes sharing approach but that allowed full access to the apps documents folder which allowed the user to mess around with content they should not be allowed to!
The content in the app will be a bundled group of images, text and audio so could be large in size so was thinking of how to transfer easily from within the app to a PC of some file sharing service that would allow the target user to then select this content from within there app and have it imported available to use.
Is this a big ask?
The 1Password app uses DropBox and it seems to work quiet well.