How to use FCUUID in a Swift project? [duplicate] - ios

Is there a way I can use a CocoaPod written in Objective-C in my Swift project using swift?
Do I just make a bridging header? And if so, can I access the objects, classes, and fields defined by the libraries in the CocoaPod in Swift?

Basic answer to your question is Yes, you can use objective-c code built with CocoaPods.
More important question is "How to use such libs?"
Answer on this question depends on use_frameworks! flag in your Podfile:
Let's imagine that you want use Objective-C pod with name CoolObjectiveCLib.
If your pod file uses use_frameworks! flag:
// Podfile
use_frameworks!
pod 'CoolObjectiveCLib'
Then you don't need add any bridge header files.
Everything that you need is import framework in Swift source file:
// MyClass.swift
import CoolObjectiveCLib
Now you can use all classes that are presented in lib.
If your pod file doesn't use use_frameworks! flag:
// Podfile
pod 'CoolObjectiveCLib'
Then you need create bridging header file and import there all necessary Objective-C headers:
// MyApp-Bridging-Header
#import "CoolObjectiveCLib.h"
Now you can use all classes that are defined in imported headers.

In podFile use the flag use_frameworks!
Inside Xcode in the Pod folder structure in the dependency, you add xxxxxxx-umbrella.h in Support Files.
In your {PROJECT_NAME}-Bridging-Header.h use:
#import "xxxxxxx/xxxxxxx-umbrella.h"
It works for me.

You just need a bridging header and import there what you need.

AND don't forget to add Bridging Header file name to Target -> Build Settings -> Objective-C Bridging Header

Related

Mixing Objective-C files in Swift Pod

We are creating our private Pod in Swift and have to include some legacy code which is written in Objective-C. I was wondering what's the way to include that in our Pod and use it in swift files?
If you're using the latest cocoapods it should just work. Include the Swift files and the Objective C files in the source_files for the spec and it will all compile together. You can't use a bridging header in a pod, but the Swift classes can access anything that's public in the umbrella header.

Bridging-header works, but imports are not working?

I installed a library using CocoaPods, and it all seems to work. I added a bridging-header to my project, which I know works since I use several different libraries in this header. But I just installed and bridged libPhoneNumber-iOS and it seems to work, except...it doesn't. I find the files, it imports them correctly in the header, but I can't use it in swift. It should be NBPhoneNumberUtil, but it doesn't exist.
I have imported them like this in my header:
#import "libPhoneNumber_iOS/NBPhoneNumberUtil.h"
#import "libPhoneNumber_iOS/NBPhoneNumber.h"
and if I type anything differently it will give me an error saying it can't the specified files, so this should be imported correctly. Also if I type NBPhoneNumberUtil in this header-file I can see the object so it works. But in my swift-project the modules doesn't exist. Again, I know my bridging-file works since I use other libraries in this file, and in swift. Any ideas what might be wrong?
Update #1:
So I tried adding import to my swift-file, and it "works".
import libPhoneNumber_iOS/NBPhoneNumberUtil
import libPhoneNumber_iOS/NBPhoneNumber
Except that Xcode complains that this is not a viable syntax, it wants to add a semi colon somewhere. But now I can create the objects I need, but I can't compile since Xcode wants me to fix the errors first. This is so weird. Any ideas?
I resolved the issue now. The issue was that I use use_frameworks! in my pod file so the paths are different. In fact, when you use use_frameworks! you don't need a bridging header, and have to import the files directly in swift. The problem was that I didn't know how to import it, but now I do.
pod file:
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
platform :ios, '8.0'
target 'test' do
pod 'libPhoneNumber-iOS', '~> 0.8'
end
target 'testTests' do
end
In the Xcode project you do not need a bridging header anymore, and simply import the library where you want to use it, like this:
import libPhoneNumber_iOS
Now it should be working. Hope this helps someone else.
The answer is to import them like this in your bridging header:
#import "NBPhoneNumberUtil.h"
#import "NBPhoneNumber.h"
The procedure that I used to test this is written below.
Create a new Xcode project
I used the Single View Application template for Objective-C.
Create Podfile and Install
Podfile:
source 'https://github.com/CocoaPods/Specs.git'
pod 'libPhoneNumber-iOS', '~> 0.8'
Install:
$ pod install
Create a Swift file
At this point a bridging header file was created by Xcode.
I added the imports into the bridging header using:
#import "NBPhoneNumberUtil.h"
#import "NBPhoneNumber.h"
In the Swift file, I wrote:
class Test {
func test() {
let util = NBPhoneNumberUtil()
}
}
The project compiled without errors.

use CocoaPods 0.36.0 in CocoaTouchFramework with Swift

I would like to use CocoaPods in my CocoaTouchFramework, which has Swift classes.
My Podfile looks the following:
platform :ios, '7.0'
inhibit_all_warnings!
link_with 'MyFramwork'
pod "AFNetworking", "2.5.0"
But how do I achieve to include e.g. AFNetworking into my .swift class in the CocoaTouch Framework? There's no briding header so I somehow have to import it directly in my swift class...
AFNetworking is an objective-c library. So you need to have a bridging-header and import the correct headers.
If you want to use a Swift library for networking you should look to Alamofire. It's from the same creator. Put this in your podfile:
pod 'Alamofire', '~> 1.1'
In every Swift file where you want to use it import the library with this line:
import Alamofire
You need to import AFNetworking, just using in your Swift files:
import AFNetworking
Be careful with upper/lower case letters, as autocompletion does not work. Import every pod library you need to use, using its name (i.e., the name of the folder inside the Pods group/directory)
If you want to use AFNetworking pod, try to add an Objective-C class by using File->New->File->Cocoa Touch Class. Xcode will ask you to add bridge header.
In your bridge header you can import AFNetworking such as;
#import <AFNetworking/AFNetworking.h>
If you don't want to use bridge header you should use Alamofire pod.

Adding SocketRocket to a Swift project through Pods

I'm trying to add the SocketRocket framework to my Swift project using pods and I haven't been able to get the import to work on the Swift side.
I added the following entry to the Podfile:
pod 'SocketRocket', '0.2.0'
And ran pod install.
Then added the bridging header with:
#import <ScoketRocket/SRWebSocket.h>
In my ViewController, Xcode doesn't find the header file:
import SRWebSocket
fails. I really hope to get this done through pods instead of manually adding the files to the project.
Thanks.
Is there a typo ?
#import <ScoketRocket/SRWebSocket.h>
ScoketRocket/SRWebSocket.h
You have to import modules by their module name and not their header name:
import SocketRocket
If you use that in your view controller, then you wouldn't even need an import in the bridging header. Module Imports work with CocoaPods since >= 0.36 with frameworks support, which you have explicitly enable by putting the following in your Podfile:
use_frameworks!
You can still use SocketRocket with older versions of CocoaPods and without this directive from Swift, by adding the import statement to the bridging header like you already figured out. If you do that, you don't need a further import statement in your view controller. The bridging header makes imports available for the whole Swift module.

what to do for 3rd party code which was placed in bridging header before integrating pod file

![enter image description here][1]Before using cocoa pods in swift and xCode 6 beta 6 bridging file was used for integrating 3rd party code of obj c. But after making pod file and integrating that 3rd party code library in pod file then what to do for bridging header file and will the pod file library will be available to my code??
It's really not clear what you are asking to be honest. But I will try to answer your question.
The bridging file is for including Objective-C classes that you want to use in your Swift code. It doesn't matter if it's 3rd party code, a Cocoapod, or if you made it yourself.
If you want to access Cocoapod code in your Swift files then just add the Cocoapod as usual (Update Podfile then pod install). Then #import the header file of the pod class that you want to use in your Swift code.
You should then be able to use the pod in your Swift files using Swift syntax.
For example your bridging header would look like:
// My-Bridging-Header.h
#import "AwesomeSauceClassFromAPod.h"

Resources