Can't include Objective C Files into Project? - ios

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.

Related

Install a Library without cocoapods

I am trying to install this library without using Pods. In their git, they explain that it's possible to do it by dragging the .xcodeproj file on the user project:
Yes you can copy it the classes like before. However it's more
comfortable to drag the xcodeproj to the project. It's not recommended
to drag the sources themselves, as this will make updating Chrats in
your project more difficult.
By dragging the project I get an error When I try to import the module:
No such module 'Charts'
I also selected my project -> Build Phases -> Link Binary With Libraries and added the Charts.framework. That didn't solved the problem.
I think to use any of the cocoa pods you have to build a header file as maybe the code that you are going to use might be in objective C and you might be using swift.
Simply create briding header file with named
"projectname-bridging-header.h" at root of your project.
Now got to build setting of project and in that do below changes:
Install objective-c compatibility header : YES
objective-C briding header : set path of your bridging header like
"projectname/briding header file name.h"
once its done import all your objective c file of pods which u want
use in swift app.
Note : if required then set the path as a recursive in the resource headers and Swift compiler search section.
let me know if any issues :)

iOS Framework headers not found in some files of the framework

I am developing a framework for my obj-c iOS app.
I followed this tutorial: https://www.raywenderlich.com/65964/create-a-framework-for-ios
My framework files builds correctly and I can import it into my sample project and it builds correctly. But my project doesn't perform as expected.
When I did deeper into my framework headers, I find my problem.
Certain header files allow me to CMD-Click on the imports to view the corresponding header files in my project.
So for example:
In TestFile.h, I have: #import <Framework/SomeFile.h>, I can CMD-Click on this grand and I get linked to SomeFile.h
In TestFile2.h, I also have: #import <Framework/SomeFile.h>. When I CMD-Click on this, I get the Question Mark symbol indicating that the file isn't found.
In my Framework Project, I have a directory structure like so:
FrameworkProj.xcodeproj
->FrameworkProj
->FrameworkHeaders.h
->Folder
->some files
->some folders
->Folder
I'm wondering if I haven't set a flag in my Build Settings that doesn't recursively link the files to my compiled framework, but I'm wondering why can I see the files in my headers files if this is the case.
Thanks very much!
May seem stupid but I solved this problem with a single line.
I needed to include -all_load in Target > Build Settings > Other Linker Flags

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.

Xcode can't find header file

I am trying to make a framework for my project. Into my framework I added the path of my header files to target>Build Settings>header-search path. After that I added this framework to my project by Build Phases>Link Binary With Libraries.
When I want to import the header file which I included in my framework, I get a .h file not found error. Is what I'm trying to do possible? Or am I missing anything?
I created framework like that;
Opened new project as iOS>Framework&Library>Cocoa Touch Framework
I didn't add any class, i just added header search path and library search path and linker flags. I don't think i did a mistake in this part because we do it in every project but first time i m doing this for framework. Then i pressed run and get my framework from Products.
I opened my project and added framework Build Phases>Link Binary With Libraries. I m able to import header file of framework like #import <myframework/framework.h>
After this i added framework also General>Embedded Binaries. Everything look normal but i cannot add headers to my project which i included to my framework with header search path. I have to use header search path because there is tons of headers, i cannot add all of them to my Xcode.
Make sure you all Public Header appears in Public Section else drag and drop .h file to public
Everything look normal but i cannot add headers to my project which i included to my framework with header search path.
It sounds as though you're expecting all the headers that can be found at the path specified by your header search path will become part of your framework, so that if there's a header named SomeHeader.h in your search path, it will be built into your framework and you'll be able to import it into client projects like:
#import <MyFramework/SomeHeader.h>
But that's not the case at all. If you want your framework to provide SomeHeader.h, you need to add that file to the project and, as Meghs Dhameliya already pointed out, you need to specify SomeHeader.h in the Public Headers portion of the Headers build phase. This will make Xcode copy the header file into the framework so that clients of the framework can import the header file. It's not clear that that's what you really want, though... in comments you wrote:
There is a lot of headers in another path. I have to use header search path unfortunately. Kind of company rule.
So it sounds like all projects in your company specify the same header search path so that they have access to these header files. If that's true, then there's no reason for projects to need to #import them from your framework, but in that case it's not clear what the actual problem is. Or, perhaps you're creating the framework so that client projects can get the headers from your framework instead of having to reference the header search path. In that case, you will need to add those headers to the project and specify them as described above.

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