Coder Social home page Coder Social logo

ashislaha / frameworkcreationanddistribution Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 73 KB

Basic Concepts about iOS Frameworks : How to create framework ? How to distribute your framework ? How to update your framework after modifying the functionality ?

Swift 24.79% Objective-C 42.17% Ruby 33.04%
ios-framework framework static-library dynamic-library creating-framwork distributing-framework

frameworkcreationanddistribution's Introduction

Framework Creation And Distribution

Let's discuss about how to create a framework and distribute the framwork so that you can import it and use it in other projects.

BASIC CONCEPTS :

The act of linking libraries is a form of code dependency management. When any app is run, its executable code is loaded into memory. Additionally, any code libraries that it depends on are also loaded into memory.

There are two type of linking: static, and dynamic. Both offer different benefits to the developer and should be used according to these benefits.

Statically linked Library/framework :

Unlike dynamic, linking static libraries includes the object file code from the library into the target's binary. This results in a larger size on disk and slower launch times. Because the library's code is added directly to the linked target's binary, it means that to update any code in the library, the linked target would also have to be rebuilt.

Dynamically linked framework :

Dynamic linking is most commonly used on OS X and iOS. When dynamic libraries are linked, none of the library's code is included directly into the linked target. Instead, the libraries are loaded into memory at runtime prior to having symbols getting resolved. Because the code isn't statically linked into the executable binary, there are some benefits from loading at runtime.

At runtime, only a single copy of the library code is shared among all processes who are using it. Thus ii’s reducing the memory usages and improves the performances.

BUILDING FRAMEWORK :-

You need a way to package a static library and it's header into a single component which you can add in your project and use it immediately.

What is a framework ?

A framework is modular and reusable set of code that is used as a building blocks of high level piece of software.

The best reason to use framework: Build once and use infinite number of times.

Let's create a FrameWork:

Just for sake of simplicity I am creating a simple UIView component inside a framework (ALModalStatus) and using in another project (TestFramework).

The "ALModalStatusView" consists of Image, Title and Description.

STEP 1: Create a Cocoa Touch Framework:

let's name it "ALModalStatus".

screen shot 2018-03-05 at 9 21 11 pm

STEP 2: Change the build configuration to release mode of your framework

Product --> Scheme --> Edit Scheme --> Build configuration --> (move from debug to release)

screen shot 2018-03-06 at 8 27 04 am

STEP 3: Create ALModalStatusView with XIB:

import UIKit
public class ALModalStatusView: UIView {

@IBOutlet private weak var imageView: UIImageView!
@IBOutlet private weak var titleLabel: UILabel!
@IBOutlet private weak var descriptionLabel: UILabel!

private let nibName = "ALModalStatusView"
var contentView: UIView!

// MARK: Set up views
public override init(frame: CGRect) {
    super.init(frame: frame)
    setupView()
}

required public init?(coder aDecoder: NSCoder) {
   super.init(coder: aDecoder)
    setupView()
}

private func setupView() {
    let bundle = Bundle(for: type(of: self))
    let nib = UINib(nibName: nibName, bundle: bundle)
    contentView = nib.instantiate(withOwner: self, options: nil).first as! UIView
    addSubview(contentView)
    
    contentView.center = center
    contentView.autoresizingMask = []
    contentView.translatesAutoresizingMaskIntoConstraints = true
    titleLabel.text = ""
    descriptionLabel.text = ""
}

// Allow view to control itself
public override func layoutSubviews() {
    // Rounded corners
    layoutIfNeeded()
    contentView.layer.masksToBounds = true
    contentView.clipsToBounds = true
    contentView.layer.cornerRadius = 10
}

// MARK: update UI
public func set(image: UIImage) {
    imageView.image = image
}

public func set(title: String) {
    titleLabel.text = title
}

public func set(description: String) {
    descriptionLabel.text = description
}

}

build the project.

STEP 4: Create a test Project to use ALModalStatusView.

create a "TestFramework" project.

STEP 5: Add framework in the test project.

Project Inspector --> General --> Embedded Binaries --> click on + button --> Click on "Add other" --> Choose "ALModalStatus.xcodeproj"

Click on "+" button again in Embedded Binaries, you will get options for "ALModalStatus" framework. Add it. Now you can use this framework.

screen shot 2018-03-06 at 8 36 44 am

For ease of use, You can copy the "ALModalStatus.framework" to some other place like Desktop/Documents and use it instead of adding the entire "ALModalStatus.xcodeproj" file.

After adding the framework:

screen shot 2018-03-06 at 8 45 28 am

STEP 6: Use of ALModalStatus framework in test project:

import UIKit
import ALModalStatus

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .black
}

@IBAction func showStatusViewTapped(_ sender: UIButton) {
    
    let modalView = ALModalStatusView(frame: view.bounds)
    modalView.set(image: #imageLiteral(resourceName: "select"))
    modalView.set(title: "Hi I am Header")
    modalView.set(description: "Provide some description")
    view.addSubview(modalView)
    
    DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
        modalView.removeFromSuperview()
    }
  }
}

7: Output:

simulator screen shot - iphone 8 plus - 2018-03-05 at 21 10 11

simulator screen shot - iphone 8 plus - 2018-03-05 at 21 10 14

What's next? Framework distribution using CocoaPods:

frameworkcreationanddistribution's People

Contributors

ashislaha avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.