Unit Testing With Swift and Obj-C - ios

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 :)

Related

Xcode cocoa pods pod install Sentry causes " Module 'Sentry' not found"

I follow the guide on their home page.
https://docs.sentry.io/platforms/apple/guides/ios/
I call pod deintegrate and then after I call pod install.
The error appears immediately after I call pod install and I have no idea why.
The Library Sentry.Framework is in Link Binary With Libraries under build Phases in my target app.
I have tried looking through SO for similar issues, but none actually solve the issue for me.
I have tried to clean build, added $(inherited) to other linker flags and adding path to framework search path($(PROJECT_DIR/Pods/Sentry) I don't know whether this is the correct way to do it). None of the mentioned solved the issue.
Any leads to what could be wrong would be appreciated.
UPDATE:
platform :ios, '9.0'
use_frameworks!
target 'yourApp' do
pod 'Sentry', :git => 'https://github.com/getsentry/sentry-cocoa.git', :tag => '7.2.2'
end
The above is my Pod File
Instead of using the module import #import mapped by .modulemap, could you add as declarative framework library #import.
Module import:
#import Sentry;
Framework import:
#import <Sentry/Sentry.h>

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

Including a pod inside a framework target: file not found

I'm using framework targets (for better code reuse and IB_Designables), and I've already had a framework target working perfectly. I've decided to move some other classes to a framework target too.
I've set up the pods (just a single one in this case), but whenever I try to include the pod I'm getting not found error.
No change if I try to use the modules approach too:
The problem is that I've already got another framework too, with the same settings (cross checked all the compiler settings/linker flags/build phases etc) and that framework has no issue importing its pods.
Here is my podfile (TUComponents is the working on, TUModels is the failing one):
[...]
target 'TUComponents' do
pod 'AHKNavigationController'
pod 'TTTAttributedLabel'
use_frameworks!
end
target 'TUModels' do
pod 'JSONModel'
use_frameworks!
end
Even weirder; Xcode has no problem code-completing importing the JSONModel/JSONModel.h header (or JSONModel in case of module #import). But when I try to compile, it fails.
What might be wrong with my configuration?
UPDATE: If I give up using frameworks in pods and use regular old static library, and set allow non-modular includes in frameworks to YES, I can build. But I have no idea why I can't build when using Pod frameworks.
Maybe try solution from: https://www.natashatherobot.com/cocoapods-installing-same-pod-multiple-targets/
platform :ios, '9.0'
use_frameworks!
# My other pods
def testing_pods
pod 'JSONModel'
end
target 'TUComponents' do
pod 'AHKNavigationController'
pod 'TTTAttributedLabel'
testing_pods
end
target 'TUModels' do
testing_pods
end
From iOS - Build fails with CocoaPods cannot find header files :
Make sure your Podfile includes link_with on targets missing a config file. Cocoapods only sets the first target by default otherwise. e.g.
platform :osx, '10.7'
pod 'JSONKit', '~> 1.4'
link_with 'Pomo', 'Pomo Dev', 'Pomo Tests'
Restarting Xcode fixed it for me
The following steps worked in my case:
Quit the Xcode n Simulator
Delete Drived data
Again open your project
Clear it

Updating project to CocoaPods 1.0.0 resulting in many errors

I am working on updating some of my libraries to work with CocoaPods 1.0.0.
I have updated my Podfile to use the new DSL syntax and ran pod update which seemed to work as expected.
Problem 1:
I now have a bunch of errors I have never seen before in my project mainly
Ambiguous use of internal linkage declaration XXX defined in multiple modules
Does anyone know what this error means and how to fix it?
Problem 2:
#import <CocoaPodName/Header.h> style imports can no longer find the appropriate header file.
Problem 3:
FBSnapshotTestCase will no longer build and gives the error Use of private header outside of module scope...
All of these problems began as soon as I ran pod update after updating to CocoaPods 1.0.0.
Podfile:
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
target 'App' do
...
target 'Tests' do
inherit! :search_paths
pod 'FBSnapshotTestCase'
end
end
Update:
I was able to get everything to compile by removing use_frameworks! from the Podfile and using pod 'FBSnapshotTestCase/Core.
Ideally I would prefer to be able to use use_frameworks!, but it seems there may be a bug in CocoaPods 1.0.0?

Swift: Cannot use library from Pod file

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

Resources