Reference a library in Xcode for iPhone App - iphone

I am used to Visual studio, I have class library projects that I reference in my web sites or windows applications. Using class libraries allows me to create common functionalities, utilities once and use them over and over again.
How do I do this with Xcode and iPhone App development?
I am looking for some details on how to create the library, where to stores the files and how to reference in my App. That is if it is possible.
I am very very new to Mac and Xcode.

Creating a Static Library for iPhone App

On the iPhone, you need to create a 'static library' target. This will build your code into a .a file which will then be integrated directly into your iPhone application.

http://db-in.com/blog/2011/07/universal-framework-iphone-ios-2-0/
This way has worked for me. If you need to use xibs or other non-compilable resource files in the framework, look at the embedded framework section.

Related

How to build dynamic lib for iOS with XCode 4.3.2

I am trying to build a dynamic library for iOS 5, but there is no template for this. Can you help me please?
There are no user dynamic library for iOS, at least none that are officially supported. Indeed, an iOS app (which Apple would accept in its App Store) must me comprised of a single binary file (plus resources). This means that you can only link static libraries with your main program.
The iOS SDK supports the creation of native applications that appear on the device’s Home screen only. It does not support the creation of other types of code, such as drivers, frameworks, or dynamic libraries. If you want to integrate code from a framework or dynamic library into your application, you should link that code statically into your application’s executable file when building your project.
(source - "What can you create?" section)
Of course, dynamic linking is supported by iOS; it is only that Apple does not support its use in iOS apps, except for SDK frameworks.
If you are interested in creating a dynamic library anyhow, have a look at this article. The trick is starting with a plain Cocoa (i.e., MacOS) framework bundle, then change the build settings in order to compile against the iOS SDK.

Which project template should I use for an iOS and Cocoa library?

I have a bunch of code in an iOS project (named "MyLibs") I re-use across different apps. I drag the MyLibs project into the workspace of whatever app I'm creating. I don't use unit tests per se, but I have buttons than run through all the tests very easily in the iPhone app.
I'm learning Cocoa and would like to divide up my library into libs I can use on both projects. I was thinking it would be MyCommonLibs (or MyFoundationLibs), MyIOSLibs and MyCocoaLibs.
However, when creating a new project, I must choose between an iOS app or a Cocoa app. It looks as though the iOS Framework and Library -> Cocoa Touch Static Library is appropriate because it links against the Foundation framework. On the other hand, I'd prefer to use an application template if there is no major drawbacks to it.
I need to be able to use MyCommonLibs in both app types, as some of them are useful to both, such as NSArray categories, etc.
Which template should I use for the MyCommonLibs and must I use a Library, Framework (in the Mac OS templates) or can I just use a normal application template (as I've been doing thus far)?
Create an iOS Framework & Library project. Let's call it TestLib.
Add a new target (File > New > Target) of type MAC OS X Framework & Library.
This way, you can compile both an iOS library and a MAC OS X library from the same project.
You can choose which files are included in each target. So if you want to make a class available for both iOS and MAC OS X, you add it to both, and if you want to make it available for only one platform, you can add it to only one lib.
As you can see in the screenshots below, SharedClass is available in both libs, iOSOnlyClass is available for iOS only and MACOnlyClass is available for MAC OS X only.
You can also add targets for unit tests in the same project.
To organize your code, you can put the shared classes in a group, and the classes of each target in a separate group.

Xcode & iPhone - Best way for reusing code within multiple projects?

What is the best way to reuse code within projects?
Let's say I implemented a UI Element and I want it to be used in both my iphone and ipad application without having to copy the code over and have 2 copies of it.
Just create a project, which includes all your shared code in XCode and reference this project in your iPhone and iPad application project. Plain and simple.
For me I would make a static library project which contains the shared code (UI Element in your example) in Xcode.
Then when I need to use the library in the iPhone or iPad app project, then I can just reference the static library project by drag and drop the project to the Project Navigator and configure the correct dependency, library used and header search path. In this way you always have a single source of library source code for easier maintenance and modification.
Certainly you can actually compile the static library into binary and link it to your project, but it just not too flexible when you find bugs in your static library and need to switch to another project to correct the bug, and then do the compile and copy of the binary library file.
I have just wrote an article (link below) on how to link up the static library project to an iOS project on Xcode 4.3.2. This maybe useful for you to solve the header not found problem you encountered. Hope this help.
http://vicidi.wordpress.com/2012/04/11/linking-an-ios-static-library-project-in-another-project-in-xcode-4-3-2/

Can I develop my own objective-C Framework for Cocoa Touch Applications?

Is it possible to create an own obj-C Cocoa Touch framework which can be used by other developers? And furthermore can you protect this framework?
I've created templates for Xcode 4 that allow you to build universal iOS frameworks (which work in both device and simulator).
Once the templates are installed, you simply select "Static iOS Framework" when creating a new project and it does the rest. It also works with unit tests.
https://github.com/kstenerud/iOS-Universal-Framework
You can create a static library. There is an option in the XCode project chooser to do this. You'll have to distribute the compiled static library file and the header files to users of your library. Your actual implementation files (.m) do not need to be distributed.
GHUnit does a good job of this - packaging up the libraries for both simulator and device - so I recommend looking at this project. (I also recommend using this library for unit testing :-)
The frameworks in Objective C are typically just C / ObjC code and a bunch of classes, nothing amazingly special. As such, you can create your own if you'd like, and then just include that in your project when you build it. The iPhone doesn't care about the difference, it just knows to put all that code into your app, along with everything else.
Have a look at the Framework Programming Guide on Apple's website. It will get you started. Essentially what you'll do is create a Framework project in XCode and then go from there.
As for "protecting" your framework, I assume you mean making your code unreadable. I'm not sure if and how you can do this, but perhaps Apple's guide will say something about it.
Yes you can create frameworks for use with Cocoa Touch.
However there are these caveats:
has to be a statically linked libary, no dynamic loading (dyld) for us
should be a combined (lipo) library for i386 (simulator), arm6 and arm7
you need to hack a bundle project into a framework
you should embed (small and few) images into the library so that the developer does not have to mess around with resources but just drags/drops it into his project
... or if you have large and many images build a bundle with these
I have guides for these things on my site.
1+2 = http://www.drobnik.com/touch/2010/04/universal-static-libraries/
The other links you have to google because this site does not let me post more than one URL.
You could make a static library available as binary (i.e. rudimentary "protection") to third parties, but not a dynamic one, as Apple's App Store policy prevents dynamic linking.
Take a look at a worked example for static libraries given at
this site
If you're going to do it, in my opinion JSON.framework is a great example to follow. To hide/obfuscate the source code is a different story, and a different question entirely,
When creating a new project, navigate to iOS > Framework & Library > Cocoa Touch Framework, it's as simple as that. When you are successfully compile, .framework will be created under Products folder in XCode. Right click to show in Finder, and you can find the output.
It's unlikely this will work the way you want it to because the other developers won't be able to use your framework. This StackOverflow Question explains why.

How to develop distributable UI controls for iPhone?

I would like to develop a reusable UI control for iPhone. How should I go about doing this? When I say reusable I mean it's packaged in a dll (or whatever is used on iPhone platform) so it can be reused on multiple projects.
While dynamic libraries are not allowed to be used in iPhone applications that are to be sold on the iPhone App Store, here's a tutorial on building static libraries with the iPhone SDK. (assuming you don't want to release source code)
You have two options:
Supply the full source code. Other developers can then add it directly to their app.
Distribute the compiled version of your code as a static library.
As you're planning to develop a UI control, I suggest you also develop an IB plugin and ship that with it too.
You probably want to provide custom Interface Builder objects, or maybe source code libraries.
Well, the Xcode way would be to bundle your code into a Framework and link to that. However, under the iPhone you can't link to non Apple approved frameworks (even if they are your own)
So you would probably have to link in the source to the reusable code. A good article here