I'm using the following code snippet in a Mac Catalyst app to "export" (copy) a file from an URL to a user-selected URL:
UIDocumentPickerViewController* documentPicker = [[UIDocumentPickerViewController alloc] initForExportingURLs:#[ [[NSURL alloc] initFileURLWithPath:path] ] asCopy:YES];
This works as expected if a local folder is picked (the file in path is copied there). However, if a network folder is picked (in this case, SMB), a zero-length file is generated and no error is shown.
Does anyone know what might cause this?
I'm looking for cocoa framework that acquires the following information for each application : vendor name, version and full application name.
Alternatively, I could use a file that contain this information ... I've tried to search it in /Application/(name).app/... but I couldn't find it in specific location that is the same for all applications.
It should be in the info.plist under CFBundleName
If you go into <application>/contents/info.plist you'll find it there
from the documentation:
CFBundleName (String - iOS, OS X) identifies the short name of the
bundle. This name should be less than 16 characters long and be
suitable for displaying in the menu bar and the app’s Info window. You
can include this key in the InfoPlist.strings file of an appropriate
.lproj subdirectory to provide localized values for it. If you
localize this key, you should also include the key
CFBundleDisplayName.
here's an example code for acquiring version from Info.plist of a selected application:
string GetAppVersion(string app)
{
NSString *vlcFilePath = [NSString stringWithCString:app.c_str()];
NSDictionary* infoDict = [[NSBundle bundleWithPath:vlcFilePath] infoDictionary];
NSString* version = [infoDict objectForKey:#"CFBundleVersion"];
}
...
...
GetAppVersion("/Applications/Notes.app").c_str());
in my app, I want to load certain image user picks, make some modification, after that, save a copy in my own file format. Since I want to keep track of user modification, I decided to export my file as a package(bundle).
So, in my document class, there's a NSImage object that holds the image file. In the fileWrapperOfType:error: method, I've setup a NSFileWrapper object, and put the object in it. Here's the code:
NSDictionary *fileWrappers = [self.documentFileWrapper fileWrappers];
if (image != nil) {
NSBitmapImageRep *imageRep = [image bitmapImageRepresentation];
NSData *imageData = [imageRep representationUsingType:NSPNGFileType properties:nil];
NSFileWrapper *imageFileWrapper = [[NSFileWrapper alloc]
initRegularFileWithContents:imageData];
[imageFileWrapper setPreferredFilename:#"image"];
[[self documentFileWrapper] addFileWrapper:imageFileWrapper];
}
return self.documentFileWrapper;
In my project plist file, I have two document types, first is the type of public.image since I need to load images in my app:
The other one is my own document type. To make the file a package, I've checked the bundle checkbox in xcode:
If I simply run this now, the code compains that finding extension from type identifier is deprecated, so, I managed to add an entry in Exported UTIs:
At this moment, everything seems working, except that the outputed folder with mdc extension, is indeed a folder instead of a package, what am I missing here? Thanks in advance!
Finally I solved the problem, the key here is to change Conforms To field to com.apple.package.
In order for a package on OS X to be a pkg, it must have a .pkg extension. I hope it is as simple as having the wrong extension.
Note: Until recently, .pkg files were merely directories. Now, we often see package.pkg packages that are obscured cannot be browsed causually without some tricks like using Pacifist, pkgutil or xar:
pkgutil --extract file.pkg folder/
or
xar -xf file.pkg
I have two versions of my model Model001.xcdatamodel and Model002.xcdatamodel. These two are in the Model.xcdatamodeld bundle.
I also have a Model001to002.xcmappingmodel which is not part of the Model.xcdatamodeld. I checked: both the xcmappingmodel and the xcdatamodeld get copied into the .app bundle.
My managed object context is initialized like this:
NSURL *documentModel = [bundle URLForResource:#"Model"
withExtension:#"momd"]; managedObjectModel = [[NSManagedObjectModel alloc]
initWithContentsOfURL:documentModel]; return managedObjectModel;
I also set these properties on my overridden initWithFileURL: in my UIManagedObject subclass.
NSMutableDictionary *options = [NSMutableDictionary dictionaryWithDictionary:self.persistentStoreOptions];
[options setObject:#YES forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setObject:#YES forKey:NSInferMappingModelAutomaticallyOption];
self.persistentStoreOptions = [options copy];
But when I try to open a documet, I get the following error:
Can't find mapping model for migration
-- UPDATE --
Even if I do a manual migration
[NSMappingModel mappingModelFromBundles:#[[NSBundle mainBundle]]
forSourceModel:sourceObjectModel
destinationModel:self.managedObjectModel];
this returns nil. Although I double checked that the Model001to002.cdm is in the app bundle. It has to be in the app bundle right?
A "gotcha" with mapping models is that you are not allowed to make any changes to the models after you created the mapping. If you do, you will also get this error.
OK, solved the problem by removing all core data files from Xcode, reading them and setting the source and destination of the mapping model again.
Damn you Xcode!
You are not allowed to make any changes to the source/destination model after you have created the mapping models.
If you do make some changes,
mappingModelFromBundles:forSourceModel:destinationModel: will not be able to find the mapping model file
addPersistentStoreWithType:configuration:URL:options:error: with {NSInferMappingModelAutomaticallyOption: #NO} will report an error "Can't find mapping model for migration"
migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error: will report an error "Mismatch between mapping and source/destination models"
So, just recreate the mapping model and copy every change you made in the old one.
TL;DR
At least as of Xcode 8/9, open the mapping model then from the Editor menu select Refresh data models. Usually it seems you need to restart Xcode. If that doesn't do it you might try re-selecting the destination at the bottom of the model editor.
More Tips
Definitely NEVER change a model after it has been distributed in an app build.
For this example, let's say you have published Data Model 1 (DM1) and are making a migration to DM2. If you set DM2 as the active version then run your app, a migration will run on your persistent store. If you then make another change to DM2, run your app... Boom!
The issue is that your store has already been migrated to "DM2" but the data in the store doesn't fit into the model anymore. And, we can't migrate from DM2 to DM2 again.
It may seem like an obvious solution to go ahead and create DM3. It is
usually a good idea though to minimize the number of models and
migrations while you are developing.
So... now you have a persistent store that has been migrated to a defunct DM2. How do you test the migration again? You could revert your app and generate some data with DM1 but I prefer to use backups
Creating a backup
Before you run your app with DM2 you can copy the existing store (with DM1) to use for later test migrations. On macOS you can easily do this manually. The code below should do the trick as well. Typically you wouldn't want to ship this, rather you could just put it somewhere before your normal CD stack opens, run the app, then stop the app (maybe place a breakpoint just after then end the run via Xcode).
let fm = FileManager.default
let url = // The store URL you would use in ↓
// try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
let dir = url.deleteLastPathComponent().appendingPathComponent("Backup", isDirectory: true).appendingPathComponent("DM1", isDirectory: true)
print("Saving DB backup for DM1")
if !fm.fileExists(atPath: dir.path) {
do {
// Create a directory
try fm.createDirectory(at: dir, withIntermediateDirectories: true, attributes: nil)
let backupURL = dir.appendingPathComponent(url.lastPathComponent)
try fm.copyItem(at: url, to: backupURL)
}
catch {
print("Failed to save DB backup")
}
}
Oops, I need to make another change...
If you run your migration to DM2 then realize you need to make another change, you'll want to re-test your migration from DM1 -> DM2. This is where the backup comes in.
Same way you made the backup, run this code.
let fm = FileManager.default
let url = // The store URL you would use to add the store
let dir = url.deleteLastPathComponent().appendingPathComponent("Backup", isDirectory: true).appendingPathComponent("DM1", isDirectory: true)
let backupURL = dir.appendingPathComponent(url.lastPathComponent)
if fm.fileExists(atPath: backupURL.path) {
do {
fm.removeItem(at: url.path)
try fm.copyItem(at: backupURL, to: url)
}
catch {
print("Failed to restore DB backup")
}
}
You now have a restored DM1 store and have made changes to DM2. If you run the app the migration might succeed but it won't use your custom mapping model.
Remember if you are using a custom mapping, you will still need to use the Refresh Data Models technique before the mapping model will work.
This can happen if your test device's store is from a version of the data model that no longer exists.
For example I had Data Model Version 7, then I made Data Model Version 8. I made a mapping model to go from 7 to 8. Then I ran it on my test device and everything was happy.
Then I made some more changes to 8.
The thing to realize is that in Core Data, every model has a hash identifier that the system creates by taking a checksum of the xcdatamodel file. So if you make even a slight change, even if you didn't create a new version, it sees it as a different version. These versions' identifiers are NSStoreModelVersionHashes (see documentation here).
So in other words, I ended up with:
Data Model 7 (release) - 0plcXXRN7XHKl5CcF+fwriFmUpON3ZtcI/AfK748aWc=
Data Model 8 (beta) - qeN1Ym3TkWN1G6dU9RfX6Kd2ccEvcDVWHpd3LpLgboI=
Data Model 8 (release) - EqtMzvRnVZWkXwBHu4VeVGy8UyoOe+bi67KC79kphlQ=
Instead of making a version 9, and saving the original version 8 in the data model history, I just updated 8, figuring automatic migration could take care of me. Well, it couldn't, and I couldn't make a mapping between the two, because the old (beta) version of 8 was gone.
I did it that way because it was an intermediary internal build (not a release) so it wasn't a big deal, but it did throw me for a loop!
If it wasn't an internal build and I needed to make this work, I could go back to the (beta) commit and pull out that xcdatamodel file for 8 (beta), rename the (release) version to 9, then stick it into the release build and make a mapping model between 8 and 9.
However since it was just an internal beta build, we just erased and reinstalled the app on test devices. We did verify that, when going from 7 (release) to 8 (release), the migration went smoothly.
Removing Coredata files from its path an re - run project is worked for me
I would like to get the unique identifier of my application that is used as the file name in the IOS file system. How to do that ?
If you are talking about path to your app which contains uniq. identificator then you can extract this id from path of main bundle [[NSBundle mainBundle] bundlePath].