0

Hi i am trying to load image which is located at desktop .

NSURL *imageURL = [[NSWorkspace sharedWorkspace] desktopImageURLForScreen:[NSScreen mainScreen]];
 NSLog(@"%@",imageURL);
 NSImage *testImage = [[NSImage alloc] initWithContentsOfURL:imageURL];
 NSLog(@"%@",testImage);
[self.desktopView setImage:testImage];

Log of testImage (null)

Above code I am using but test image is getting nil .

Any suggestions?

Thanks in Advance !

iosdev
  • 90
  • 9

1 Answers1

1

You probably have the App Sandbox enabled in Capabilities (I believe it's enabled by default). With it enabled, you won't be able to access files outside of your apps container (or certain other specified locations) without user interaction (i.e. NSOpenPanel or NSSavePanel).

File System Programming Guide: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

App Sandbox documentation here: https://developer.apple.com/library/archive/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html

If you'd like to disable the App Sandbox, it can be turned off by clicking on your project file > target name, selecting the capabilities tab and switching the App Sandbox off.

Edit:

You’re going to want to use a security scoped bookmark for persistent resource access. I’m not familiar with the apps in your comment but my guess is they prompt the user on first run for access.

Documentation here:

https://developer.apple.com/library/archive/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html#//apple_ref/doc/uid/TP40011183-CH3-SW16

https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingAppSandbox.html#//apple_ref/doc/uid/TP40011195-CH4-SW18

R4N
  • 2,455
  • 1
  • 7
  • 9
  • My app Sandboxed without user interaction can't we access location of image located in desktop Because every time i don't want to present NSPanel and also [HiddenMe](https://itunes.apple.com/us/app/hiddenme/id467040476?ls=1&mt=12) and [Presenter Mate](http://presentermate.com/?ref=producthunt)is doing that how are they doing? – iosdev Sep 28 '18 at 11:50
  • You’re going to want to use a security scoped bookmark. I’ve edited my answer. – R4N Sep 28 '18 at 13:07
  • @iosdev In a sandboxed app you cannot access locations outside of the container without any user interaction, There is **no way**. – vadian Sep 28 '18 at 13:12
  • No they are not prompting even in the first run . – iosdev Sep 28 '18 at 13:13
  • @vadian Check Hiddenme app how he is accessing the desktop wallpaper ? It is sandboxed application . – iosdev Sep 28 '18 at 13:15
  • @iosdev This app uses most likely an API (maybe in CoreFoundation) to do that. Once again: Accessing an URL outside of the container without user interaction is impossible, there is no exception. – vadian Sep 28 '18 at 13:21
  • @vadian if you could explain how they are doing as you told with some API it will be really helpful. – iosdev Sep 28 '18 at 13:26
  • @iosdev I have no idea, I'm just guessing. Logically there must be an API – vadian Sep 28 '18 at 13:27
  • Helped [this](https://stackoverflow.com/questions/8082837/cocoa-take-screenshot-of-desktop-wallpaper-without-icons-and-windows) for single screen how to do it for multiple screens. – iosdev Oct 08 '18 at 13:53