ios project architecture

Upload: tdkhoa

Post on 03-Jun-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 IOS project Architecture

    1/67

    Massimo OlivieroFreelance Software Developer

    Building iOS App Project & Architecture

  • 8/12/2019 IOS project Architecture

    2/67

    About

    Massimo OlivieroIT Consultant & Software Developer#pragma mark founder

    email [email protected] web www.massimooliviero.net slide www.slideshare.com/MassimoOliviero twitter @maxoly

    http://www.slideshare.com/MassimoOlivierohttp://www.massimooliviero.net/mailto:[email protected]
  • 8/12/2019 IOS project Architecture

    3/67

    Source

    Kimera A sample iOS application

    https://github.com/maxoly/Kimera

    https://github.com/maxoly/Kimera
  • 8/12/2019 IOS project Architecture

    4/67

    Agenda

    " Project

    " Architecture

    " Resources

  • 8/12/2019 IOS project Architecture

    5/67

    Project

  • 8/12/2019 IOS project Architecture

    6/67

    Project

    " Conventions

    " Structure

    " Folders

  • 8/12/2019 IOS project Architecture

    7/67

    Conventions

  • 8/12/2019 IOS project Architecture

    8/67

    Naming conventions

    " First establish a naming convention for all the things

    for le names, class names, project names, images, etc.

    " Use Pascal Case for les, folders and class

    start with a capital letter i.e. Controllers, MyClass, BestAppEver, etc.

    " Use Camel Case for methods, properties & variables

    start with a lowercase letter i.e setFirstName:, userPassword, etc.

    " Avoid using of acronyms and abbreviations

    What the hell does it mean usrPswdLbl? Yuck!

  • 8/12/2019 IOS project Architecture

    9/67

    Coding conventions

    " Choose your coding conventions & style

    there are ton of conventions out there

    " K&R Style , or Allman Indent Style

    http://en.wikipedia.org/wiki/Indent_style

    " Also read Coding Guidelines for Cocoa by Apple

    http://developer.apple.com/library/mac/#documentation/Cocoa/ Conceptual/CodingGuidelines/CodingGuidelines.html

    " But most important, choose a convention and respect it

    the important thing is always be consistent in your project

    http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.htmlhttp://en.wikipedia.org/wiki/Indent_style
  • 8/12/2019 IOS project Architecture

    10/67

    Coding Guidelines for Cocoa

    Code Commentary

    insertObject:atIndex: Good.

    insert:at:Not clear; what is being inserted? whatdoes at signify?

    removeObjectAtIndex: Good.

    removeObject:Good, because it removes objectreferred to in argument.

    remove: Not clear; what is being removed?

    An extract from Coding Guidelines for Cocoa By Apple

    https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingBasics.html#//apple_ref/doc/uid/20001281-BBCHBFAH
  • 8/12/2019 IOS project Architecture

    11/67

    Structure

  • 8/12/2019 IOS project Architecture

    12/67

    Structure

    " Create a specic workspace

    dont let Xcode do it for you

    " Setting up projects with correct name and prex

    use simple word (only alphanumeric) and at least 3 chars for prex

    " Create a Build Automation to scripting common tasks

    to compiling source code or to deploy artifacts with one command

    " Create a README le with most important info

    you can use markdown syntax or other markup language like that

  • 8/12/2019 IOS project Architecture

    13/67

    Structure

    " Create a AdHoc and AppStore Build Conguration

    So you can handle conguration for di # erent destination

    " Congure Build Settings to improve quality

    i.e. you can enable Static Analyzer or Treat Warnings as Errors

    " Manage third-part libraries with CocoaPods

    it reduces headaches of storing/managing 3rd party libraries

  • 8/12/2019 IOS project Architecture

    14/67

    Workspace

  • 8/12/2019 IOS project Architecture

    15/67

    Project Name & Prex

    " Choose a simple Product Name Only alphanumeric characters, avoidspaces

    " Choose a right Class Prex

    At least 3 chars, use product name'sacronym

    " Choose your Personal Prex

    Use it in your Library Projects

    " Use Automatic Reference Counting

    If app targets iOS 5.0 or above

  • 8/12/2019 IOS project Architecture

    16/67

    Build Automation

    " Its the act of automating a wide variety of tasks you can use build tools like Ant, Maven, Make , CMake or Rake

    " At least you must automate Compiling and Deploying

    compiling and deploying are the most common tasks for developer

    " You can also automate Testing and Docs generation

    they are useful to use in combination with a Continuous Integration

    " You can also automate Static Code Analysis tools

    for improving quality and reducing defects by inspecting C, C++ andObjective-C code

  • 8/12/2019 IOS project Architecture

    17/67

    Build Congurations

    " Use di # erent Congurations to specialize the behavior

    i.e. Code Signing Identity, Preprocessor Macros, Linker Flags, etc.

    " Use AdHoc Conguration to deploy testing app

    i.e. app for TestFlight with its own Code Signing Identity & Linker Flags

    " Use AppStore Conguration to deploy on App Store

    Duplicate Release Conguration to use the same optimizations

  • 8/12/2019 IOS project Architecture

    18/67

    Build Settings

    " Enable Run Static Analyzer run the Clang static analysis tool on sourceles

    " Enable Treat Warning as Errors

    it causes all warnings to be treated as errors

    " Disable Compress PNG Files

    instead use ImageOptim

  • 8/12/2019 IOS project Architecture

    19/67

    Build Settings

    " Enable new -Ofast optimization Level

    it maximizes optimization, use it only in Release build conguration( Disclaimer : do not use if your application has high level oating pointprecision requirement)

    " Enable Link-Time Optimization

    reduce binary and increase performance

  • 8/12/2019 IOS project Architecture

    20/67

  • 8/12/2019 IOS project Architecture

    21/67

    Readme

    " The Basics Describe what it is that your project makes easier. Provide code examplesdetailing how the use of the library. Document the installation process.

    " Dont get lazy just because this is for developers Technical writing is still writing, and need not be dry and boring. Just as ifyou were writing an essay or blog post, you should strive to grab theattention of your reader early. This can be easily accomplished with awell-written introductory paragraph.

    " The code matters

    Besides speaking English, your readers also understand code.Developers love to see code samples, and a few lines of syntaxhighlighted source are worth a thousand words.

    http://robots.thoughtbot.com/post/50852570430/how-to-write-a-great-readme

    http://robots.thoughtbot.com/post/50852570430/how-to-write-a-great-readmehttp://writingcommons.org/process/organize/paper-structure/396-how-to-write-an-engaging-introduction
  • 8/12/2019 IOS project Architecture

    22/67

    Folders

  • 8/12/2019 IOS project Architecture

    23/67

    Folders

    " Put things in the right place

    ...and everything makes sense, unfortunately, Xcode doesnt help us

    " Map all Xcode group folders to le system directories

    Xcode group folder dont represent physical folder

    " Please remove Supporting Files group folder

    Who wants Supporting Files anymore? yuck!

  • 8/12/2019 IOS project Architecture

    24/67

    My folders structure

    " Application

    specic app related stu # like AppDelegate, main.m, .pch etc

    " Controllers

    view (.xib) and view controller stu # put together (obviously)

    " Library

    specic application classes like helpers, base classes, services, etc

  • 8/12/2019 IOS project Architecture

    25/67

    My folders structure

    " Models

    application domain models and entities, Core Data models too

    " Resources

    assets like images, fonts, sounds, videos, etc.

    " Vendors

    third part libraries and frameworks (not managed by CocoaPods)

  • 8/12/2019 IOS project Architecture

    26/67

    Controllers

    " Put .xib , .h and .m together in the same folders

    " One (physical) folder for each view controller

    " If there are too many, group them into a subfolder

    " Group them by tab (TabBar) or by functions

  • 8/12/2019 IOS project Architecture

    27/67

    Resources

    " One folder for each type of asset

    images, fonts, sounds, videos, strings, plist, samples

    "One subfolder for each type of image

    buttons, backgrounds, logos, shapes, icons, app (splash etc.)

    " If your app support multiple themes , create a hierarchy

    themes > themes name > images, fonts, etc.

    " Name image les based on state (if no assets catalog)

    button_blue_normal.png, button_blue_highlighted.png, etc.

  • 8/12/2019 IOS project Architecture

    28/67

    Folders

  • 8/12/2019 IOS project Architecture

    29/67

    Architecture

  • 8/12/2019 IOS project Architecture

    30/67

    Architecture...

  • 8/12/2019 IOS project Architecture

    31/67

    Application

    " Design (programming)

    " Layers

    " Compositions

    " Best Practices

  • 8/12/2019 IOS project Architecture

    32/67

    Design

  • 8/12/2019 IOS project Architecture

    33/67

    Design

    " Typically an application is divided into layers

    A layer is a black box with a contract that dene an input and output

    "To increase the cohesion and decoupling of the software

    The layers, if well designed, help to decouple and increase the cohesion

    " Cohesion indicates strongly related software module

    it would be a subroutine, class or library with common responsibilities

    " Coupling measure the level of dependency

    between two software module, such as classes, functions or library

  • 8/12/2019 IOS project Architecture

    34/67

    Design Principles

    " Single Responsibility Principle

    A module should have a single responsibility, and that responsibilityshould be entirely encapsulated by the module

    " Open Closed Principle

    A module should be open for extension but closed for modications

    " Liskovs Substitution Principle

    Derived types must be completely substitutable for their base types

  • 8/12/2019 IOS project Architecture

    35/67

    Design Principles

    " Interface Segregation Principle

    Clients should not be forced to depend upon interfaces that they don'tuse

    " Dependency Inversion Principle

    High-level modules should not depend on low-level modules. Both shoulddepend on abstractions. Abstractions should not depend on details.Details should depend on abstractions

    " SOLID : the "rst ve principles"

    S ingle responsibility, Open-closed, Liskov substitution, Interfacesegregation and Dependency inversion

  • 8/12/2019 IOS project Architecture

    36/67

    From Principles to Patterns

    " Design Pattern is a general reusable solutionto a commonly occurring problem within a given context

    " Its a description or template for how to solve a problemIts not a nished design that can be transformed into source code

    " There are many types of design patterns Architectural, Algorithm strategy, Computational, Implementation strategy,Structural, etc.

  • 8/12/2019 IOS project Architecture

    37/67

    Most Common Design Patterns

    " Model View Controller Design Pattern

    Its a fundamental design pattern in Objective-C.

    " Singleton Design Pattern

    For your information the AppDelegate is a singleton

    " Chain Of Responsibility Design Pattern

    Have you ever met the Next Responder or the First Responder?

  • 8/12/2019 IOS project Architecture

    38/67

    Layers

  • 8/12/2019 IOS project Architecture

    39/67

    Layers

    " Layer represents a logical section of the system

    Layer enforce reusability and testability

    " A typical client/server app have at least 3 layers

    Presentation Layer, Business Layer and Data Access Layer

  • 8/12/2019 IOS project Architecture

    40/67

    Layers

    Presentation Layer

    Data Access La er

    Business La er

    Views

    Controllers

    Service Layer Domain Model Layer

    Persistence Layer Network Layer

  • 8/12/2019 IOS project Architecture

    41/67

    Presentation Layer

  • 8/12/2019 IOS project Architecture

    42/67

    Presentation Layer

    " It have 2 components: the UI and the presentation logic

    in Cocoa the UI is the View and the presentation logic is the(View)Controller

    " Cocoa adopt Model View Controller Design Pattern

    the Presentation Layer is already in iOS SDK out-of-the-box

    " Advanced Appearance Customization with Theme

    use Appearance Proxy and Theme technique to customize UI.

    Th

  • 8/12/2019 IOS project Architecture

    43/67

    Theme

    // KMRMTheme.h typedef NS_ENUM(NSInteger, KMRMThemeLabelType){

    KMRMThemeLabelTypeArgumentTitle,KMRMThemeLabelTypeArgumentDescription

    };

    @protocol KMRMTheme

    - ( void )configure;- ( void )themeLabel:( UILabel *)label type:( KMRMThemeLabelType )type;

    @end

    // Concrete implementations#import "KMRMTheme.h"

    @interface KMRMDefaultTheme : NSObject @end

    @interface KMRMGreenTheme : NSObject @end

    P i L

  • 8/12/2019 IOS project Architecture

    44/67

    Presentation Layer

    UIView

    UIViewController Theme

    Model

    Appearance

  • 8/12/2019 IOS project Architecture

    45/67

    Business Layer

    B i L

  • 8/12/2019 IOS project Architecture

    46/67

    Business Layer

    " It holds the specic app Business Logic and Behaviors It is concerned with the retrieval, processing, transformation, andmanagement of application data; application of business rules andpolicies

    " The Domain Model is a conceptual model of business

    It describes the various entities, their attributes, roles, and relationships,plus the constraints that govern the problem domain

    " Business Layer gets data through a Service Layer

    Service Layer denes an application's boundary with a layer of servicesthat establishes a set of available operations and coordinates theapplication's response in each operation

    D i M d l L

  • 8/12/2019 IOS project Architecture

    47/67

    Domain Model Layer

    " Domain Model

    An object model of the domain that incorporates both behavior and data

    " You can use simple Objective-C objects

    A plain old Objective-C object that inheriting from NSObject

    " Or you can use Core Data objects you can extend the class NSMangedObject with your Objective-C class

    S i L

  • 8/12/2019 IOS project Architecture

    48/67

    Service Layer

    " Service Layers is a design pattern

    The benets a Service Layer provides is that it denes a common set ofapplication operations available to di # erent clients and coordinates theresponse in each operation.

    " Service Layer uses Data Access Layer to access data

    Service Layer uses DAL to performs the task of retrieving and storing databoth from server via network and from database

    " Service Layer is used by ViewController

    No more a ton of line of codes in your ViewController, instead few lines ofsimple Service Layer calls

  • 8/12/2019 IOS project Architecture

    49/67

    Data Access Layer

    D t A L

  • 8/12/2019 IOS project Architecture

    50/67

    Data Access Layer

    " Its a layer which provides simplied access to data

    The data may be stored in a persistent storage like SQLite or in abackend accessible by network

    " It may uses a Persistence Layer or Network Layer

    Both exposes a simplify contract to access data

    P i t L

    http://en.wikipedia.org/wiki/Layer_%28object-oriented_design%29
  • 8/12/2019 IOS project Architecture

    51/67

    Persistence Layer

    " The persistence layer deals with persisting

    The persistence layer is responsible for manipulating the database, and itis used by the service layer

    " You can use Core Data as Persistence Layer

    Or, in alternative, you can use FMDB for direct access to SQLite

    N t k L

  • 8/12/2019 IOS project Architecture

    52/67

    Network Layer

    " Network Layer is responsible of all networking calls

    " You can use AFNetworking as Network Layer

    AFNetworking is a delightful networking library for iOS and Mac OS X. It'sbuilt on top of NSURLConnection, NSOperation, and other familiar

    Foundation technologies

  • 8/12/2019 IOS project Architecture

    53/67

    Composition

    C iti

  • 8/12/2019 IOS project Architecture

    54/67

    Composition

    " Its a way to combine objects into more complex ones

    Compositions are a critical building block of many basic data structures,including the tagged union, the linked list, and the binary tree, as well asthe object used in object-oriented programming

    " In a real-world app composition takes an important role

    On iOS / OS X App composition is necessary for a good layering and fora structure UI.

    Composition

  • 8/12/2019 IOS project Architecture

    55/67

    Composition

    " Custom Views are an example of composition

    A custom view is used to manage small portions of the interface in orderto recycle the content and its management

    " In a real-world iOS/OS App there are many custom views

    For example, all views that must be inserted in a scroll view, or all those

    portions of the view that occur multiple times in di # erent view and onlywith di # erent content.

  • 8/12/2019 IOS project Architecture

    56/67

    Best Practices

    General Best Practice

  • 8/12/2019 IOS project Architecture

    57/67

    General Best Practice

    " Use Automatic Reference Counting

    Always use ARC. All new code should be written using ARC, and alllegacy code should be updated to use ARC

    " Use AppDelegate as Singleton

    Create all common and singleton objects in App Delegate and thenexpose them by UIResponder Category

    Coding Best Practices

  • 8/12/2019 IOS project Architecture

    58/67

    Coding Best Practices

    " Create a property for every ivar and use self to access it

    Always create a @property for every data member and use self.name toaccess it throughout your class implementation

    " Alway declare atomic or nonatomic attribute

    Always use the nonatomic attribute on your properties, unless you arewriting a thread-safe class and actually need access to be atomic

    " Use literals and modern Objective-C syntactic sugar

    The source code will be less verbose and more clear.

    Presentation Best Practices

  • 8/12/2019 IOS project Architecture

    59/67

    Presentation Best Practices

    " Create a base UIViewController Create a MYBaseViewController from which all the view controllers inherit.In this way all the controllers can inherit common behavior.

    " Create a base UIView Create a MYBaseView from which all the custom views inherit. In this wayall the views can inherit common style and appearance

    " Create a base UITableViewCell

    Create a MYBaseTableViewCell from which all the custom table view cellsinherit. In this way all the cells can inherit common style and appearance

    Code Design Best Practices

  • 8/12/2019 IOS project Architecture

    60/67

    Code Design Best Practices

    " API Design

    Pay attention to the design of your API. Learn your target platform'sconventions before coding. Dene the rules that are in accordance withthe convention of language

    " Block and Delegation

    When should I use blocks instead of delegation for callbacks? Payattention to this topic and alway look at Apple docs to see how they done

  • 8/12/2019 IOS project Architecture

    61/67

    Resources

    Links

  • 8/12/2019 IOS project Architecture

    62/67

    Links

    " Blocks vs Delegation

    http://thejoeconwayblog.wordpress.com/2012/05/29/blocks-or-delegation/

    " API Design

    http://mattgemmell.com/2012/05/24/api-design/

    " Modern Objective-C

    http://www.slideshare.net/giuseppearici/modern-objectivec-pragma-night

    Links

    http://www.slideshare.net/giuseppearici/modern-objectivec-pragma-nighthttp://mattgemmell.com/2012/05/24/api-design/http://thejoeconwayblog.wordpress.com/2012/05/29/blocks-or-delegation/
  • 8/12/2019 IOS project Architecture

    63/67

    Links

    " objc.io - A periodical about best practices and advancedtechniques in Objective-C

    http://www.objc.io/

    " Automatic Reference Counting recap

    http://www.slideshare.net/giuseppearici/pragma-night-automaticreferencecounting

    " How to write a great README

    http://robots.thoughtbot.com/post/50852570430/how-to-write-a-great-readme

    Videos

    http://robots.thoughtbot.com/post/50852570430/how-to-write-a-great-readmehttp://www.slideshare.net/giuseppearici/pragma-night-automaticreferencecountinghttp://www.objc.io/
  • 8/12/2019 IOS project Architecture

    64/67

    Videos

    " Customizing the Appearance of UIKit Controls

    Session 114 - WWDC 2011 Session Videos

    " Advanced Appearance Customization on iOS

    Session 216 - WWDC 2012 Session Videos

    " Whats New in the LLVM Compiler Session 402 - WWDC 2013 Session Videos

    Books

  • 8/12/2019 IOS project Architecture

    65/67

    Books

    " Cocoa Design Patterns

    " Erik M. Buck & Donald A.Yacktman

    " Addison Wesley

    Books

  • 8/12/2019 IOS project Architecture

    66/67

    Books

    " Patterns Of Enterprise Application Architecture

    " Martin Fowler

    " Addison-Wesley Professional

    " updates: http://martinfowler.com/ books/eaa.html

    Thank you

    http://martinfowler.com/books/eaa.html
  • 8/12/2019 IOS project Architecture

    67/67

    Thank you

    Massimo OlivieroIT Consultant & Software Developer#pragma mark founder

    email [email protected] web www.massimooliviero.net

    slide www.slideshare.com/MassimoOliviero twitter @maxoly

    Any questions?

    http://www.slideshare.com/MassimoOlivierohttp://www.massimooliviero.net/mailto:[email protected]