use CocoaPods 0.36.0 in CocoaTouchFramework with Swift - ios

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.

Related

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

NXOAuth2Client in swift based single view iOS app: how to import the library

I'd like to use NXOAuth2Client In my single view iOS swift app; therefore I'added the library to my project using CocoaPods. I open the workspace and the project builds successfully.
the tutorial I'm following in in objective-c and, at some point, in AppDelegate.m they:
#import "NXOAuth2.h"
but when I'm trying to do the same in my AppDelegate.swift I always get No such module ; for example if I try:
import NXOAuth2Client
I get:
No such module NXOAuth2Client
( the same if I try: import NXOAuth2 )
I looked at Swift and Objective-C in the Same Project but I'm not experienced enough to figure out what to do in my case.
Here my workspace structure:
Need I to add Bridging-Header.h ? how it works and where I put it ?
you Podfile is wrong. you need to make it use frameworks with swift.
platform :ios, '8.0'
use_frameworks!
target 'MyApp' do
pod 'xyzPod'
end

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.

Missing required module 'CocoaLumberjack' in iOS 8 app / framework

I'm having a problem with integrating a cocoa pod (CocoaLumberjack in this case) into an iOS app and my own frameworks.
The Podfile looks like this:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, "8.0"
target "CommonModule" do
use_frameworks!
# CocoaLumberjack wasn't officially released with Swift support yet
# pod 'CocoaLumberjack'
pod 'CocoaLumberjack', :git => 'git#github.com:CocoaLumberjack/CocoaLumberjack.git', :commit => '6882fb5f03696247e394e8e75551c0fa8a035328'
xcodeproj 'CommonModule/CommonModule.xcodeproj'
end
I have a hierarchy of modules (dynamic frameworks) like this:
CommonModule
ModelsModule (has a dependency CommonModule)
And finally, the main app:
MySwiftApp (dependency both ModelsModule and CommonModule)
Now, CocoaLumberjack is used in several files in CommonModule and works as expected. However, every time I do import CommonModule in any file in ModelsModule, I get the following compile error:
~/Developer/ModelsModule/ModelsModule/SomeFile.swift:2:8: error: missing required module 'CocoaLumberjack'
import CommonModule
^
Any idea how to solve this issue?
UPDATE: Some people Recommend to use Carthage. I would like to avoid that, if possible.
You'll also need to ensure that CommonModule.framework and CocoaLumberjack.framework (and any other frameworks) are listed in the Embedded Binaries section of your application target.
All the new iOS 8-style dynamic frameworks must be embedded within your app—even those that you aren't using directly, but that are dependencies of your dependencies—so you might end up seeing references to items you don't recognize.
Incidentally, there is a new Swift-based logging engine called CleanroomLogger that might make things easier if you're having trouble interacting with CocoaLumberjack from Swift.
I am assuming that CommonModule is swift and that your also using CocoaPods 0.36 as I see your calling use_frameworks!. I'm also assuming that you're using the Obj-C version of CocoaLumberjack, and trying to use it with Swift. That use_frameworks! flag tells CocoaPods to generate frameworks of the pods for linking in your Xcode project. So you need to say at the top of your class
import CocoaLumberjack
instead of using the Swift-Bridging-Header
Here is the blog post on cocoapods.org where they talk about the how to author a pod for the new use_frameworks! flag. Scroll down to the part Common Header Pitfalls
It could also be that your podspec creates a dependency to use CocoaLumberjack, and when linked to your project CocoaLumberjack and CommonModules, but Common Module is not referencing it correctly in the library. To get past that you need to refer to it as a framework when you import it into your Objective-C library
#import <CocoaLumberjack/CocoaLumberjack.h>

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.

Resources