How to use Realm (installed with Carthage) with a framework in a Swift app? - ios

I'm working on an iOS App and a Watchkit App.
I read a few things regarding best practices and I decided to create a custom framework, as NathashaTheRobot advise here:
https://realm.io/news/architecting-app-apple-watch-natashatherobot/
So I'm trying to use Realm in my framework.
I followed the installation instructions for Carthage:
Add github "realm/realm-cocoa" to your Cartfile.
Run carthage update.
Drag RealmSwift.framework and Realm.framework from the
Carthage/Build/iOS/ directory to the “Linked Frameworks and
Libraries” section of your Xcode project’s “General” settings.
On your application targets’ “Build Phases” settings tab, click the
“+” icon and choose “New Run Script Phase”. Create a Run Script with
the following contents:
/usr/local/bin/carthage copy-frameworks
and add the paths to the frameworks you want to use under “Input
Files”, e.g.:
$(SRCROOT)/Carthage/Build/iOS/Realm.framework
$(SRCROOT)/Carthage/Build/iOS/RealmSwift.framework
Then I added my framework to the Target Membership of both Realm.framework and RealmSwift.framework.
But when I try to build the project, I get this error:
ld: framework not found Realm for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Then I gave a shot to lipo:
$ lipo -info Carthage/Build/iOS/Realm.framework/Realm
Architectures in the fat file: Carthage/Build/iOS/Realm.framework/Realm are: i386 x86_64 armv7 arm64
Do you have any ideas of what I might be doing wrong here? Thank you.
EDIT:
OK so I found the problem and it was totally unrelated to Realm...
It looks like I deleted the Headers and Resources sections of my framework Build Phases somehow (which were and are still empty). I just put them back and everything compiles/works like it should.
Don't be tempted to delete those two

Is it possible that it's your test target that can't find the frameworks? You'll have to add the parent location of the frameworks to the "Frameworks Search Path" section of your unit tests (likely $(SRCROOT)/Carthage/Build/iOS).
Here's a sample project of a Swift framework bundling RealmSwift as a dependency which you might find useful to compare your build settings against: https://static.realm.io/debug/ParentFramework.tgz

Related

Xcode project giving pod file error for FIRAnalyticsConnector.framework . How to solve it?

I'm working on a book project and the project is connected by API . I'm working on the book app which has different libraries imported from the various places.
Recently, i cloned the project from bitbucket in Xcode and tried to run it. The result i got i error as follows:
enter image description here
It shows :
ld: in /Users/ishinfoservices/Documents/vadltaldhambooks/Pods/FirebaseAnalytics/Frameworks/FIRAnalyticsConnector.framework/FIRAnalyticsConnector(FIRAnalyticsConnector_a8eeba373b74508311b8b22b8d3202a6.o), building for iOS Simulator, but linking in object file built for iOS, file '/Users/ishinfoservices/Documents/vadltaldhambooks/Pods/FirebaseAnalytics/Frameworks/FIRAnalyticsConnector.framework/FIRAnalyticsConnector' for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Help needed to fix the error, as the project has lots of mixes of old libraries along with .h and .m files, the swift files with wireframes
According to the image I saw that you are trying run the app on the iOS simulator, then you could try to add the arm64 to the Excluded Architectures on the Build Settings section of your project or target, it would be like:
Take in mind that if you use Cocoa Pods is possible that also you should add the arm64 to the Exclude Architectures into the Pods project
After the above, clean, build and run the project on the iOS simulator, is possible that on your physical device you must remove the arm64 from the Exclude Architectures, it could depend on what you use in your project.
On the other hand, you can try the following: Select the project -> Select the target -> Go to the Build Phases -> Expand the Link Binary with libraries and add all pod libraries (remove if they exist in embedded binaries or Remove the old FrameWorks), after that, clean and build the project

Static library and cocoapods with Xcode 6.1.1 - ld: library not found Error

Not sure it is related to Xcode 6.1.1 or Cocoapods 0.35.0.
I have generated new static iOS library with Xcode 6.1.1.
SUCCESS (without cocoapods)
Generate simulator and Device library
Lipo to both and generate FAT MyLib.a static final lib
Create new View Application project. (I haven't changed any default setting. e.g. Base SDK, Valid Architecture.)
Add MyLib.a and necessary header files to application project manually.
Build the app project and IT WORKS FINE !! NO ERROR NO WARNING.
ERROR
Upload same MyLib.a & headers (generated above) to HTTP direct download server(kind of artifact server).
Generate specs & podfile.
Remove all dependency (lib and header) from app project which i have done manually earlier.
Add pod file and local specs
"Pod install" command pull out MyLib.a and header properly in /Pods folder.
But while building the app project gives me
Ld Build/Products/Debug-iphonesimulator/LibTest.app/LibTest normal x86_64
cd /Users/admin/Cocoapods/Project/Library/TestProject/LibTest
export IPHONEOS_DEPLOYMENT_TARGET=8.1
ld: library not found for -lMyLib
clang: error: linker command failed with exit code 1 (use -v to see invocation)
When i fire "lipo" command on MyLib.a
Architectures in the fat file: /Users/admin/Cocoapods/Project/Library/Libs/Final/MyLib.a are: armv7 armv7s i386 x86_64 arm64
Its not really architecture issue otherwise it should get failed in my first attempt which doesn't have cocoapods. Cocoapods does some build configuration changes and that creates this issue.
Help me to resolve this issue.
After spending 2 days to figure out the exact issue, i found very silly fix. Its basically issue with static library naming convention issue.
If you have static library in artifact/media http server and wanted to integrate with Cococapods, make sure library name should be lib(your name).a along with headers.
In my case i have kept MyLib and it got dowloaded currently while "pod install" but during app building it was failing.
After renaming to libMyLib.a, every thing works for me. :)
Very easy to apply yet difficult to find such solution]
Not sure whether cocoapods has documented this in there any guide or not.

Cannot Archive when a library is included in my own iOS custom framework

I created my own iOS framework by following this tutorial, https://code.google.com/p/ios-static-framework/, which uses a static library template and aggregate target with a custom run script to create a framework.
At first it works fine. After including another library in the framework project creates the error when archive or build for device. I think the problem is with some wrong settings for that library. But I just don't know what to try. I have tried setting some sensible Other Linker Flags from https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/ld.1.html , but no luck. Can anyone help please ;(
What steps will reproduce the problem?
Follow the tutorial, but change the Aggregate script architecture from armv6 armv7 to armv7 armv7s. Here is the part of the aggregate target script I changed. Everything else is the same.
if [[ "$SF_SDK_PLATFORM" = "iphoneos" ]]
then
SF_OTHER_PLATFORM=iphonesimulator
SF_ARCHS=i386
else
SF_OTHER_PLATFORM=iphoneos
SF_ARCHS="armv7 armv7s"
fi
Add an external library to the project, here I use libBlocksKit.a.
Build the framework, success.
In another child project. Include my built framework.
Add -ObjC in the app Target > Build Settings > Other Linker Flags
Archive and get error. Building for device (iPhone5) gives error too. But building for simulator seems to work.
What is the error?
This error, basically "ld: warning: directory not found for option ... ld: lto: could not merge in ... symbol multiply defined!".
ld: warning: directory not found for option '-L/Users/hlung/Dropbox/- Notes/stackoverflow/RealFrameworkApp/RealFrameworkApp/External/BlocksKit'
ld: lto: could not merge in /Users/hlung/Library/Developer/Xcode/DerivedData/RealFrameworkTest-evagqzwzyyolhjenkkjbvzibxppf/Build/Products/Debug-iphonesimulator/RealFrameworkTest.framework/RealFrameworkTest(NSObject+BlockObservation.o) because 'Linking globals named 'OBJC_CLASS_$_BKObserver': symbol multiply defined!', using libLTO version 'LLVM version 3.2svn, from Apple Clang 4.2 (build 425.0.28)' for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If I archive my child project with only one architecture (like armv7), it works. It shows this error with armv7 armv7s architectures ( $(ARCHS_STANDARD_32_BIT) ).
What version of the product are you using? On what operating system?
OS X 10.8.5, XCode 4.6.3
== Update 1 ==
Posted an issue at the tutorial's code.google.com page Update: 2 weeks no answer.
I found a set of useful suggestions from this answer. Update: Doesn't work
I have created a project so you can run and see for yourself here
Linking against a static library from within a framework can create some interesting challenges... It sounds like you may be linking to BlocksKit from both your framework and your application projects.
You should link in only one of those places. Try removing libBlocksKit.a from the Link Libraries build phase of your framework, but leave it in the other project.
You can do like this:
Click on your project (targets)
Click on Build Settings
Under Library Search Paths, delete the paths
I hope it can help you.

XCode linking error to cached library information without being called (Undefined symbols for architecture)

I am attempting to use a static library (.a) for which I also have the header files (no .m files), but when I add the library to my target (without even referencing/importing the library in my project code), I receive the Undefined symbols for architecture armv7 linking error at compile time. The linker is complaining about classes that supposedly no longer exist in the newest version of the library, and may be using cached data? That suspicion could be supported by the finding that if I create a blank project and add the library in the exact same way as I added it to my project, the new project compiles without issue. But on another note, when I grep my file system for the classes/ivars the linker is complaining about, I get matches in my static library file and my xcuserdata workspace folders. So I am wondering if the issue is in my project, the library I am trying to import, or the way in which I am trying to add the library to my project? And also, why do I get linker errors when I am not even importing/calling the library from my code? I am using XCode 4.6.1.
The linking error is as follows:
Undefined symbols for architecture armv7:
"_OBJC_IVAR_$_GenericPopupView.delegateTheme", referenced from:
-[MyListView setDelegateTheme:] in myLib.a(MyListView.o)
-[MyListView tableView:cellForRowAtIndexPath:] in myLib.a(MyListView.o)
"_OBJC_CLASS_$_SampleMarkView", referenced from:
objc-class-ref in myLib.a(MyListView.o)
"_OBJC_CLASS_$_CheckMarkView", referenced from:
objc-class-ref in myLib.a(MyListView.o)
"_OBJC_METACLASS_$_GenericPopupView", referenced from:
_OBJC_METACLASS_$_MyListView in myLib.a(MyListView.o)
"_OBJC_CLASS_$_GenericPopupView", referenced from:
_OBJC_CLASS_$_MyListView in myLib.a(MyListView.o)
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I added the library to my target by performing the following steps:
Right-click on my project
Select 'Add Files to "My Project"'
Locate the .a file through the popup Finder window
Verify the library path is in my Target -> Build Settings -> Library Search Paths (automatically added when I complete step 3)
Add the header directory to Target -> Build Settings -> Header Search Paths
Verify that the library is listed in Target -> Build Phases -> Link Binary With Libraries
Verify that the library is compiled for armv7 via file, otool, & lipo (I heard the library may also include armv6s, but I didn't see armv6 when I used the three commands)
$ file myLib.a
myLib.a: Mach-O universal binary with 2 architectures
myLib.a (for architecture armv7): current ar archive random library
myLib.a (for architecture cputype (12) cpusubtype (11)): current ar archive random library
$ otool -L myLib.a
Archive : myLib.a (architecture cputype (12) cpusubtype (11))
…
$ lipo -info myLib.a
Architectures in the fat file: myLib.a are: armv7 (cputype (12) cpusubtype (11))
Here are things I have tried inbetween each compile attempt without any success:
Clean the project with (apple)+shift+k
Clean the build directory (DerivedData) with (apple)+alt+shift+k
Delete all the DerivedData directories from the command line
Remove and re-add the library and its headers to my project
Add the library without the headers
Put the libraries in a project that is a target dependency instead of in my project directly
Note that I am unable to add the library as a Target Dependency in Build Settings; should I be able to?
I am also unable to add the library to the target's Build Scheme (via XCode menu's Product -> Scheme -> Edit Scheme... -> Build -> +)
Check the minimum iOS version via iOS Deployment Target in Build Settings – both my target and the library are iOS 6.0
Removed all target search paths except those required for my project to build without the new library as shown below (most importantly I removed the DerivedData from the linker path); my overall project build settings (not the target's build settings) contains no other search paths
Header Search Paths
$(BUILT_PRODUCTS_DIR)/usr/local/include
$(SRCROOT)/../MyLibDirectory/headers
Library Search Paths
$(inherited)
$(SRCROOT)/../TargetDependencyDirectory
$(SRCROOT)/../openssl/lib
$(SRCROOT)/../MyLibDirectory
Removed all other instances of the library on my machine (old versions)
Set the user-defined build setting USE_HEADERMAP = NO
Install on a newly imaged machine with a fresh project checkout and repeat all of the above steps
Add –ObjC++ to the other linker flags (-ObjC is already a linker flag)
Use armv6s by itself and with armv6 in the Valid Architecture under Build Settings for my target and the target dependencies
Set Build Active Architecture Only to Yes
Switched between adding the library in a project group vs. at the parent project group level
Add an import and instantiation of an object from the library in my project code
Played with the library's different settings for Location under Identity and Type in the Utilities window on the right side of XCode when the library is selected
Add the library to the target's Build Phases -> Link Binary With Libraries via the + then Add Other...
I have already tried the recommended steps in these and other posts (only the last two of these listed have actual instructions):
Xcode referencing old
framework
Xcode referencing old/removed frameworks, causing multiple interface
declarations
Xcode custom framework - No known method error
Undefined symbols for architecture armv7
I have not tried changing all of my files to .mm (I didn't need to for previous versions of this library), but I have a lot of .m files that I think might break if I change them all...
Any help or succestions are greatly appreciated! I'm really having a hard time figuring out why this new version of the library is giving me problems.
I resolved my own problem, or rather, the issue was with the black box of a library that I was including. From the command line performed the following command in a terminal window:
grep SampleMarkView myLib.a
Which resulted in:
Binary File:myLib.a matches
I talked to the person who created the library and they sent me an updated version that didn't cause the problem - I guess they had old references in their code. So at the very least I hope this post provides a list of various things to try if you are having a library referencing problem!

clang: error: linker command failed with exit code 1, only when testing on device

I was just about to test my app on a device when I ran into this problem, I'm getting this Linker Error.
I've already checked all my compile sources and Build Phases, but there's no sign of my importing things twice.
ld: duplicate symbol _calculateNextSearchPage in /Users/wouter/Sites/test/FastPdfKit.embeddedframework/FastPdfKit.framework/FastPdfKit(FastPdfKit) and /Users/wouter/Sites/test/FastPdfKit.embeddedframework/FastPdfKit.framework/FastPdfKit(FastPdfKit) for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This only happens when testing on a device, not in the simulator.
Alright guys I had the same problem. Seems like I fixed it.
I am using cocapods therefor the described standard procedure can not be properly executed.
Steps to do to properly add FastPDFKit with Cocoapods.
Download FastPDFKit
In your project Add Files to "Your Project"
go to FastPDFKit folder you just have downloaded
locate 1 folder and 1 project file. Press and hold Command key and add these in to your project
FastPdfKit.xcodeproj
FastPdfKit.embeddedframework
(Note: FastPdfKit.embeddedframework is the actual Framework and you
might want to open FastPdfKit.xcodeproj standalone before adding it
to your project, delete FastPdfKit.embeddedframework folder from
your disk and Build FastPdfKit target in the project. You should see
newly created FastPdfKit.embeddedframework folder)
Go to your Project Settings > Your target > Build Phases > Link Binary with Libraries
Make sure FastPdfKit.framework is there.
If not, drag it from the project and put it there.
Clean the project, delete derived data
Add #import <FastPdfKit/FastPdfKit.h> where you need it and you are good to go.
Here is how my Link Binary with Libraries look like
Please let me know if you have any troubles I might've missed something.
Try to delete duplicate files/images which are in target-> Build phase -> Compile files, Copy bundle resources

Resources