Does anyone know how to override framework resources (mp3 in my case) in an app using said framework? On android its just a case of creating a resource with the same name and file path in the app directory. Haven't been able to find any such way on iOS.
use bundleWithIdentifier to get the frameworks's bundle , then access it like this
let path = frameworkBundle.path(forResource: "Test", ofType: "mp3")
Related
I am writing an iOS app and I am trying to use some data/config files. At first I was storing these files in the application bundle and getting the URL path for them like
let path = Bundle.main.url(forResource: "Dasher", withExtension: "json")
This was working fine while running in the simulator but when I would try to run on an actual device I would get file not found errors as it seems like the build was not including the support files.
I then did try to use the documents directory for this
let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
let fileURL = (dir?.appendingPathComponent(file))!
But it seems every time you build/run you app you get a new application directory so the files I want to read are no longer present.
I feel like I am being obtuse and missing something simple but when I try to look for a solution I just mainly find examples similar to the code above and not how to resolve my use case.
now, I have DB Data being used on Linux and Window.
I'd like to put it in the iPhone, and use them in my iOS Application.
How to put DB in Sandbox area of my iOS Application ?? And where is the path?
Generally you’d use FileManager method url(for:in:appropriateFor:create:) to build a file URL:
let fileURL = try! FileManager.default
.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appendingPathComponent("test.sqlite")
So, if you had a blank database in your bundle, you’d copy that initial copy of the database to the above file URL, and then open it using this URL from that point on. Or if you were programmatically creating the database from scratch, you’d again use this resulting file URL.
But don’t save this absolute path anywhere. Always build it programmatically like above.
Note, historically we used to use the .documentsDirectory for things like this (and you’ll see lots of those sorts of answers lingering about on the web). But nowadays we would use the .applicationSupportDirectory. See iOS Standard Directories: Where Files Reside document or the iOS Storage Best Practices video.
Basically, I have 2 projects running, one is in obj-c and another is in Swift 4 and I want them to access their directory folder in order to exchange their resources in the run-time. Right now my approach is accessing document directory of Swift 4 project via:
FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
so it will generate the path like:
/Users/public1/Library/Developer/CoreSimulator/Devices/22D423BD-C28D-45B7-A976-D9FB02409988/data/Containers/Data/Application/3B15E00A-910B-420E-8047-99F2E6E5013D/Documents
And if I want get the same Documents directory of obj-c project, I will use above path, delete 2 last components path:
/Users/public1/Library/Developer/CoreSimulator/Devices/22D423BD-C28D-45B7-A976-D9FB02409988/data/Containers/Data/Application
then using enumerator.nextObject() to find the flag of obj-c project. It works perfectly fine in simulator but in the device, those code counters a problem is that when running on a device, those folders won't be on the macOS folders but in device folders, so the URLs executes by above code is something like:
/var/mobile/Containers
and I neither can find nor access to that folder so that my enumerator won't work. So is there a way to accessing that folder (using code) or some mechanism to get 2 project folder shared with each other?
=========================================================
EDIT: for using AppGroup I added those code in my Obj-C project:
NSUserDefaults *pathShared = [[NSUserDefaults alloc] nitWithSuiteName:#"com.example.myDomain"];
[pathShared setObject:directoryPath forKey:#"path"];
and in Swift 4 code:
let userDefault = UserDefaults(suiteName: "com.example.myDomain");
let path = userDefault?.object(forKey: "path");
but it didn't seem to work...
A language that you are using to implement application is not a reason. It is just a different applications with different sandboxes. In iOS one application can't get access to files from a sandbox of other application. You can share data between applications by using same AppGroup for the applications, after that you can use shared UserDefaults and write and read files from a shared directory:
<...>
NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName: #"com.example.domain.MyShareExtension"];
[mySharedDefaults setObject:theAccountName forKey:#"lastAccountName"];
<...>
NSFileManager* fileManager = [[NSFileManager defaultManger];
NSURL* url = [fileManager containerURLForSecurityApplicationGroupIdentifier:[self appContainerName];
<...>
On iOS, the sandbox will prevent your app from accessing any other app's data, even if you developed the other app yourself, too. However, you can set up an App Group and entitle both of your apps to access it, and that will allow you to share what you want. The Sharing Data with Your Containing App heading on this page has a good overview on how to do that
I'm playing around with this code example.
What I am trying to do is create a 100% Swift iOS version of this metronome app that allows playing of the MoreCowbell.caf file similar to how they have already done in the macOS example.
It makes logical sense that IF there were some way to reference this MoreCowbell.caf "asset"/"resource" (?) using the URL format, then the above code could work.
Is there a way to do that? If not, then how can we refer to MoreCowbell.caf from code in order get those ones and zeros into the sound buffer?
I have done much googling and looking at apple documentation but and going in circles so thank you for any help!
You can get a URL to files in your Bundle (ie. in your App) by using a method on Bundle (or NSBundle when using Objective-C). It works like this:
let fileURL = Bundle.main.url(forResource: "MoreCowbell", withExtension: "caf")
You can find more information on this in the official documentation.
Also make sure that the file MoreCowbell.caf is included in your Bundle. You do that by setting it's Target Membership
I'm newbee at IOS Developing and just started learning Swift.
In my app I need to store files somewhere...
For example in Android I use assets folder. In IOS I didn't find any way to retrieve folders and files from assets.
Where should I store files to use them when app is running, and how should I get access to them. Also need to get file/folder names
I am not sure you what you mean by "store files", but if you just want to store resource files, like a CSS stylesheet, sound effects etc, you can just drag it into the navigator in Xcode (the left panel). In my project here, I've added a bunch of mp3 files:
To access these files, you can use the Bundle class. It will give you the path to a specific file in string or URL.
For example, if you want to store the text in a text file named text.txt in a string variable:
let contentsOfFile = String(contentsOfFile:
Bundle.main.path(forResource: "text", ofType: "txt")!, encoding: .utf8)
use Bundle. In this way, the file will be packaged into the App
use Sandbox. This way the file is stored on the local disk