How do I use SwiftyJSON in my iOS App? - ios

I recently added SwiftyJSON into my xCode project using Carthage.
Cartfile
$ cat Cartfile
github "SwiftyJSON/SwiftyJSON" >= 2.1.2
I ran this command to install SwiftyJSON
$ carthage update
I added the built Framework to my xCode project
I imported the Framework at the top of my project
But I still get this error:
Use of unresolved identifier 'JSON'
Seriously, what am I doing wrong? I'm new to iOS development.
UPDATE: I tried removing the Frameworks and re-dragging the frameworks to the Linked Frameworks and Libraries and Adding the "Run Script" section to "Build Phases" as the instructions in Carthage asked.
I'm convinced that something went awry during the Carthage Update process because I received this error on my initial attempt:
$ carthage update
*** Fetching SwiftyJSON
*** Fetching Alamofire
*** Checking out SwiftyJSON at "2.1.3"
*** Checking out Alamofire at "1.1.4"
*** xcodebuild output can be found in /var/folders/0x/swzt630n3_575tglljpwhn4h0000gn/T/carthage-xcodebuild.Pc6rLg.log
*** Building scheme "Alamofire iOS" in Alamofire.xcworkspace
*** Building scheme "Alamofire OSX" in Alamofire.xcworkspace
*** Building scheme "SwiftyJSON" in SwiftyJSON.xcworkspace
** BUILD FAILED **
The following build commands failed:
Check dependencies
(1 failure)
After looking up the problem, I was told that you could run
carthage update --configuration Debug
Which installed it fine, but something is still amiss...

It seems like you missed a step during the addition of SwiftyJSON and Alamofire frameworks from Carthage. Check out these steps from the Carthage readme page :
Steps :
Create a Cartfile that lists the frameworks you’d like to use in your project.
Run carthage update. This will fetch dependencies into a Carthage/Checkouts folder, then build each one.
On your application targets’ “General” settings tab, in the “Linked Frameworks and Libraries” section, drag and drop each framework you want to use from the Carthage/Build folder on disk.
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/Alamofire.framework
$(SRCROOT)/Carthage/Build/iOS/SwiftyJSON.framework
Looks like you missed step 4. I tried these steps myself in a demo Swift project and they are working great. Try these steps out, and if you still have problems, leave a comment. Thanks!

I switched over to Cocoapods and it just worked. Again, I'm a total noob when it comes to iOS development. When looking for a dependency manger I looked at Cocoapods and Carthage for what they had to offer. I was intrigued by Carthage's claim of keeping things simple:
"Carthage because we wanted the simplest tool possible—a dependency manager that gets the job done without taking over the responsibility of Xcode"
While I always will choose the simpler tool when given a choice, I think I don't know enough of the iOS ecosystem (read: Xcode) to use Carthage.

TL,DR: Can't use it inside of Emulator, Use iPhone.
I also followed your way, and I found out that --configuration debug will code-sign "iOS Developer" identity as SwiftyJSON's project property.
AFAIK, Code-signed "iOS Developer" identity wasn't run with Emulator.
My issue was resolved when I run it at my iPhone 6.

I do not know why you can not add this resource with help of Carthage but if you need use SwiftyJSON you only need:
Enter this URL: SwiftyJSON
Clic the button "Clone or download" and then in "Download ZIP" like the next image shows:
Clic the button "Clone or download" and then in "Download ZIP" IMG
You will get a ZIP file with name "SwiftyJSON-master"
Extract the files
Enter the path:
SwiftyJSON-master/Source/
You will see a file with name:
SwiftyJSON.swift
Copy that file into the files of your project.
Add that file into your project directory.
That file will be like other class into your project that you may use.

Related

Firebase Crashlytics | Swift Package Manager (SPM) Run Script?

I want to add Crashlytics to my app using the Swift package manager.
Now, since the usual way to install Firebase is through CocoaPods, the tutorial on how to set up Crashlytics properly is also adjusted for CocoaPods.
In other words - the run script we should copy from the tutorial is not compatible with SPM, as you can see here:
${PODS_ROOT}/FirebaseCrashlytics/run
So how can I find the file location of Crashlytics using SPM to get the correct run script?
Thanks!
Turns out its inside the DerivedData folder:
~/Library/Developer/Xcode/DerivedData/YOURAPP-.../SourcePackages/checkouts/firebase-ios-sdk/Crashlytics/run
For your convenience, you can use a build variable to generalize the folder path:
${BUILD_DIR%Build/*}SourcePackages/checkouts/firebase-ios-sdk/Crashlytics/run
Finally, your run script should look something like that:
And another quite important thing if a non-default name for the firebase configuration file is used.
This flag is needed to denote it:
-gsp ${PROJECT_DIR}/PATH-TO-CONFIG/GoogleService-Info-Dev.plist
Thus final script should look like
${BUILD_DIR%Build/*}SourcePackages/checkouts/firebase-ios-sdk/Crashlytics/run -gsp ${PROJECT_DIR}/PATH-TO-CONFIG/GoogleService-Info-Dev.plist
Where PATH-TO-CONFIG is the path to the firebase configuration file in the project.
Thanks for the suggestions. I'm unable to make this work. I get a crash and the following error message:
/Users/[path-to-my-project]/SourcePackages/checkouts/firebase-ios-sdk/Crashlytics/run: No such file or directory
Command PhaseScriptExecution failed with a nonzero exit code
I don't understand why it tries to find the crashlytics script in my Xcode project folder and not in the derived data folder at runtime.
This worked for me, but I had to change Project Settings.
"${BUILD_DIR%Build/*}/SourcePackages/checkouts/firebase-ios-sdk/Crashlytics/run"
Add a package by selecting File → Add Packages… in Xcode’s menu bar.
Search for the Firebase Apple SDK using the repo's URL:
https://github.com/firebase/firebase-ios-sdk.git
Next, set the Dependency Rule to be Up to Next Major Version and specify 8.10.0 as the lower bound.
Then, select Add Package.
Choose the Firebase products that you want installed in your app.
If you've installed FirebaseAnalytics, add the -ObjC option to Other Linker Flags in the Build Settings tab.

After importing Objective C Framework into my Swift project, my project does not recognise my header file in my Bridging file

I've got to import a Framework into my Swift project that is written in Objective C. So I've created a Bridging file and after I've added the Header files to it and compiled, my project says it cannot find them. The image below shows my Framework and you can clearly see AWController.h but it throws an error in my Bridging file when building.
The Bridging file shows in my Settings so what am I doing wrong or what have I missed? I've set my Defines Module to Yes in Build Settings and my Module Name has no spaces.
I would review the setup for AWS at their website. They even have a section that details using with Swift iOS9.
Among other things, if you are using the Frameworks manual integration, verify that step 4 is complete.
Under the Build Phases tab in your Target, click the + button on the
top left and then select New Run Script Phase. Then setup the build
phase as follows. Make sure this phase is below the Embed Frameworks
phase.:
Shell /bin/sh
bash
"${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}/AWSCore.framework/strip-frameworks.sh"
Show environment variables in build log: Checked Run script only when
installing: Not checked
Input Files: Empty Output Files: Empty
You might want to install and use Carthage to make this easier. Head over to the Homebrew website then after installing homebrew you'd run brew install carthage from the command line. After that go back to the link above and follow the Carthage instructions.
Secondly, I would take another walkthrough of the Apple Documentation on Working with the bridging header files to see if any of the edge cases apply and just as a sanity check.
Third, if that doesn't work, I'd just try creating an empty Swift project and walk through the steps again using one of the techniques above. There might be something wrong with one or more settings in your project files or the file itself might be corrupt.
BTW - The product bundle identifier should probably be following the convention of com.yourdomain.YourProductName.
You should not use bridge header. Just use import AWSDK in the swift file where you use it.

Build failed with Realm and circleCI (with Carthage)

I keep getting linker error when running my project on circleCI saying that realm framework is not found. I have no problem with the framework on my development environment. I have checked the framework search path to include both $(inherited) and carthage build folder ($(PROJECT_DIR)/Carthage/Build/iOS). Below are the content of both my cartfile and circle.yml (circleCI's configuration file)
cartfile
github "Alamofire/Alamofire" ~> 3.4
github "Alamofire/AlamofireImage" ~> 2.0
github "SwiftyJSON/SwiftyJSON" ~> 2.3.0
github "SnapKit/SnapKit" >= 0.15.0
github "realm/realm-cocoa"
circle.yml
machine:
xcode:
version: "7.3"
dependencies:
pre:
- curl -O https://gist.githubusercontent.com/cabeca/cbaacbeb6a1cc4683aa5/raw/8e17f39f5a7413fd0559c9e6808e01b6fb514422/simulator_populator
- ruby ./simulator_populator
- carthage update --platform iOS
The 2 lines before carthage update are suggested by circleCI support tech to avoid issues with tvOS framework. I have also tried switching carthage update to carthage bootstrap with the same result. Below are the error message I get
From the screenshot it seems like the error occurs when it tries to link test files, but both test and uitest files are default created by xcode, I haven't added anything on it yet
but both test and uitest files are default created by xcode, I haven't added anything on it yet
I think that's the problem here. You need to make sure that both targets can find the framework via the build setting FRAMEWORK_SEARCH_PATHS. This should include the parent directory of the relevant frameworks, e.g. $(SRCROOT)/Carthage/Build/iOS. You can achieve that usually by dragging or adding the framework(s) to the "Link Binary with Libraries" build phase.
You need
carthage bootstrap --platform iOS
not
carthage update --platform iOS

RealmSwift (0.92.3) and provisioning profile error

The following error-message occurs when I try to run my watchkit app with my iPhone device (i.e. simulator runs fine) :
dyld: Library not loaded: #rpath/RealmSwift.framework/Frameworks/Realm.framework/Realm
Referenced from: /private/var/mobile/Containers/Bundle/Application/9CCC-B93-89-4E6/MyApp.app/MyApp
Reason: no suitable image found. Did find:
/private/var/mobile/Containers/Bundle/Application/9CC8D-43AE7-F8E/MyApp.app/Frameworks/RealmSwift.framework/Frameworks/Realm.framework/Realm:
code signature invalid for '/private/var/mobile/Containers/Bundle/Application/9C98D-435D-A8E6/MyApp.app/Frameworks/RealmSwift.framework/Frameworks/Realm.framework/Realm'
(lldb)
Any idea what there is to do ??? Any help appreciated !
The solution is to create a new "Run Script Phase" in the app's target as described in the realm.io page:
"If using Realm in an iOS project, create a new “Run Script Phase” in your
app’s target’s “Build Phases” and paste the following snippet in the script
text field:
bash "${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}/RealmSwift.framework/strip-frameworks.sh"
This step is required to work around an App Store submission bug when
archiving universal binaries."
Here is a screenshot on how to find in Xcode-menu and how it eventually looks like !
Kruherson recommends, that you use CocoaPods. Check this post...
Thanks to Kruherson - we know now, that there seems to be a problem later on during app-revision in the app-store with the current solution given from the official realm.io Swift-documentation page !
Here is instruction for CocoaPods installation
Install CocoaPods 0.37.1 or later ([sudo] gem install cocoapods).
In your Podfile, add use_frameworks! and pod 'RealmSwift' to your main and test targets.
From the command line, run pod install.
Use the .xcworkspace file generated by CocoaPods to work on your project!

xcodebuild failure clang:error no such file or directory:

Having a problem when building with xcodebuild. My project/app builds fine with the Xcode - gui. It simply isn't finding/building the libcryptopp library which is part of the build process.
The error is:
clang: error: no such file or directory: '/Users/builder/repo/ioskpay/xcode-cryptopp/cryptopp/build/Release-iphoneos/libcryptopp.a'
This particular file should be derived from another project inside the main app - xcodebuild simply isn't correctly pointing at the right file folder which should be:
~/Library/Developer/Xcode/DerivedData
Any ideas?
To fix this, go to your project settings, go to Targets and select your main project target. Then go to Build phases. Under Target dependencies add the static library project.
This way, when you compile the main project, the static library subproject gets compiled before the main project and your static library will be available.
I had the same problem, but for a resource file .m
I opened target -> build phases -> Compile sources
and I found the file the compiler was tell it can't find duplicated: one with strange icon and the other with a normal icon. I simply removed the one with strange icon and it worked. ( I added the file multiple times and I had a merge conflict before that which made something wrong in the project file)
For your case I think you need to remove the lib from target dependencies list and add it again. This may work for you.
In Xcode Version 9.2 (9C40b) this happened when I drug a bunch of files into the project, some of which were duplicate. Rather than simply not adding the duplicates, it added them again and only the name, not the path.
In Target > Build Phases > Compile Sources each of the duplicates showed with no "...in" after them. Each one caused the clang error.
After removing all of the duplicates that Xcode collected, the project compiled and ran.
It makes you use the Project Navigator instead of managing your source files in the Finder. Then the Project Navigator can't replace duplicates like any decent file management system (ahem Finder). 🤨
My xCode info is:
I faced similar errors during xcode building projects (native swift, flutter, react native, native script) in which I got error messages related to clang compilar. Errors like:
clang-4.0: error: no such file or directory: '/Users/xxxxxxxx/Library/Developer/Xcode/DerivedData/xxxxxxxxxxxxxxx/Index/Data Store'
clang-4.0: error: cannot specify -o when generating multiple output files
others
Despite of errors related with DerivedData for native apps can be fixed by deleting the directory and, eventually, restart xCode and even restart the machine... in this case, you will see that after deleting the directory and start building process again, the error comes back.
Then, is the moment of checking the clang installation by running clang --version. The normal output will be something like:
as you can see the InstalledDir is incorrect for xCode. In my case, some days ago I needed to install Anaconda app (R, Python, etc) and, now, I remember that I had to install some dependencies and one of them was clang and its installation was altered.
To fix this problem (in my case that I will not need anaconda any more): (edited)
1.- Delete anaconda and all its dependencies (I recommend to use App Cleaner).
2.- Re-install xCode
After reinstalling xCode, if you type again clang --version, you'll get this:
More info at: https://github.com/flutter/flutter/issues/32457#issuecomment-496161092
Hopefully, this info helps some else.
Best
Ok so by simply adding the correct -target -configuration and -scheme parameters I got this to run correctly. However due to my running this in Jenkins for autobuild purposes it still doesn't work as I'd like - getting stuck in exactly the same place. It's odd because I have the exact same code being built in another job that isn't having this problem. There is no rhyme or reason for it at this point. I will keep shooting rubber bands at it and update when I have an answer...
Sometimes Xcode performs weird.
You have to find that static library project e.g.. "filename.a" under 'Link Binary With Libraries' in Build Phase and then remove it and add it again.
I had the same problem while I was archiving my target. I removed the library and the build succeeded.
For me it was because I had removed a package or pod. I ran pod install and it fixed it
Look for the missing file in the Xcode project i.e the files may be deleted or miss placed.
add the missing files to the xcode, then everything will work fine.
Run this command :
$ conda deactivate

Resources