Swift Can't Import Sqlite3 iOS - ios

I added libsqlite3.0.dylib to my project, and then I tried to import using the following code:
import UIKit
import sqlite3
class Dataware: NSObject
{
}
But it's giving me this error:
No Such Module 'sqlite3'

Add it to your Bridging-Header.h file:
#import <sqlite3.h>
This is the primary mechanism for importing any C-language libraries.
If you don't yet have a Bridging-Header.h file:
Add a file Bridging-Header.h (or more typically (ProjectName)-Bridging-Header.h
Go to the build settings tab for your project
Find "Objective-C Bridging Header". The easiest way is to search for bridging.
Enter the name and path for the file you created in step one. It's probably (ProjectName)/(ProjectName)-Bridging-Header.h

when one want to add sqlite to framework target, module.map is needed
since sqlite is not mapped, and to do so just:
1. create file in your project 'module/module.map'
2. create the module from the umbrella header:
module sqlite3 [system] {
header "/Applications/Xcode6-Beta5.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.0.sdk/usr/include/sqlite3.h"
link "sqlite3"
export *
}
*change the Xcode6-Beta5.app in the path to right one
3. connect the map file to you project, search for 'import paths' in 'Build Settings'and put the full path to the module file

We need to import the header files for SQLite3 into the view controller so that the compiler can see the function and other definitions that make up the API.
There is no way to directly import the header file into Swift code, because the SQLite3 library is not packaged as a module.
The easiest way to deal with this is to add a bridging header to the project. Once you have a bridging header, you can add other header files to it, and those header files will be read by the Swift compiler. There are a couple of ways to add a bridging file. We’ll use the simpler of the two, which is to temporarily add an Objective-C class to the project. Let’s do that now.
File ➤ New ➤ File.... In the iOS section of the dialog, choose
Cocoa Touch Class and press Next. Name the class Temporary, make it a subclass of NSObject, change the language to Objective-C, and press Next. In the next screen, press the Create button.
When you do this, Xcode will pop up a window asking whether you want to create a bridging header. Press Yes.
Now, in the Project Navigator, you’ll see the files for the new class (Temporary.m and Temporary.h) and the bridging header, which is called SQLite Persistence-Bridging-Header.h. Delete the Temporary.m and Temporary.h files—you don’t need them anymore. Select the bridging header to open it in the editor, and then add the following line to it:
#import < sqlite3.h>
Now that the compiler can see the SQLite3 library and header files, we can write some more code in ViewController.swift
That's it!

Hi Please follow these steps
In xcode 8.3.3 using swift 3
Go to Build Phases tab
Go to Link Binary with Libraries sub tab.
(a) Click + button to add sqlite framework then search for sqlite then you can see libsqlite3.0.tbd and libsqlite3.tbd
(b) Then select only libsqlite3.tbd(Don't add both because the compiler can not find sqlite3 stuct when you declare in viewController)
Then Add Bridging-Header.h file (because sqlite is not written in swift)
Bridging name should be your Projectname-Bridging-Header.h file (Just for naming convention, not mandatory)
Write #import <sqlite3.h> in your Bridging-Header file
Go to build settings tab
(a) Under the build settings tab search for Swift Compiler - General option and set YES to Install Objective-C compatibility Header
(b) Set your name and path for the header file in Objective-C Bridging Header option (Or you can simply drag the bridging header file)

Related

Swift bridging header file won't work with use_frameworks

I'm trying to use GoogleidentityToolkit library to handle login an things. I enable use_frameworks! on my pod file, but the module GITkit can't be found. I'm trying to figure out what's going. As far as I know if you use "use_frameworks" you don't need to create any bridging header file, since cocoapods compiles down the library into a single module, so later you can imported as usual on your*.swift files.
What do I need to get using Google Identity Toolkit library in Swift?
This question was asked one week after the release of CocoaPods 1.0.0 (at a time where CocoaPods 0.39.0 was still popular), and available version of Google Identity Toolkit was 1.1.3 from 2015, but got deprecated in favor of Firebase Authentication (pod 'FirebaseUI/Auth') following Google I/O 2016.
A) Create a Bridging Header file named
"ProjectName-Bridging-Header.h" in the root folder of your project.
B) Go to the project build settings and set the following values:
"Install objective-c compatibility header" : YES
"Objective-C Bridging Header" : path of your bridging header (e.g. "ProjectName/ProjectName-Bridging-Header.h"
After that you can use the header file to import all your ObjectiveC files which you want use within swift code.
NOTE: if required set the path as a recursive both in the resource headers and the Swift compiler search section.
None of the answers above worked for me or weren't precise enough.
In Xcode 11.4 (Swift 5.2) this solution worked for me:
1. Create a new header file in your project's root directory. I'm not sure if the name of the file actually matters, but Apple's auto-generated bridging header files are named "ProjectName-Bridging-Header.h".
2. Add all the imports you need to the newly created file.
3. In Project Navigator click on your project's name.
4. In the topmost bar choose "Build settings", and in the one a bit lower choose All and Combined.
5. Search for "Swift Compiler" in the upper right corner
6. Find "Swift Compiler- General" tab, expand it and double-click the right side of "Objective-C Bridging Header".
7. All you need to do now is just drag the bridging header file you've created into the pop-up window and hit enter. You're all set!
*Remember that you'll have to update the path to your Bridging Header every time you project's direct path changes
The easiest way I've found is to create a fake .swift file within XCode. This should bring up the prompt to automatically create a bridging header.
File > New > File...
For the filetype, choose Swift.
Allow Xcode to manually create the Swift Bridging Header.
Delete the .swift file you originally created.
Add a new file to Xcode (File > New > File), then select “Source” and click “Header File“.
Name your file “YourProjectName-Bridging-Header.h”.
Create the file.
Navigate to your project build settings and find the “Swift Compiler – Code Generation” section. You may find it faster to type in “Swift Compiler” into the search box to narrow down the results. Note: If you don’t have a “Swift Compiler – Code Generation” section, this means you probably don’t have any Swift classes added to your project yet. Add a Swift file, then try again.
Next to “Objective-C Bridging Header” you will need to add the name/path of your header file. If your file resides in your project’s root folder simply put the name of the header file there. Examples: “ProjectName/ProjectName-Bridging-Header.h” or simply “ProjectName-Bridging-Header.h”.Or, simply drag and drop bridging header file from finder to this empty field. This will automatically add the path of bridging header file.
Open up your newly created bridging header and import your Objective-C classes using #import statements. Any class listed in this file will be able to be accessed from your swift classes.
Swift 4 and Xcode 9.3
Create a Bridging Header file:
Xcode> File/New.../File> Header File
Name the file "ProjectName-bridging-header.h"
Save to root of your project folder
Xcode> Go to Build Settings (In the project explorer pane select the top most item, should be your project name and in the right pane select the "Build Settings" topic)
Just below "Build Setting" make sure "All" and "Combined" is selected
In search box type "swift compiler" and find "Objective-C Bridging Header" item
Collapse it and double click to the right of it to edit
Insert the file name of 1. above -> "ProjectName/ProjectName-bridging-header.h" (note the folder path if bridging file is saved in project folder)
Include the #import 's needed
First create briding header file with named "projectname-bridging-header.h" at your project root level.
Now in build settings set your bridging header file path and its objc compatibility header.
Once done, Clean and build your project its work fine.

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)

When I import OBJ-C classes in swift, Xcode doesn't prompt me to add a Bridging-header.h

When I first import an OBJ-C class into a swift project, Xcode doesn't prompt me to add a bridging-header file.
so I fixed in the same way as in Swift Bridging Header and visibility of Obj-C class
But when I want to import another Obj-C class, if I do the same thing again, the file's url will be mixed so that Xcode can't distinguish them.
I tried to make a bridging-header file by myself , but didn't work either.
Help please.
To make bridging header manually first create header file. Name it whatever you want. Then Click your project's target and open Build Settings tab and search "Bridging". You will see "Objective-C Bridging Header" option. Double click it and write your bridging header name like this format TargetName/BrdgingFile.h
ok,NOW I fixed this problem and in addition I figured out how "bridging header"file works.
Actually I followed the method of the question I mentioned above, but what I need to drag is exactly the "bridging-header" file, rather than other implicit header file (because you can only import one header file there.)
So I created a header file called "myproject-bridging-header.h" (actually the name doesn't matter) and dragged it to the item, and in the header file I imported other header files
Bingo! It works smoothly~

Swift Bridging Header and visibility of Obj-C class

I've aded bridging header, specified in build settings the full path to it, bridging header was created automatically. After this, i've included my obj-c header files in it. But every attempt of calling constructor of object fails : "Use of undeclared identifier".
The list of things i've done :
Created .m file and Xcode proposed to create bridging header
Added obj-c files to project and imported them in header
In build setting provided the FULL path to bridging header file
Used Obj-C type in code... But it doesn't builds.
Then, i provided not the full path to the header, but the path from the folder in which project is - no result.
I double-checked all the steps according to apple documentation, but no result.
Why? Any help would be appreciated.
Follow these steps:
Create a Swift project
Add a test class as Cocoa Class instead of .m and .h separately. Xcode prompt add bridging header.
Import test class header in bridging header, which you already did. Should have no issue instantiate test class in Swift.
Copy BL_KeyChainWrapper .m and .h to project directory in finder.
Drag BL_KeyChainWrapper files to project and make sure Add to Targets.
Import BL_KeyChainWrapper header in bridging header.
Instantiate BL_KeyChainWrapper class in Swift.
If followed the above steps, and still have the error. It is probably that you didn't declare a class named BL_KeyChainWrapper in BL_KeyChainWrapper.h. Make sure in your BL_KeyChainWrapper.h, you have code like following:
#interface BL_KeyChainWrapper : BaseClass
Well, after creating a test project with bridging header, I found out the following:
I added .m file, Xcode proposed me to create bridging header.
I added .h file, named it as class and created Obj-C class in this way.
AND:
In build settings - code generation section looks like this:
Bridging file located at the following path:
ProjName / BridgHeader.h
At the same level as .xcodeproj file exists.
BUT:
When I added already created Obj-C class to project and added header import in bridging header, I couldn't use it:
So, I guess, Xcode 6 beta 2 cannot add existing files to swift project. Did anyone face this trouble? Cause I don't want to paste all existing libraries, that I was developing for 5 years to created files.
P.S:
Bridging header:

Swift "Bridging-Header.h" file not allowing me to instantiate objective-c classes in .swift files

When X-code tries to create a bridging header automatically, it crashes every single time, so I followed the instructions on how to manually create a bridging header.
(Create a .h file, name it <#PROJECT_NAME>-Bridging-Header.h, import all the .h files you need?)
Problem is, when I try to instantiate a class in the .swift file that's included in that header, nothing happens (it says that class doesn't exist) Also, in the Bridging Header it doesn't seem to autocomplete my filenames when I try to include them, leading me to believe somethings not linking properly.
Has anyone run into this? Does anyone know how to fix it?
You need to add it to your target's build settings:
In Xcode, if you go into the build settings for your target, and scroll all the way down you'll find a "Swift Compiler - Code Generation" section.
Set "Objective-C Bridging Header" to <#PROJECT_NAME>-Bridging-Header.h
I'm not sure of the correct value for "Install Objective-C Compatibility Header", but it's a yes/no, so you can toggle that if it doesn't work at first.
I tried to create a bridging header myself but for some reason Xcode didn't like it.
So i deleted my custom one, imported an Obj C file which made Xcode ask if I wanted it to create one for me.
I clicked yes, and it worked!
1) create a file called "FMDB-Bridging-Header.h"
inside this file type the following:
#import "FMDB.h"
3) go to Build Settings -> Swift Compiler - Code Generation
- add to 'Objective-C Bridging Header': FMDB-Bridging-Header.h
or if it was placed inside a folder in your project:
FolderName/FMDB-Bridging-Header.h
Add a header file to your project with the name "[your-project-name]-Bridging-Header.h
Go to Build Settings > Build Options and set "Embedded Content Contains Swift Code" to "Yes"
Go to Build Settings > Linking and add "#executable_path/Frameworks" to Runpath Search Paths
Build your project now!
it could help setting the name of the bridging header with its Project root, as "MyProject/MyProject-Bridging-Header.h" into the string value of the Swift Compiler Build key 'Objective-C Bridging Header'

Resources