Swift: Cannot use library from Pod file - ios

I'm using Typhoon library for Dependency Injection Framework. I use CocoaPod for installing this library. Here is my pod file:
target "typhoon-swift-demo" do
pod 'Typhoon'
end
target "typhoon-swift-demoTests" do
end
I have installed successfully but when I open workspace project file. I type those line of code as Typhoon sample code:
public class ApplicationAssembly: TyphoonAssembly {
}
I meet error that my application doesn't recognize TyphoonAssembly I have tried to use some lines such as:
import Typhoon // not recogize typhoon
import TyphoonAssembly // not regconize
Please tell me how to fix this problem. What should I add before I can use library. Thanks :)

You have to import the Pod header files using Objective-C and not Swift. So you'll be mixing the two languages if you want to use CocoaPods with Swift. Here's a great tutorial on how to accomplish this.

In addition to Quark's answer, The Typhoon Swift example shows how to set up Typhoon for usage with Swift and CocoaPods.
Note that if you're using "application-style" tests, which is the default almost everywhere now, then the test target will already implicitly have the main target's dependencies. Therefore the test target should be declared exclusive. Example:
platform :ios, '7.0'
target :PocketForecast, :exclusive => true do
pod 'Typhoon', :head
pod 'CKUITools'
pod 'ICLoader'
pod 'NGAParallaxMotion'
pod 'NSURL+QueryDictionary'
pod 'OCLogTemplate'
pod 'PaperFold', :git => 'https://github.com/jasperblues/PaperFold-for-iOS.git', :tag => '1.2-no-gesture-recognizers'
end
target :PocketForecastTests do
pod 'Expecta', '~> 0.2.1'
pod 'OCHamcrest'
pod 'OCMockito'
end
inhibit_all_warnings!
If the test target is not declared exclusive, then it will have all of the application's libraries linked twice. This can cause problems in Typhoon's case, as it uses a lot of introspection.
Also note in the sample application, that there is a bridging header, that includes:
#import "Typhoon.h"
Typhoon Swift Example:

I think this needs an update. I have started recently developping Swift applications and, coming from a Java background, searched for a framework like Spring.
The best I found is Typhoon. I didn't find a good introduction for newbies however, so I made a scratch project to try it out.
I integrated Typhoon by:
Installing cocoapods
Creating a basic podfile with a "use_frameworks!" line like here
pod install
Adding the "TyphoonInitialAssemblies" array to my plist file
Create a first assembly and add it to the array in the plist
In the assembly,
import Typhoon
public class MyAssembly:TyphoonAssembly{}
Works like a charm!

You have even met this error.
I fixed by way:
=> remove "platform :ios, 'x.0'"
use_frameworks!
target 'LateManagement' do
pod 'Alamofire'
pod 'SwiftyJSON'
end

Related

Firebase for iOS inside framework

I have a project who needs firebase for login and ...
So I decided to create a project(Cocoa Touch Framework) inside my workspace to handle all firebase operation.
The firebase framework added to main app via Linked Frameworks and Libraries
I also using cocoa pods
My pod file is something like this:
platform :ios, '10.0'
inhibit_all_warnings!
def firebase
# Firebase
pod 'Firebase/Core'
pod 'FirebaseUI/Auth'
pod 'FirebaseUI/Google'
pod 'FirebaseUI/Facebook'
pod 'FirebaseUI/Phone'
pod 'Firebase/Storage'
pod 'FBSDKLoginKit'
end
target 'RKFirebaseModule' do
use_frameworks!
workspace 'Main'
project 'RKFirebaseModule/RKFirebaseModule.xcodeproj'
firebase
end
target 'App' do
use_frameworks!
workspace 'Main'
project 'App.xcodeproj'
rx_swift
rx_cocoa
end
at this point if I run the app I will get this error:
dyld: Library not loaded: #rpath/Bolts.framework/Bolts
Referenced from: /.../RKFirebaseModule
Reason: image not found
So I tried to solve this problem by adding firebase dependencies to the main app (in pod file):
target 'App' do
use_frameworks!
workspace 'Main'
project 'App.xcodeproj'
rx_swift
rx_cocoa
firebase
end
Now I'm getting bunch of error relating to duplicate implementation of classes like this:
Class FIRAIdentifiers is implemented in both /.../RKFirebaseModule.framework/RKFirebaseModule and /.../App.app/App. One of the two will be used. Which one is undefined.
So how can I solve this problems?
Any help or suggestion will be appreciated. tnx
EDIT 1: Similar cases founds here, but non of the mentioned methods works for me.
1.Duplicate symbols when framework target has a static dependency
2.Duplicate classes warnings at runtime when multiple targets are contained in the same project
EDIT 2: The first problem is normal and it is due to this fact that cocoa pods won't bundle the dependencies into the framework, so I have to use same dependencies for main app.
The second problem caused by some of the firebase static framework, so my framework have a copy of firebase static frameworks and main app has a copy too, so the error is expected here.
I have to remove duplicate static frameworks. HOW?
I had a similar issue when I was using a framework I built and this framework was used in projects that used firebase as well. The workaround I came across was through using cocoapods. I built my framework as a static framework using cocoapods (check .podspec below):
s.static_framework = true
s.dependency 'Firebase'
s.dependency 'Firebase/Core'
The example app i was working on imported my framework via cocoapods as well
use_frameworks!
pod 'gameballSDK', :path => "~/Documents/Libraries/gameballSDK"

How to integrate both Objective-C and Swift pods in same project in iOS app

I am doing Objective-C in iOS app. But the problem is I want to add few Objective-C apis into that I added successfully earlier with cocoa pods, But, now I want to add Swift Api through cocoa pods, but the problem getting while installing is following.
[!] Pods written in Swift can only be integrated as frameworks; add use_frameworks! to your Podfile or target to opt into using it. The Swift Pods being used are: apis
But I can't add this manually due to its large api and it contains sub folders.
But, if I remove "#" key from use_frameworks!, its getting installed, but, the old Objective-C apis getting file not found in my project.
Even I have very basic knowledge at installing frameworks/apis through cocoa pods.
Can any one suggest me how to over come this.
use_frameworks! will work with Objective-C pod only if they are dynamic frameworks not static libraries. Most of the popular third party libraries are using dynamic frameworks like AFNetworking, GoogleMaps etc.
Make sure all your Objective-C pods are dynamic frameworks. If you are not sure just create a sample project with cocoapods and use use_frameworks!. Try adding one by one, all the pods and find out which one is the culprit.
I had that problem once, what I did was use use_frameworks! like you mentioned, and then I changed how the Objective-C imports are written.
The command use_frameworks! turns each pod into a Framework, so in your projects the .h and .m files are no longer visible to the import like they would usually.
As a result, instead of using for example #import <AFNetworking/AFNetworking.h>, you do #import AFNetworking;
From my own experience, and maybe it was just a special case for my project. But turning everything into frameworks slowed down the compile time on Xcode and it made my App Package bigger for some reason.
Podfile file:
platform :ios, '10.0'
use_frameworks!
def pods
pod 'Alamofire', '= 4.4'
pod 'SwiftyJSON' '= 3.1.4'
pod 'MBProgressHUD'
end
target 'YourProject' do
pods
end
YourProject-Bridging-Header.h
#import <MBProgressHUD/MBProgressHUD.h>
Build Settings

Cocoapods: no such module 'CorePlot'

I have an Objective-C project.
Most of files are written in Objc
Several custom UIView classes are written in Swift.
Specially one of UIViewController subclass using Core-Plot is written in Objc.
It has no problem, until...
Now, I am trying to create a new UIViewController using Core-Plot written in Swift.
Trying to use import CorePlot to import external framework Core-Plot as described here. But the compiler shows the error "No such module 'CorePlot'"
Here is my Podfile:
platform :ios, '8.0'
target 'Meters' do
# use_frameworks!
# Pods for Meters
pod 'PQFCustomLoaders', '~> 1.1.0'
pod 'CorePlot'
end
I have used pod install and 'pod update'
I tried "Clean and rebuild"
Neither works for me. Please let me know if any other information needed.
You have to build pods as frameworks (uncomment use_frameworks! in the pod file) to create modules. Core Plot will work fine that way but I don't know about the other pod you're using.

Unable to use both Swift and Obj-C libs with Cocoapods

I have just started a new Swift project and I would like to use different libraries. In particular, I would like to use Realm.io, an Obj-C library. But, I would also like to use pure Swift libraries such as Alamofire or Dollar.
I use Cocoapods for managing my dependencies. I use the latest version (0.37.0) and the new use_frameworks! flag. pod install is successful anytime.
Unfortunately, when I try to build my project I get two errors (for my main target):
Umbrella header Realm.h not found from module.modulemap
Could not build Objective-C module Realm from any file using import Realm
Other imports work fine.
I have noticed the following: if I remove pure Swift libs and use_frameworks, everything works fine. I am aware about this current issue from Cocoapods. However, it should not be a problem for Realm asks developers to use that flag.
Here is my Podfile:
platform :ios, '8.0'
use_frameworks!
target 'rothrock' do
pod 'Realm'
pod 'Cent'
pod 'SwiftyJSON'
pod 'Alamofire'
end
target 'rothrockTests', :exclusive => true do
end
I use no bridging header. Should I?
Any idea or workaround?
Alright, here is the full walkthrough:
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.
You need a bridging header and import your Objective-C library headers there.
If you are using only Realm you can check out this documentation for Swift http://realm.io/docs/cocoa/ (go to CocoaPods down in the tabs)
Swift
Install CocoaPods 0.36.0 or later ([sudo] gem install cocoapods).
In your Podfile, add use_frameworks! and pod 'Realm' to your app target.
From the command line, run pod install.
Use the .xcworkspace file generated by CocoaPods to work on your project!
Download the latest release of Realm and extract the zip.
Drag the file at Swift/RLMSupport.swift into the File Navigator of your Xcode project, checking the Copy items if needed checkbox.
I just installed the Realm library in a project I have with some of the libraries you mention above like Alamofire and SwiftyJSON and others and it works fine when you build the project and even put the import Realm, no compilation errors at all.
I'm using Cocoapods 0.36.0, the stable version and this is my PodFile :
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
platform :ios, '8.0'
link_with 'ApiWorkflow', 'ApiWorkflowTests'
pod 'SwiftyJSON', '~> 2.2'
pod 'Alamofire', '~> 1.2'
pod 'Typhoon', '~> 3.0'
pod 'SwiftCSV', '~> 0.1'
pod 'Realm'
I hope this help you

Unit Testing With Swift and Obj-C

I'm starting a new Swift project and I'm trying to create unit tests for it. I added the Google Analytics framework to the project and linked SystemConfiguration, CoreData, libsqlite3, libz, and libGoogleAnalyticsServices.
I then had to manually create a bridging header and added the GA headers I was going to use immediately. The app was up and running and posting to GA. I then tried to add some unit tests.
Once this happens I receive an error in my bridging header that 'GAI.h' file not found from the test target if I add a bridging header to it. I also receive a Segmentation Fault 11 error from the compiler. The error remains the same without a bridging header.
I have tried linking my test target with SystemConfiguration, CoreData, libsqlite3, libz, and libGoogleAnalyticsServices. This does not get rid of the error.
There is not much to my bridging header at the moment.
#import "GAI.h"
#import "GAILogger.h"
#import "GAITrackedViewController.h"
#import "GAIFields.h"
I'm also using cocoapods but I'm not using it with Google Analytics at the moment since there was so much I needed to manually change in the config files every time I would run the pod process. If it helps here is my pod file:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.1'
pod 'JVFloatLabeledTextField'
# Swift Pods
pod 'Alamofire'
pod 'JSONHelper'
target 'example' do
end
target 'exampleTests' do
pod 'Quick', :git => "https://github.com/Quick/Quick"
pod 'Nimble', :git => "https://github.com/Quick/Nimble"
end
I haven't been able to write any tests yet because I'm not able to get passed the linker errors. Any ideas?
As stated in my comment above, I think I experienced the same or a similar issue: my code worked fine when I ran it, but when I tried to run tests, I got a Segfault 11 on trying to instantiate an object that referenced anything from a cocoa pod. I've solved it in my case.
When I had the error, my Podfile looked like this:
pod 'ReactiveCocoa'
target 'MyTests' do
use_frameworks!
pod 'Quick'
pod 'Nimble
end
use_frameworks! was the culprit: because use_frameworks! only applied to the testing target, I ended up linking to ReactiveCocoa statically when building for the normal target, and dynamically in the test target. I was missing some ReactiveCocoa imports that were required only when linking dynamically, but instead of the compiler telling me that it segfaulted.
My Podfile now looks like this:
use_frameworks!
pod 'ReactiveCocoa'
target 'MyTests' do
pod 'Quick'
pod 'Nimble
end
There were a few linking issues to work out but they were easy from there, because when I compiled the main target I got sane errors. Hopefully this helps someone :)

Resources