Can I use UIDocument in a Mac OSX application?

I would like to share a common document format between iOS and OSX. Please note that this is not an MDI application; there will ever be one document to save / load. NSDocument style user interface controls (e.g. Save, Save As, Open, etc.) are not required.

The biggest problem is that there seems to be no single document encoding format that is naturally compatible with OSX and IOS (for now). According to the document-based programming guide for iOS, it seems that a coding / decoding conversion between the NSDocument and UIDocument classes is required. I wish that the universal serialization mechanism is compatible with all devices in the Apple ecosystem. Any thoughts, ideas, tips are welcome in this regard.

Is it possible to use the UIDocument derived class in a mac osx application and the document becomes iOS compatible?

+4
source share
2 answers

No, you cannot use a UIDocument subclass in a Cocoa application, because a UIDocument does not exist in Cocoa.

In NSDocument and UIDocument, you define the format that you will use. So just use them both to use the same format for output, and for input.

It would not be too difficult to use a preprocessor to configure a file pair that implements a subclass of NSDocument when creating for Mac and UIDocument when creating for iOS. This may prevent you from performing inappropriate serialization and deserialization implementations, since you will only have one copy of each and use it on both platforms.

+5
source

Peter's answer above is correct, but I have a suggestion that does not include the C preprocessor (not available in Swift), which is slightly larger than Cocoa-like.

Watch a WWDC 2014 233 session to configure iOS and Mac applications in the same project as separate targets, and then use the categories in the document class to implement common functions:

CommonFunctions.h @interface AppDocument (CommonFunctions) - (void)function; @end 

-

 CommonFunctions.m @implementation AppDocument (CommonFunctions) - (void) function { /// stuff here } @end 

Your AppDocument class would have 2 different classes inheriting from the UI / NSDocument, as needed for each platform / target, and each target would pull categories from a common place.

+1
source

Source: https://habr.com/ru/post/1383844/


All Articles