adding RealmSwift to Cocoa Touch Framework: missing required modules - ios

I try to include RealmSwift into a Cocoa Touch Framework: I dragged RealmSwift.framework into my Xcode project and added it to "Linked Framwork and Libraries".
I also added $(PROJECT_DIR)/MyProject/RealmSwift.framework/Frameworks to the Framework Search Paths but I still get the error:
Missing required modules: Realm.Private, Realm
What am I missing here?

Per Realm's documentation on using Realm Swift's prebuilt dynamic frameworks, you need to ensure that both Realm.framework and RealmSwift.framework are added to your Xcode project, can be found on the framework search path, and are configured to be linked by the targets that use Realm Swift. It's not necessary for RealmSwift.framework/Frameworks to be on the framework search path, only the directory containing RealmSwift.framework.

Related

How to use Obj-C Framework code in Swift Framework?

How to use Obj-C Framework code in Swift Framework?
Is it possible or I need to mix them in a single Framework?
I have code for both Frameworks. I connect them manually without CocoaPods, Carthage or Swift Package Manager. Bridging header cannot be used inside of a Framework, only in App Target. Google says that I need to use modulemap file. In all examples I found they create their own modulemap files and put them near the Swift framework code. I tried to use both my own modulemap file and existing one inside ObjCFramework.framework/Modules. I use them by adding a path to the modulemap file to SWIFT_INCLUDE_PATHS build setting of my Swift Framework. I also tried to #import Obj-C Frameworks Ubrella Header to Swift Frameworks Ubrella Header with no luck. Of course I added the Obj-C Framework target to my Swift Framework target dependencies, the framework itself to Frameworks and Libraries and the path to it to Framework Search Paths. I didn't put anything to Header Search Paths to avoid "Include of non-modular header inside framework module", but that didn't help. CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES build setting doesn't help too. DEFINES_MODULE is set to YES.
Now I'm in endless loop of several errors:
Include of non-modular header inside framework module 'ObjCFramework': '/Users/bond/Library/Developer/Xcode/DerivedData/SwiftFramework-aqidpocynynsvdgtrqwukanwqkxf/Build/Products/Debug-iphoneos/ObjCFramework.framework/Headers/View.h'
Module 'ObjCFramework' was built in directory '/Users/bond/Library/Developer/Xcode/DerivedData/SwiftFramework-aqidpocynynsvdgtrqwukanwqkxf/Build/Products/Debug-iphoneos/ObjCFramework.framework' but now resides in directory '/Users/bond/Library/Developer/Xcode/DerivedData/SwiftFramework-aqidpocynynsvdgtrqwukanwqkxf/Build/Products/Debug-iphoneos/ObjCFramework.framework/Modules'
No such module ObjCFramework
underlying Objective-C module not found
In brief this is described here: https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_objective-c_into_swift, "Import Code Within a Framework Target" chapter.
If the Defines Module setting is turned on in your framework target, modern Xcode puts Modules folder in the framework, with Swift versions of all you ObjC public interfaces, making ObjC stuff available either for external Swift code that links your framework or Swift code of the framework itself.
This does not depend on whether your framework target contains any swift code.
Just add you ObjC header to framework target, mark it as Public in "Target membership", include in framework Umbrella header and build.

I can import my custom framework but I can't use a class in it

I built a swift framework and integrated it into my project.
It was automatically added to the Linked Frameworks and Binaries and I also added it to the Embedded Binaries.
I can call #import myFramework from anywhere in my project files, but when I try to use a class from myFramework xcode says: Use of unresolved identifier ...
I returned to myFramework files to verify the classes are public and they were.
I verified that myFramework path (the actual framework file) is in Framework Search Path.
I started a new swift project and integrated myFramework into it the same way. That worked! I could import the myFramework and use its classes with no problem.
What can be misconfigured in my original project that I can't use myFramework from it?

Creating a custom framework which uses RealmSwift

When using realm in a simple project, I used the following steps from realm's instructions:
Download the latest release of Realm and extract the zip.
Go to your Xcode project’s “General” settings. Drag RealmSwift.framework and Realm.framework from the appropriate
Swift-versioned directory for your project in ios/, osx/, tvos/ or
watchos/ directory to the “Embedded Binaries” section. Make sure Copy
items if needed is selected (except if using Realm on multiple
platforms in your project) and click Finish.
In your unit test target’s “Build Settings”, add the parent path to RealmSwift.framework in the “Framework Search Paths” section.
If using Realm in an iOS, tvOS or watchOS project, create a new “Run Script Phase” in your app’s target’s “Build Phases” and paste the
following snippet in the script text field:
bash "${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}/Realm.framework/strip-frameworks.sh"
I need to create a custom framework so that I can share multiple classes between apps. These classes use the Realm and RealmSwift Framework.
So I created a custom framework and added some classes. For these classes to work however, I need to have a link to the Realm framework within my custom framework.
I'm unsure how this can be done as in step 2, it states to drag the Realm and RealmSwift frameworks into the 'embedded section' of the project's general settings. But with a framework project, there is no embedded section. There is the 'Linked Frameworks and Libraries' section. I dragged the realm and realmSwift frameworks in there instead. But the custom framework will not build and it says that no module could be found for RealmSwift.
I'm unsure in a framework project if the search paths are the same as what they would be with a regular xcode project ( i.e. $(PROJECT_DIR) ) and I'm unsure what level I should expect to see the Realm and RealmSwift frameworks in my project explorer.
It's very annoying working with framework and library, if your framework is not packed into a .framework, then you might do something like this, might work:
Create a folder for your framework, add some source files
Add a .podspec with dependencies is Realm and RealmSwift, other value then you can learn from other pod
Create a normal app project
Install your framework into that app project with cocoapods with :path point to your folder, this will install realm and realmswift along with it
Now you can code your framework with realm normally without hard linking
For cocoapods and framework related stuff, check this tutorial
For framework project, what i usually do is avoid packing third party lib/framework into my .framework, i have 2 solution for this:
Let user does the job, mean you will create completion block and let them save it to realm themselve
Only include headers of the third party lib into your project and hope it will build without source files, this only works with simple third party project that have few headers, probably not Realm

Realm: bcsymbolmap is missing from working copy

I'm trying to include Realm 2.0.3 and RealmSwift 2.0.3 iOS Swift 2.3 as dynamic framework binaries in my project. I find that they take too long to compile.
I'm able to build my project and run it in the simulator just fine, but when I archive I'm receiving this error:
This is how I'm including the frameworks (simply drag and drop into project):
Help!
Per #TiM's suggestion, in the target's Build Phases I made sure that the two dynamic frameworks are added in in these sections:
Link Binary with Library
Embed Frameworks
Dragging and dropping the frameworks in the project does NOT automatically add them to Embed Framworks

Adding frameworks to Xcode project

I have added a framework to xcode project. Under Build phases->Link Binary With Libraries.
The framework consists of a library file (.a file) and a folder "Headers" which includes all the necessary header files for that framework.
Now I am trying to import a header file in the framework to one of my classes.
#import <MySDK/MyHeaderFile.h>
But an error occurs "Symbol not found" while building.
My understanding was that, if we are using framework instead of library file there is no need to add the header files path in "Header Search Path".
But still, I have specified the path to my framework in header search path.
Also I have specified the framework path in Framework search and Library Search path options.
This is first time I am working with frameworks. For libraries I just added the .a files and specified full path to header files in header search path.
What configuration am I missing for adding frameworks?
If it's a static library file and a bunch of headers, it's not a framework. A framework is a specific method of packaging files. On MacOS X static and dynamic frameworks have one structure, while static frameworks on iOS have a different structure.
For a static framework using Xcode 5, your file structure would look like this:
MySDK.framework/
en.lproj/
Headers/
MyHeaderFile.h
Info.plist
MySDK
Where MySDK is the binary archive file (it should not be MySDK.a). If you have a file ending in .a , you have a static library rather than a framework. Building a static framework using Xcode 5 isn't easy but it is also not impossible. Building a static library is much, much easier and trouble free however. It sounds like you already have a static library, so you just have to tell Xcode where to find the library archive and header files using the appropriate search path settings for your project or target.
If/when you DO have a framework, adding it to "Link libraries and frameworks" OR setting "Other linker flags" to "-framework path/to/MySDK.framework" will work fine.
Newer versions of Xcode may support different functionality for building or using frameworks, however linking against them should be largely the same.
MySDK/MyHeaderFile.h : This explains that your library is inside MySDK folder. Check if it exists in same path. Otherwise you'll get "Symbol Not Found" error.

Resources