How to add AFNetworking to XCode app - ios

I'm relatively new to XCode and iOS. My background for understanding how to add libraries/dependencies to a project is in the world of Java and Maven.
I need to add AFNetworking to my project and have been unable to do so successfully.
First, I tried going to my project -> Build Phases -> Link Binary With Libraries, clicking the "+" button, and then search for AFNetworking from the list. It wasn't in the list.
So, I figured I'd have to add it using the "Add Other..." option. Which means, of course, I'd need to download AFNetworking somehow. I'm not sure I was able to download it in the correct form -- it seems all I could get was the AFNetworking project itself, rather than a framework (if I understand correctly, frameworks are analogous to jars? Can you generate a framework by building an iOS project in a certain way?). I downloaded it from their GitHub page. (I also tried their "Getting Started" guide, but it had me using CocoaPods, which I couldn't get to work right and I'd rather avoid it for now.)
I've tried adding project files and folders from AFNetworking just to see if they worked (but of course they don't -- I think I need some sort of .framework file).
Bottom line: I can't seem to find a way to add AFNetworking to my project. How can I do it?

It sounds like you got the first part down, which is downloading it from the GitHub page.
When I follow this tutorial, it shows how they imported AFNetworking into their own sample project (that is, you'll see a "AFNetworking" group in the list of files and folders along the left side of your Xcode window).
Once AFNetworking is building along with the rest of your code in your project, you should be able to call into the various AFNetworking API's from your code. You'll need to make certain to add in the appropriate "#import "AFNetworking.h"" lines into your source code files from which you'll be calling AFNetworking.

Use pods, than you wont have to upload AFNetworking to your github project, and there is no worry about wrong linkers etc.
Use pods: install pods
Instalation AFNetworkign with pods: install AFNetworking

I believe if you download their code you can generate the .a file using the process in this stack overflow answer: (obviously replacing the project they are getting from gihut with the project you found)
How to create framework from Xcode 4 project?

Use this Manager to help you use AFNetwork.

Related

No Such Module like Sqlite in Swift

i was making a project using sqlite in swift. I added Sqlite manually to my project like told in this tutorial
This link under Manual method
So it compiled and worked fine with my mac. But when i transferred it to other one and compiled then it said No Such Module.
But again when i manually added the framework to my xcode project in the other computer, it started working. Can you tell me how can i do it like that it does not need to be added again and again.
It took some time but here is the answer:
1. Need to download the file sqlite.swift-master.zip
Downlod from here
2. Unzip it and drag the SQLite.xcodeproj file into your own project.
3. In your target’s General tab, click the + button under Linked Frameworks and Libraries, add the sqlite framework for ios and in the Embedded Binaries the sqlite.framework.
Here is a nice video that helps:
Youtube video
This would be a great use of git for transferring between computers that way you can avoid issues like this and you will have less chance of losing work that started on one computer but forgot to transfer it to other computer.
Best practice would be to commit on major changes in code or if you are not going to be working on the code for an extend time frame. Or when you know that you need to transfer to another computer.
I use bitbucket because it allows unlimited private repositories. If you are going to do a file level copy you should always copy the full folder.
Change your deployment target of your Project and your Target to IOS 11. That should do it. Also, make sure that the swift_version of all your targets is set to Swift 4.

Adding an Objective-C framework to a Swift XCode project

I know this is a very simple - maybe obvious - question, but I've been struggling with it for a while. I'm working on a SpriteKit project in XCode (using Swift) and I'm trying to add this SKEasing Framework from GitHub.
I've already downloaded and extracted the zip file, and I did everything that 孙博弘 answered including the bridging header (which I made the with the help of this site);
However, the framework doesn't seem to be working. I can't use any of the actions provided by this library. Is there something I'm missing? What am I doing wrong?
You don't add the project to your project.
First open the SKEasing project in xcode by itself. Set the build target to generic iOS device. (With the pop up in the top left corner of the xocde project window.) Then use the Build command (under Product) and when that's done run Archive. There should be a library built now under the products folder (left side of project window). Control click on the library name and select Show in Finder. This will lead you to the library. Copy this file to your project and you should be good to go.
if you use .a, you should use it as follow img.

Adding an Xcode subproject: Shouldn't all the source files get copied?

I am manually adding the SQLite.swift subproject to my project. As the directions indicated, I copied the .xcodeproj file to my project. This allows me see all the source files (unlike this SO question).
Everything seemed like it was working fine. However, I discovered that the source files of that subproject were not copied to my project. They are still in original location where I downloaded them. Is this by design? What if I want to share my project source code with other people in the future? They won't have the subproject source that is necessary for my project to work.
If I do need to copy the subproject source to my project, then how do I do that?
Add it to your filesystem-structure first. For example by pressing "Show in Finder" on the "Chimee"-project which will lead you to the folder it's located in. Then copy the SQLite-project structure in there.
Afterwards you can then drag&drop the xcodeproj into your project which will then still link to its original path, but as it's now inside your project-structure isn't a problem anymore.
I guess that this is by design, because when you try it via Add files to "YourProject" you can select the Copy items if needed-option but it will still not get copied (only with .xcodeproj-files, it works with all other filetypes)
After doing more research, I now feel that using a dependency manager (like CocoaPods or Carthage) is a better option than manually adding the framework to the project.
This will allow easier updating in the future.
Github source code sharing and App Store submission issues have already been considered.
Using Carthage is not too difficult if you follow this excellent guide: Carthage Tutorial: Getting Started
Notes
Delete the framework files that you manually copied in before installing the framework with Carthage.
I will leave #TMob's answer as accepted for now, but I am no longer pursuing that route.

How should i handle a dependency in my static library?

I have written a static library which interfaces with a web service for internal use across multiple projects.
This library uses AFNetworking heavily, but so do the projects which will use the library. If I include AFNetworking in the library.
I get duplicate symbol errors, but if I remove AFNetworking from the library, it won't compile.
How should I address this dependency ?
Edit: My project is in a GIT repository. Whatever I do, other developers should be able to pull the project and have it compile successfully (unless I am wrong and I shouldn't be that way).
The easiest solution is to rename AFNetworking Classes.
Go to each .m file of afnetwork, click on class name, then Edit->Refactor->Rename
.There are around 10 files. It wouldn't take you long.
PS: A good practise is to prepend your library initials, for example if your library name is MyLibrary, you can rename AFNetworking classes like
MyLibAFNetworking... MyLibAFHTTP....
Use Cocoapods, you can add the dependency for AFNetworking and it will automatically add the library and everything will work like a charm. https://cocoapods.org/
Copy the files of AFNetworking project and add them to your project as files of your library. In other words,
Right click on any AFNetworking file in your project and do Show in Finder.
Select all the AFNetworking files.
Drag them to your Library project and then drop them.
When the pop up appears make sure Copy Items if needed is check marked.
Cheers!

Reference code in a separate project in XCode 4.2

I'm working on an iOS app where I use third party libraries. I want to migrate my project to use ARC, but the third party libraries are still using the old memory management. So I want to separate third party code and put it in a separate project without ARC, and then somehow link that project into my iOS-app project, so that they will be built together using the same configuration.
Is this possible to do in a very simple way, or am i better of just turning off ARC for the individual files? (seems very tedious..)
Can I use a workspace? Where one project is my iOS app and the other just contains third party code?
I've played around a bit and googled a lot, but there just doesn't seem to be any simple soultion, or am I wrong?
So I figured it out myself, with a lot of help from different blogs. Something this basic should be more trivial and well documented... But here we go, this is what I did to get a library for AsiHttpRequest:
Create a new iOS project. Select the 'Cocoa Touch Static Library' template. Call it whatever you like. You don't want to tick 'Use automatic reference counting', since AsiHttpRequest does not support it.
Select a location for your library project (will matter later on).
Delete the default .h- and .m-file created by Xcode.
Drag and drop the AsiHttpRequest files into the project
You can add the frameworks that AsiHttpRequest is dependent of, but you will have to add them to your main project anyway, so it is not necessary.
Try to build the project, it should do so without errors.
Open your main project
From finder, drag your library .proj-file into your main project (in Xcode, so that it 'lands' onto the main project file)
The library project should now appear under your main project (still in XCode). It should be expandable and you should be able to see the library project files as well. If it doesn't, try closing all open projects and reopen the main project.
Select the main project, and select target. Under Build Phases - Link Binary With Libraries, click the +-sign.
In the list of frameworks you should see your library project (called something like libname.a). Select that file
The newly added file might appear red in the list of frameworks, don't worry, it works anyway. Guess it's a bug.
Still under target, go to Build Settings
Under Header Search Paths add the relative search path to where the library .h-files are. This is relative to your main projects .proj-file. (For example ../some folder/libproject/)
Hopefully your main project will build without errors and the library project will be built at the same time, using the same configuration as the main project.
I have no idea if this is a good approach or if there is some easier way to do it. However, I like this, since I can use the library project in several projects. And if I want to update the library project, I only have to do it in one place, and the other projects will be updated as well, since they all reference the same project.
Edit1:
I had some problems with library projects using objective c categories. I received unrecognized selector sent to instance errors in runtime when trying to call those methods. This problem was solved by following the answer given here.
Go to build settings of the target in the main project and add -ObjC to the entry called Other Linker Flags
Edit2:
I found this template for creating Universal frameworks. I haven't tried it, but I guess something like this would work as well.

Resources