I'm testing my app on iPhone 5s, in this app I'm saving audio file using NSData.writeToURL(destinationUrl, atomically: true), but this line does not work.
How destinationUrl is beeing generated:
let documentsUrl = NSURL(fileURLWithPath: NSBundle.mainBundle().resourcePath!)
let fileName = remoteFileUrl.lastPathComponent
let destinationUrl = documentsUrl.URLByAppendingPathComponent(fileName)
While tested in iOS simulator - everythins was OK
How can I fix it?
You must not write anything at runtime into the application bundle.
That will break the code signature of the application.
Related
trying to export a sequence of OBJ from GARAugmentedFaceSession
In the following project, I just added the following code snippet to export OBJs;
https://github.com/google-ar/arcore-ios-sdk/tree/master/Examples/AugmentedFacesExample
Into Ln 236 from FacesViewControllers.swift;
// Added
let mdlMesh = MDLMesh(scnGeometry: faceTextureNode.geometry!)
let asset = MDLAsset()
asset.add(mdlMesh)
do {
let directory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
let obj_url = directory!.appendingPathComponent("test_\(id).obj")
try asset.export(to: obj_url) // iOS14
self.id += 1
}
catch {
print("SCNSceneRendererDelegate:renderer :: Can't write mesh to url")
}
The weird thing is it works fine in iOS13, but crashes in iOS14.
I am using the latest version of ARCore,1.20.0
If I just use ARSCNFaceGeometry, it works fine.
But I want to use GARAugmentedFaceSession for better results.
Any idea why it crashes on iOS14?
On provided screenshot in comments your directory url is nil
I have downloaded a pdf using code below. I am able to find the file in App Data Container, but from app data container my device needs Mac, x code or iTunes etc.
Can I give a distinction path to Documents OR another place to find the pdf in iPhone files? I have an option to open the file with iBook but it is not there.
My code to download the file is here:
func downloadFile(){
let url = "https://www.tutorialspoint.com/swift/swift_tutorial.pdf"
let fileName = "MyFile"
savePdf(urlString: url, fileName: fileName)
}
func savePdf(urlString:String, fileName:String) {
DispatchQueue.main.async {
let url = URL(string: urlString)
let pdfData = try? Data.init(contentsOf: url!)
let resourceDocPath = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
let pdfNameFromUrl = "YourAppName-\(fileName).pdf"
let actualPath = resourceDocPath.appendingPathComponent(pdfNameFromUrl)
do {
try pdfData?.write(to: actualPath, options: .atomic)
print("pdf successfully saved!")
//file is downloaded in app data container, I can find file from x code > devices > MyApp > download Container >This container has the file
} catch {
print("Pdf could not be saved")
}
}
}
Configure your app so that its files appear in the Files app by adding below lines to your Info.plist file.
<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
OR
Just like below using Xcode
Note: Remember that you must be running iOS 11 or above.
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: [:])
}
I ran into this bug that prevents my app from displaying PDF using UIDocumentInteractionController or QLPreviewController: https://forums.developer.apple.com/thread/91835
According to the suggestions, the solution is to copy files to documents or tmp folders and load files from there.
However, this does not work for me. Loading the files from .documentDirectory or NSTemporaryDirectory() produces the same error, but now not only on device, but also in simulator.
Edit:
The following code solved the problem for me:
func copyFiles(fileName: String) -> URL {
let filemgr = FileManager.default
filemgr.delegate = self
let tempDocsFolder = URL.init(fileURLWithPath: NSTemporaryDirectory()).path
// my fileName is in format "file.pdf"
let fileSplit = fileName.components(separatedBy: ".")
let filePath = Bundle.main.path(forResource: fileSplit[0], ofType: fileSplit[1])
let destPath = "\(tempDocsFolder)/\(fileName)"
do {
try? filemgr.copyItem(atPath: filePath!, toPath: destPath)
}
return URL.init(fileURLWithPath: destPath)
}
Then returned URL is then feeded to the UIDocumentInteractionController. The reason it didn't work for me before was because I tried to copy my files to /tmp/documents/, but the files must be copied to the root of the tmp folder: /tmp/ (I have no idea why).
Check for case-sensitivity of resources(File names).
Add any screen shots of the code.
I'm transferring a file from iOS to WatchOS. applicationContext and userInfo transfers all work fine. When I run the following code, nothing is transferred. fileTransfer.transferred == false.
let fileTransfer = WCSession.defaultSession().transferFile(fileurl, metadata: dict)
I can see all of my attempts queued up when I look at:
let transfers = WCSession.defaultSession().outstandingFileTransfers
Is there something I can check that will provide feedback as to why nothing is getting transferred? I'm using simulators. The file is a plist file. I'm accessing it like this:
let fileurl = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("myfile", ofType: "plist")!)
fileurl does return the file path.