Adding SocketRocket to a Swift project through Pods - ios

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.

Related

Xcode/Swift: How to import a framework without actually including 'import xxx' in file

I've been following a Firebase tutorial for Swift here.
In the project I don't see any explicit 'import FirebaseXXX' within any of the files and was curious how this was being done.
I'm trying to get the same behavior in project but haven't had much luck yet. I have the same Bridging Header in my project, I've tried building/cleaning my project, and I've done 'pod update' and 'pod install' after adding the different pods to my Podfile.
So how exactly is this being done?
Thanks!
If you look at the bridging header (Grocr-Bridging-Header.h), they import the framework there
#import <Firebase/Firebase.h>
Objective-C #imports that happen in the bridging header are global across swift files in the project.
If you are using the swift framework, you'll need to import Firebase at the top of the files to use the framework.
Also see Cannot import Firebase in Swift app

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

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

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.

How can i use the libraries after importing dependencies using cocoapods?

I am facing issues in actually using the libraries contained within the dependancies. I followed the procedure up to the letter but yet xcode gives me errors when i try to import the kit i need to use. Heres what i have done:
create pod file at the project directory
install the pod file with dependancies
open .xcworkspace file created
create a bridging header in objective c
enter path of the bridging header to the build settings
After doing all of this, when i write the import statement (eg. import IMFData) xcode shows that it doesnt exist/ cant be found.
Could somebody shed some light on the what i am missing?
Try adding use_frameworks! to your pod file and then run pod install again. You won't need to use a bridging header then and will be able todo import IMFData alternatively if you aren't using Frameworks when you use the bridging header you don't need to use the import IMFData just access the classes within your swift class.

CocoaPods not building target for Crittercism

I added pod 'CrittercismSDK' to my Podfile and ran pod install, it finished with no error, all good.
Using import Crittercism gives No such module error. I looked into the Pods/ directory, there source code is there; however, the Pods project doesn't have a target called Pods-MyProject-Crittercism (but it does have targets for each dependency).
Build keeps failing because the import isn't found. What am I doing wrong?
PS: I'm using use_frameworks! directive in my Podfile, and I have another obj-c library that works fine, so I don't know why this one isn't working.
While this is not a general answer, I have found that:
Not using #use_frameworks
Using a Bridging-Header.h containing #import "Crittercism.h"
Not importing CrittercismSDK in the Swift class, but merely executing Crittercism.enableWithAppID("appId") does the trick.
See if below steps helps in your case. What version of pod/Xcode in use? It will be great if you can share your pod file, thanks.
Install dependencies using Cocoapods and the use_frameworks! flag.
As you need to use a Objective-C dependency, create a Bridging header. You can create one easily by importing an Objective-C class to your Swift project, than remove it (the wizard should ask you if need a bridging header). Otherwise, create a new header file. Then, navigate to your target configuration and enter the name of your file in Swift Compiler - Code Generation > Objective-C Bridging header.
Still in your target configuration, add a new entry in Search Paths > User Header Search Paths: Pods as value and flag it as recursive.
Remove any import instruction from your code, relative to your Objective-C library.
Build your project. You should have a success.

Resources