File management app without files-app integration? [duplicate] - ios

In my app I am get video from gallery using below method:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
}
But Apple is rejected app and required to include functionality to select video items from Files app.
Here is Apple give me the reason:
We noticed that your app enables users to view and select files, but
it does not include functionality to allow users to view or select
items from the Files app and the user's iCloud documents, as required
by the App Store Review Guidelines.

It seems that they want you to use UIDocumentPickerViewController to enable users to select video files from cloud services as well as from the photo library in accordance with clause 2.5.15
Apple wants their customers to have a good experience with their device and the apps they run on it, so it makes sense for your app to support all relevant iOS features.
You can create a show a document picker to select video files using:
let picker = UIDocumentPickerViewController(documentTypes: ["public.movie"], in: .import)
picker.delegate = self
self.show(picker, sender: self)
You will need to implement some delegate code to handle the picked document. For example, to copy the selected file into your app's documents directory:
extension ViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
if let pickedUrl = urls.first {
let filename = pickedUrl.lastPathComponent
self.filename.text = filename
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
var documentsDirectory = paths[0]
// Apend filename (name+extension) to URL
documentsDirectory.appendPathComponent(filename)
do {
// If file with same name exists remove it (replace file with new one)
if FileManager.default.fileExists(atPath: documentsDirectory.path) {
try FileManager.default.removeItem(atPath: documentsDirectory.path)
}
// Move file from app_id-Inbox to tmp/filename
try FileManager.default.moveItem(atPath: pickedUrl.path, toPath: documentsDirectory.path)
UserDefaults.standard.set(filename, forKey:"filename")
UserDefaults.standard.set(documentsDirectory, forKey:"fileurl")
self.fileURL = documentsDirectory
} catch {
print(error.localizedDescription)
}
}
}
}

I could see the clause in the review guideline
2.5.15 Apps that enable users to view and select files should include items from the Files app and the user’s iCloud documents.
Looks like you should add UIDocumentPickerViewController support as #Paulw11 pointed out

Related

open documents directory of my iOS app in Swift

In my iOS Swift App, I have created files in my App's documents directory through this code:
let localFileName = String("\(fileName).rtf")
let text = String("text text text")
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let fileURL = dir.appendingPathComponent(localFileName)
do {
try text.write(to: fileURL, atomically: false, encoding: .utf8)
}
catch {
}
}
Now I want to Add a link in my app to this directory where file is created so that user can see files. Currently I am doing this by this code:
let importMenu = UIDocumentPickerViewController(documentTypes: [String(kUTTypeRTF)], in: .open)
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
present(importMenu, animated: true, completion: nil)
but this code is for picking documents, not for opening directory, So How I can open my App's Documents directory, not for picking documents, just only for showing documents?
It is not possible to explore/open a directory in iOS app. Apple doesn't provide any api for the same. You need to create it by your own.
What you can do
You need to fetch all the files from the specific directory and list them all in either tableview or collection view.
And when user click on the any file, you can show that in web view or based on the file type you can do any specific operations.
So ultimately you need to explore more about FileManager.
This class contains what you want.

Present native ios Files app a specific folder from my app

I have an app say called MyApp it is primarily wkwebview. It is already integrated to Files app so I can import/export files from my wkwebview to be able to download files to local machine or upload files to the server.
Finally I am trying to build a UIbutton in my app that will allow my users to Jump into Files app in the folder where I am storing all their content. Instead of building a full FileProviderUI into my app, I just want the button to take the user into the Files App navigating to the folder.
When I give the path for UIDocumentInteractionController to be a directory and do a shared open to present it, nothing happens. No error, nothing at at all. I just want the user to be able to Jump into the folder called MyApp inside the Files app.
I thought it will be very simple. Adding a FileProvider extension or FilePRoviderUI extension seems superflous to just jump the user into this folder and let him interact with Files app to do whatever he likes - open/delete/modify document.
I have to assume that the users are not savvy enough to know even if I tell them that files are saved in Files App for them to be able to interact with directly when they are offline!
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// Just jump to the folder forcing Files app to open this
let viewer = UIDocumentInteractionController(documentsUrl)
viewer.delegate = self
viewer.presentPreview(animated: true)
Nothing gets presented to the user and nothing happens. The button tap just quietly fails.
I think I figured this out. There is a specific URL format that will automatically open the Files app it is shareddocuments:// - Here is a simple function that seems to achieve this quiet simply.
func openSharedFilesApp() {
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let sharedurl = documentsUrl.absoluteString.replacingOccurrences(of: "file://", with: "shareddocuments://")
let furl:URL = URL(string: sharedurl)!
UIApplication.shared.open(furl, options: convertToUIApplicationOpenExternalURLOptionsKeyDictionary([:]), completionHandler: nil)
}
Thanks, Vijay. Here it is in Objective-C.
- (void) openSharedFilesApp
{
NSArray<NSString *>* directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // yes = expand tilde
NSURL* url = [NSURL fileURLWithPath:directories.firstObject];
NSString* sharedDocumentPath = [url.absoluteString stringByReplacingOccurrencesOfString:#"file://" withString:#"shareddocuments://"];
NSURL* sharedDocumentURL = [NSURL URLWithString:sharedDocumentPath];
[UIApplication.sharedApplication openURL:sharedDocumentURL options:#{UIApplicationOpenURLOptionUniversalLinksOnly: #(NO)} completionHandler:^(BOOL success) {
}];
}
Adding to #Vijay's answer in iOS 15, Swift 5:
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let sharedurl = documentsUrl.absoluteString.replacingOccurrences(of: "file://", with: "shareddocuments://")
let furl:URL = URL(string: sharedurl)!
if UIApplication.shared.canOpenURL(furl) {
UIApplication.shared.open(furl, options: [:])
}

How to I hide specific file in File App of Device

First of all I know this is a subjective question, not code based, How ever I need to find solution. Please provide any references
I am working on a task in which I saving the files in Device Document directory. Upto here all is working fine. However when I see these files from :
Files App -> App Folder and the files
I can see all those files.
Now I want to hide few of them, How can I achieve these....?
As per your requirement. You need to change some lines of code in saving file.
Firstly, The FileApp shows all the data that save in documentsDirectory of app whether you put code for hide or not. To hide few files you need to make separate path for them.
First, the file you want to show in documentDirectory:
let documentsDirectory = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
The file you don’t want to show with user, you need to create path here:
let libraryDirectory = (FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)).last! as URL
Once you set the library path for saving all your important files. All will be hidden and cannot be accessed by user.
To know more about files, please refer to this link:
https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
Xcode 9 • Swift 4 or Xcode 8 • Swift 3
extension URL {
var isHidden: Bool {
get {
return (try? resourceValues(forKeys: [.isHiddenKey]))?.isHidden == true
}
set {
var resourceValues = URLResourceValues()
resourceValues.isHidden = newValue
do {
try setResourceValues(resourceValues)
} catch {
print("isHidden error:", error)
}
}
}
}
for more reference see chain here Cocoa Swift, get/set hidden flag on files and directories
or
https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/ManagingFIlesandDirectories/ManagingFIlesandDirectories.html

How to access music files stored on the iOS device?

My application is having trouble locating the files that are in the music folder on my iOS device.
If the files are held below as application data (in my playpen), no problem. But when I go looking for the "Music" folder, using the following lines:
dest_dir = try! FileManager.default.url(for: .musicDirectory, in: .allDomainsMask, appropriateFor: nil, create: false)
musicDir = dest_dir
if UIApplication.shared.canOpenURL(musicDir) {
print("found music directory")
}else {
print("did not find music directory")
}
When the code executes the canOpenURL, I get a permissions complaint.
I also tried accessing the user directory (thinking I could then navigate into Music).
Any clues on how an application can access the files and playlists held under the music system folder on the iOS device?
There is no such thing as "the Music folder" in iOS. To access the user's music library, use the Media Player framework.
Example (assuming you have obtained the necessary authorization from the user):
let query = MPMediaQuery()
let result = query.items
Here you go! (Swift 4.2)
Inside a button method or #IBAction button
let mediaItems = MPMediaQuery.songs().items
let mediaCollection = MPMediaItemCollection(items: mediaItems ?? [])
let player = MPMusicPlayerController.systemMusicPlayer
player.setQueue(with: mediaCollection)
player.play()
let picker = MPMediaPickerController(mediaTypes: .anyAudio)
picker.delegate = self
picker.allowsPickingMultipleItems = false
picker.prompt = "Choose a song"
present(picker, animated: true, completion: nil)
iOS doesn’t give you direct access to the Music folder, for security and privacy reasons; you need to use the APIs in the MediaPlayer framework. Assuming you’re trying to play content from the user’s library, take a look at MPMusicPlayerController; you’ll need to provide it some MPMediaItem instances retrieved with an MPMediaQuery, then call methods from the MPMediaPlayback protocol on the controller to make it play the content.

Save image file to app documents directory in Swift. Does the url to App document directory change even when deployed on the device?

Below are the two functions which I use in the picker view controller to save the file in the App documents directory. And save the url in the local DB & display the image using UIImage(contentsOfFile: ImagePathString)
Everything works well as until this point however as soon as i re-run the simulator images does not show up. I believe its because the app directory keeps changing every time its run on the simulator. My question if its the same when deployed on the device?
func getDocumentsDirectory() -> NSString {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
return documentsDirectory
}
//Below function is used in imagePickerController
if let data = UIImagePNGRepresentation(modifiedImage!) {
let filename = getDocumentsDirectory().stringByAppendingPathComponent("\(NSDate()).png")
data.writeToFile(filename, atomically: true)
pathImage = filename
print(pathImage)
}
Yes url to App documents folder may change when the app is deployed on the device.
Refer below link
Technical Note TN2406
.

Resources