openURL on local file in temporary directory does nothing - swift

I'm saving a downloaded PDF file to a temporary directory on my device, and have the URL for the resultant file:
file:///Users/colinbasnett/Library/Developer/CoreSimulator/Devices/A057DDAD-B116-424B-8383-442321530EEC/data/Containers/Data/Application/A0AEF93A-5B1D-4CB4-B39F-F6DFECEDD9E9/tmp/FF5C09A9-45CD-454E-B55A-4F5CEBFEBC7F-24875-000014B657DC6436/23659.pdf
Where fileURL is a valid NSURL object representing the path above, I call this:
UIApplication.sharedApplication().openURL(fileURL)
and nothing happens.
Despite this, the following call returns true:
UIApplication.sharedApplication().canOpenURL(fileURL) // returns true
Ideally this would open in Safari or whatever the preferred browser application is. Interestingly enough, I can manually open Safari, paste that directory in the address bar and it is able to display the PDF.
I'm using Swift 2.2 (can't switch to 3 yet because dependencies have not been upgraded).

I think the problem is that openURL() wants to launch a separate application, but you're passing a file: URL that points to a file in your sandbox. I suspect UIDocumentInteractionController might be more what you're after.

Related

Open Downloads Directory With Finder Prompting User for Permission If Necessary

I'm building a BitTorrent client where I give the user an option, through a context menu, to open the containing directory of a torrent.
To do so, I tried using open(_) method of an NSWorkspace instance like so:
NSWorkspace.shared.open(directory)
where directory is an URL instance pointing to a directory, like so:
let directory = URL(fileURLWithPath: item.parentPath, isDirectory: true)
Here, item.parentPath is a String holding an absolute path.
Now, let me clear up that the code runs fine. It successfully opens up the directories I want inside Finder (since it's the default application for opening directories).
However, if the directory happens to be the Downloads directory of the user, it displays this prompt:
Again, this is alright since my application does not have permission to open the Downloads directory. However, I want to attempt to open the directory, asking for permission, just like any other application on macOS, like so:
I looked up in the docs and found this method of NSWorkspace: open(_:withApplicationAt:configuration:completionHandler:). I thought it was great since I could set the promptsUserIfNeeded property of an NSWorkspace.OpenConfiguration instance to true, which I believe should make my application politely ask for permission to open the directory if needed.
Here's my resulting code:
let url = URL(fileURLWithPath: item.parentPath, isDirectory: true)
let configuration: NSWorkspace.OpenConfiguration = NSWorkspace.OpenConfiguration()
configuration.promptsUserIfNeeded = true
let finder = NSWorkspace.shared.urlForApplication(withBundleIdentifier: "com.apple.finder")
// Open file with default application
NSWorkspace.shared.open([url], withApplicationAt: finder!, configuration: configuration)
Sadly, it's making no difference. I'm still getting the same dialog as shown in the first image.
I want to know two things:
What am I doing wrong?
How can I open a directory, prompting for permissions if necessary?
I am assuming you want this all to play nicely within the sandbox. You have two choices:
Use activateFileViewerSelecting(_:) or selectFile(_:inFileViewerRootedAtPath:). Either of these will prompt for permission, & once gained, you can return to using open(_:withApplicationAt:configuration:completionHandler:), if you so wish.
Use Security-Scoped Bookmarks and Persistent Resource Access.

Why doesn't AVPlayer work with a URL found with FileManager?

If I use a file URL fetched with NSOpenPanel, creating an AVPlayerItem and replacing AVPlayer:s current item with that works fine. But if I fetch the same file URL with FileManager, AVPlayer won't open the file, even though no errors are generated. I even made a '==' comparison of the file URL:s for debug purposes and it evaluates to true (the same file URL fetched with NSOpenPanel and fetched with FileManager).
I'm fetching the file URL:s of videos in a directory and then populating a NSPopUpButton with the names of the video files. Selecting a video from the NSPopUpButton should load that video into AVPlayer, but this is not working even though the URL:s that I'm passing to AVPlayerItem are correct.
Any ideas?
Sandboxing prevents opening files that have not been opened with NSOpenPanel. Removing the Sandbox slice from project target capabilities by clicking the X, removes this restriction.
As I am creating a program for my own use, removing sandboxing is not a problem. I don't know if and how it affects AppStore eligibility.

IONIC 4: How to call iOs file resource from custom script within app

Here is the situation:
Ionic 4: (cli-5.2.7)
XCode 10.3
We have a custom eReader that lives in our assets folder.
When we want to download an eBook from our server and read it, we download the file and save it using the Native File plugin.
When we start up the reader, we send it the container in which we want the reader to be created and built, and an internal url to the file resource we want to read.
After window.Ionic.WebView.convertFileSrc, the file ref looks like:
ionic://localhost/_app_file_/var/mobile/Containers/Data/Application/[APP-ID]/Library/NoCloud/[BOOK-ID]/vol-001-chapter-006.xhtml
The reader will then build the iframe and send a simple XMHttpRequest to the provided url, then place the resulting html into the iframe.
The problem is that it only works on first load of the eBook from our server
I can see the file being downloaded, and written to its own folder in the apps file system (File.dataDirectory).
When I debug the XMLHttpRequest in our eReader.js, I can see the call being made to the resource and the HTML returning to the request, which is then rendered on the page.
BUT, if I close the app, reopen it, and try to access the already-downloaded book, the XMLHttpRequest in our eReader.js returns nothing.
I can verify that the file exists at the resource location after a reload, but the XMLHttpRequest acts like it cannot find anything at the provided location.
AND, this is only on iOs. Android works as intended.
My questions is, what am I missing? Is this something to do with the iOs permissions or persistence?
Any help would be appreciated.

Read and use a .txt file with my UIwebView

My app already uses a webview to display a website. I have all that working fine. One problem I do have is that the URL I want to use is actually hard coded into my project. I want the project to read all of the listed URL's in the .txt file and for each of them open them in the Safari View Controller(SVC). Like I said before, I have the app working, even the SVC, just with hard coded URL's in there. I would like it to be able to just know if the URL I clicked on is in the .txt file to open it in SVC not the webview. I saw some examples on how to read files, but nothing that incorporated a webview or SVC.

itms-services // action=download-manifest url as local file

I am trying to utilize:
itms-services // action=download-manifest url as local file, is there any standard way of utilizing plist file via that?
I've seen this done in an app that aided with OTA app updating for developers; what I saw in the source code was that a file:// link was used. If you want to do this, you'd have to know the location of the file in the filesystem. Then, you'd use a standard plist file, but in the url key, you'd put file://location/to/your/ipa/file and it would essentially be the same.
Though, that isn't the best way of doing things, because it is an unknown whether the user has the ipa file on their device or not.