Swift Import Obj-C Framework - ios

I am having trouble importing a Obj-C Framework into a Swift Project.
Beginning with an empty swift project here is everything I did:
Drag and drop the Tesseract framework into XCode (copy items if needed was checked)
Drag and drop a random .m file into XCode
XCode generated a Bridging Header File for me
Deleted random .m file, leaving bridging header file
Added import statement to the bridging header file
I dont have enough reputation to add pictures but here are links to various parts of my project:
Project File Structure
Project Build Phases
There is a single line of code in the Bridging Header:
#import <TesseractOCR/TesseractOCR.h>
In Build Settings the Obj-C Bridging Header setting is set to:
Swift OCR Test/Swift OCR Test-Bridging-Header.h
In Build Settings the Framework Search Paths setting is set to:
$(inherited) $(PROJECT_DIR)
I receieve a bunch of Apple Mach-O Linker Errors when I build the project. Could someone offer a suggestion about how to get the project set up properly?
Thanks

You need to add libstdc++.dylib to the Linked Frameworks and Libraries in the General tab for your project.

Related

No Such Module 'Ensembles' Error - importing objective-c framework to use in swift project

I am add Ensembles to my Swift project - found here https://github.com/drewmccormack/ensembles. I have had no luck adding iCloud support to my app and syncing data across devices so hoping this will work.
I have followed the following instructions for adding the framework to my app,
In Finder, drag the Ensembles iOS.xcodeproj project from the
Framework directory into your Xcode project.
Select your App's project root in the source list on the left, and then select the App's target.
In the General tab, click the + button in the Linked
Frameworks and Libraries section.
Choose the libensembles.a library and add it.
Select the Build Settings tab. Locate the Other Linker
Flags setting, and add the flag -ObjC.
This is how it looks in my project, I am unsure if i have done this step right.
Select the Build Phases tab.
Open Target Dependencies, and click the + button.
Locate the
Ensembles Resources iOS product, and add that as a dependency.
Open the Ensembles iOS.xcodeproj project in the source list, and open the Products group.
Drag the Ensembles.bundle product into the Copy
Bundle Resources build phase of your app.
Add the following import in your precompiled header file, or in any files using Ensembles.
It is step 10 that I am having problems with. Do I have to create a bridging header or just import the framework into my swift files ?
This is how I am importing within my CoreDataStack.swift file
import UIKit
import CoreData
import Ensembles
class CoreDataStack: NSObject, CDEPersistentStoreEnsembleDelegate {
}
This gives me the error;
No such module 'Ensembles'
I tried creating a bridging header by doing the following;
Add new header file
Import Ensembles
This is how that looks;
#ifndef Header_h
#define Header_h
#import <Ensembles/Ensembles.h>
#endif /* Header_h */
But still no luck, does anybody know where I am going wrong when trying to import the framework to use with my swift project ?
When creating a bridging header you do not need to use import.
However I don't think you may be adding a bridging header correctly, go to, file, new, file, add a objective-C file and a dialog should pop up asking if you want to create a bridging header. Add both files but delete the objective-C file and keep the bridging header.
Then import the ensembles framework to the bridging header like so.
#import <Ensembles/Ensembles.h>
When creating a bridging file successfully you should not need to import the framework in your swift files and it should be available throughout your project. See this post for more information - Connect Objective C framework to Swift iOS 8 app (Parse framework)

Installing the Gracenote SDK (GNSDK) within XCode

I am attempting to get the GNSDK framework working within XCode but I'm having some issues. I dragged the framework file from the example project into my project and adding a bridging header (since the rest of my project is in Swift). The bridging header consists of:
#import <GnSDKObjC/Gn.h>
However, XCode returns that this header file cannot be found. This is what my "frameworks" look like:
Frameworks screenshot
Note that adding or removing the .plist and .pch files does nothing. Any help would be appreciated.
Do you see the framework in the "Link Binary With Libraries" dropdown? Click on your project name (1st item in your Project Navigator). You should be able to see them in the main window. You could try deleting it and adding from here.

Can't use Objective C framework imported within Bridging Header

Usually I am using CocoaPods (with use_frameworks!) in order to manage my frameworks, but this PDF library (FastPdfKit) does not support CocoaPods, so i added it manually in "link binary with libraries" along with its dependencies. After that I let xcode create a bridging header for me (I created a .m file, and xcode created a bridging header automatically).
I imported the header in the bridging file like this #import <FastPdfKit/FastPdfKit.h>, but I can't acces any method/class from my swift files... (Use of undeclared type 'ReaderViewController' where ReaderViewController is a subclass of UIViewController in the framework).
I've also tried importing like this #import 'FastPdfKit.h" still gives the same errors.
To import Objective-C code into Swift from the same framework, go to Build Settings > Packaging and make sure the Defines Module setting for that framework target is set to “Yes”.

Can't include Objective C Files into Project?

I want to include an Objective-C project (https://github.com/soffes/ssziparchive) into my Swift Project so that I can include the SSZipArchive into my project. I need this so I can unzip a file. As included in the instructions on the Github, I included the folder minizip, SSZipArchive.h, and SSZipArchive.m into my project. I have also created a bridging header where I included the following import into my project #import "SSZipArchive.h". However, when I try to type SSZipArchive on Xcode, the autocomplete doesn't occur, leading me to believe that SSZipArchive isn't included properly in my project. Any ideas on how to do so? I have already looked at numerous links on how to include Objective-C projects into Swift and I have found that I simply need to include the corresponding header files for my project to work.
I guess that you haven't set bridging header path properly. It's a very common problem, but easy one to fix.
Go to the Project Settings -> Build Settings -> Search, and search for bridg, and under Objective-C Bridging Header set the path of your bridging header file (carefully inspect it's path in Finder first, since it may be in some sub-directory of your project).
Also make sure that all your included header files have target of your application. To check if they have, click on the header file, open up Utilities from the right side and under Target Membership, make sure the first target is checked.

Add Bridging-Header inside .framework

I want to know if it is possible to add a Bridging-Header.h inside a .framework.
My goal is to have my framework compliance for swift.
I don't have find solution to add bridging header file inside my framework but have find other solution. That is to explain how to create a bridging header file without import Objective-C file.
For people who want to do this be careful when you indicate the path in Objective-C Bridging Header under Swift Compiler - Code Generation to your bridging header file. It is important to to this in your Target build setting and not in your project build setting else you will be have an error.

Resources