• Nem Talált Eredményt

SERVICES

In document Mobile Softwares (Pldal 84-0)

• Application component

• Perform a longer-running operation while not interact with the user

• Service has no UI

• Service has its own lifecycle

• Other applications can interact with the service

• Take care of closing resources and stopping the service!

• Examples: music player, downloader application (e.g. torrent), etc.

[fragile]

5.40. Service types

• Started

• Service started with startService() method by a component

• Usually perform a single operation

• When the operation is done, the service should stop itself

• Bound

• An application component binds to the service by calling bindService()

• Offers a client-server interface to interact with the service in both ways

• Run only as long as another application component is bound to it

• Custom service can support both types at the same time

5.41. Service lifecycle

5.42. Managing Services

• The usage is similar to activities

• Communicate through Intents

• Service runs in the main thread of its process by default

• Long-running or blocking tasks should be executed in new thread

• e.g. CPU intensive work, MP3 playback, networking

• if not: well-known Application Not Responding (ANR) [fragile]

5.43. Creating Service

• Extend Service class

• Override required methods

• Take care of proper termination

• Close resources

• Stop service

• Register the Service in as an application component

• Start the Service

• startService() or bindService() [fragile]

5.44. Custom Service methods

• onStartCommand()

• automatically called when another component starts the service

• onBind()

• automatically called when another component binds to the service

• onCreate()

• called when the service is created

• before onStartCommand() and onBind()

• onDestroy()

• called before the service has been destroyed

5.45. Terminating services by Android OS

• Android OS stops services only if the Activity in the foreground needs more memory

• Lower the chance that the service will be destroyed

• If a service is bound to an Activity

• Foreground Services are almost never killed by the system

• If there is enough free memory again then the system trying to restart the service

5.46. Service attributes 1/2

5.47. Service attributes 2/2

• enabled: determine that the service can be instantiated by the system

• exported: whether or not components of other applications can invoke the service or interact with it

• icon: icon of the service

• label: a name for the service that can be displayed to users

• name: package name where the implementation is

• permission: which permissions are required to connect to this service

• process: the name of the process where the service is to run

5.48. Service example

• Custom Service

• Starting the Service

5.49. Summary

• Network communication

• Standard interfaces for standard protocols

• Location based services

• Lot of possibilities to monitoring location

• Geocoding and reverse geocoding to transform coordinates to addresses and back

• Custom Google Map based views with MapView

• Use Service to perform long-running operations in the background

• Independent from UI

6. 6 Introducing the iOS Mobile Platform, Developer

Tools and Programming basics

6.1. Lecture 6 - Outline

• Platform summary

• Developer Tools and Programming basics

• Architecture of the iOS applications

6.2. Devices running iOS

• iPhone

• iPhone, iPhone 3G, iPhone 3GS, iPhone 4, iPhone 4GS, iPhone 5

• iPod Touch

• iPod Touch 1st, 2nd, 3rd, 4th, 5th generation

• iPad

• iPad 1, iPad 2, iPad 3, iPad 4, iPad Mini

• (Apple TV)

6.3. iOS the Operating System

• Derived from core OS X technologies

• Nearly the same kernel (XNU)

• UNIX/BSD fundaments

• iOS application are not compatible with OS X

• Closed platform with numerous restrictions

• Development only with the official iOS SDK only in OS X environment

6.4. Behind success

• Innovative User Interface

• Touchscreen

• Multitouch support

• Fast and fluent

• A real Mobile web browser: Safari

• App Store: centralized application store

• "There is an App For That!"

• The developers get popularity only for a small percent of the profit

• Common Components

• Human Interface Guidelines

• Apple fanboy effect

6.5. Drawbacks

• API weakness, no full multitasking

• Became even better

• Requires OS X for development

• Requires iTunes for data synchronization

• Unable to use as USB pendrive

• Applications are available only through App Store

• Apple decides which applications can be published through the Store. Many critics.

• Customization requires Jailbreak

• No integrated FM radio (only external)

• Expensive

6.6. Drawbacks

• API weakness, no full multitasking

• Became even better

• Requires OS X for development

• Requires iTunes for data synchronization

• Unable to use as USB pendrive

• Applications are available only through App Store

• Apple decides which applications can be published through the Store. Many critics.

• Customization requires Jailbreak

• No integrated FM radio (only external)

• Expensive

6.7. The History of the iPhone

• API weakness, no full multitasking

• 2007. june: itroduces iPhone 1.0

• 2008. march: introduces the iPhone SDK

• The begining of 3rd party application development

• 2008. july: introduces iPhone 3G and iOS 2.0 and opens the App Store

• 2010. april: introduces iPad (from than on the operation system is officially called iOS)

• Currently iPad 4, iPhone 5

6.8. iOS versions from user perspective

• iOS 1: fix 20 applications (Mail, Messages, Maps, ...)

• iOS 2: App Store, A-GPS

• iOS 3: MMS, copy-paste, Spotlight search, Push Notification, IMAP, compass

• iOS 4: multitasking, app folders, inbox, Game Center, iAd

• iOS 5: Notification Center, iCloud, iMessage, native Twitter, Siri

6.9. iOS versions from developer perspective

• iOS 1.0: only web based 3rd party applications

• iOS 2.0: native 3rd party applications, iOS SDK, App Store

• iOS 3.0-3.2: MMS, copy-paste, Spotlight search, Push Notification, IMAP, compass

• iOS 4.0-4.3: New application life-cycle model, background tasks, easier thread handling(blocks), Xcode 4

• iOS 5.0: automatic reference counting (ARC), storyboards

6.10. Hardware, common properties

• ARM architecture based processors(ARMv6 és ARMv7 instruction set)

• Display resolutions:

• iPhone5: 640 x 1136, 960x640 (iPhone 4-4S, "Retina display"), 480x320

• iPad: 2048x1536 (iPad 3+), 1024x768 (iPad 1-2, iPad Mini)

• Simultaneous multitouch points:

• iPhone: 5

• iPad: 11

6.11. iPhone 5 interiors

• Display: 640 x 1136 IPS TFT, capacitive touch screen

• SoC: Apple A6 (Dual-core 1.2 GHz ARM Coretex-A9)

• RAM: 1 GB

• Wireless radio:

• 3G, HSDPA, HSUPA

• Wi-Fi 802.11 b/g/n,

• Bluetooth 4.0

• A-GPS, GLONASS, accelerometer, gyroscope, compass

• Capacity: 16/32/64 GB

• Camera: 8 MP, autofocus

• Accumulator: 1440 mAh

6.12. UDID: Unique Device Identifier

• Every iOS device have a globally unique identifier, which is a 40 character hexa string

• This identifies the test device during the application development

• It can be get with Xcode Organizer or with iTunes: Summary view, than one clikk on "Serial Number"

filed

• Hint: Cmd + C will copy to clipboard

6.13. BASIC PROPERTIES

6.14. Relevant attributes

• Application life-cycle - multitasking

• Notifications

• Isolated storage (sandbox)

• Limited application testing

• Device identification (UDID)

6.15. iOS multitasking

• Only one application can run in the foreground (active)

• The other running apps are in deactivated mode in the memory

• Home button: sending application to background (deactivating)

• Home button double press: multitasking bar

• If the memory is insufficient, the iOS terms some deactivated applications

• Certain predefined tasks can be run in deactivated applications(background execution)

• Audio playback, VoIP, GPS tracking

• How can we reach a non running application? Notifications!

6.16. Notifications

• Asynchronous notifications for the non running applications

• Two types

• Local Notification: application schedules for himself (ie. notify in 5 seconds)

• Push Notification: Notifications arriving through Apple Push Notification service through a permanently connected TCP channel

• When the notification arrives, the user decides to start the application or not.

• iOS 5 Notification Center

6.17. Sandbox model

• Applications can reach resources only within their own sandboxes

• Only specific directories can be accessed (read/write)

• No support for memory card

• It is not possible to reach the other applications sandboxes

• There is no IPC (Inter Process Communication)

• Other applications can be launched through special URL schemes:

• Device Bluetooth settings: prefs:root=General path=Bluetooth

• List of Facebook friends: fb://friends

6.18. Isolated Storage 1/2

"Every application is an Island."

6.19. Isolated Storage 2/2

• Every application has a dedicated folder, which can be accessed only by itself

• Inside the Home Directory there are three subdirectories. One of the most important is the Documents directory

• Documents

• Can be accessed through PC/Mac via iTunes ("File Sharing")

• Will be archived and stored during iTunes backup

6.20. Application distribution

• AppStore

• Application Store of Apple, main distribution channel

• Ad-Hoc

• Maximum 100 device with registered UDIDs

• The installer of the application (IPA) can be shared (via email, webpage, etc.)

• Requires iTunes for installation

• Constrained only to devices with registered UDID

• Enterprise

• In-house application sharing for companies (max 200 devices)

• 300$ / year

6.21. App Store

• Application Store, can be accessed through the device

• The developer defines the price of his application

• After each purchase 70% of the incoming will be wired monthly

• The application should fulfill the requirement of the App Store

• All submitted application will be reviewed. Those who fail on the tests will be rejected

• Developers have to enrolled in the Apple Developer Program

• 99$ / year

6.22. TOOLS AND DEVELOPMENT CONCEPTS

6.23. Application development

• iOS application types

• Web: Safari rendered, web applications optimized for mobile (HTML, CSS, JavaScript)

• Fast development, easy update

• Most parts are platform independent

• Only a subset of iOS functionality can be reached- ie. multitouch even handling via JavaScript

• Native: Objective-C based application compiled for the iOS devices

• Full iOS functionality, fast

• Web content can be also presented (WebKit)

• Slower development process

6.24. iOS development without Objective-C?

• MonoTouch

• C# / .NET based development

• Ahead-Of-Time (AOT) compiling

• Binding to numerous Cocoa Touch API

• Development only on Mac OS

• Adobe Air for iOS

• Creates iOS application from Flash source (packaging)

6.25. Official developer tools

• iOS SDK contains all of them

• Xcode: IDE, debugger, interface builder

• iOS Simulator

• Instruments: application testing and analysis (ie. memory leak, CPU usage)

• Dashcode: Tool for iOS web application development

6.26. Xcode the IDE

• Editor

• Interface builder

• Debugger

• LLDB or GDB

• For iOS and Mac OS application development

• Apple LLVM compiler for compiling

• Full integration in Xcode

6.27. iOS Simulator

• Simulating iPhone and iPad on Mac OS X

• Appliction can be tested without real hardware

• In fact the code compiles for X86 / Mac OS

• Simulating harware behaviour

• Device orienatation, hardver buttons and shake gesture simulation

• Limited multi-touch and gesture support

• Positioning: From Xcode 4.2 Ăłta the coordinates can be set

• Sensor support with external applications

6.28. iOS applications

• iOS (and Mac OS X) applications exist in the form of so called

• A predefined folder structure with the name of "Applicationname.app"

• It contains all resources of the application (binaries, images, etc.)

• All applications have a unique identifier (App ID), which was defined by the developer ie.com.mycompany.myApplication

• The installation of the application can be interpreted as copying the bundle to a predefined place

6.29. Framework

• The functionality of the iOS can be reached through the frameworks

• Framework requires linking!

• Framework: DLL + resources and headers

• Making 3rd party frameworks for iOS is not possible (for Mac OS it is allowed)

6.30. Framework examples

• Foundation

• Basic supporting classes (arrays, strings, time and date handling, etc.)

• Projects generated by Xcode automatically link it

• UIKit

• The bigest and most important framework:

• Supporting application life-cycling, multitasking

• UI components

• gestures...

• Projects generated by Xcode automatically link it

• Core Location

• Positioning

6.31. Compiling/testing with iOS device

• Should be enrolled into the iOS Developer Program

• Max 100 different device can be used during the development

• Device should be connected to the Mac via wired connection (iTunes)

• After compiling the application will be copied to the device

• On-device debug: Application running on device while debugging the code with Xcode on the Mac

6.32. Application development and iOS versions 1/2

• iOS continuously updates, current iOS 6

• Significant differences between the API versions

• All iOS applications have a minimum iOS version which is still supported

• There is a new iOS SDK for each new iOS versions

• Each iOS SDK supports compiling for older iOS versions (just a few previous ones)

6.33. Application development and iOS versions 2/2

• In Xcode not only the SDK version but the minimum iOS version can be set.

• It can be defined for each iOS application project

• Base SDK: the SDK what is used for compiling (defines also the API set)

• Deployment Target: the oldest iOS version, which the application supports

• If we would like to use an SDK functionality on a target which is not support it, runtime check required (Otherwise the application crashes)

6.34. iPad vs. iPhone

6.35. Mac OS X vs. iOS development

• Many joint API, but there are differences too

• Cocoa vs. Cocoa Touch

• Missing or reduced classes under iOS

• Separated documentation and sample codes

• 3D graphics: OpenGL vs. OpenGL ES

• No garbage collection on iOS (but from iOS 5 Automatic Reference Counting)

• The two world get even closer to each other

• In the application of Mac App Store the sandbox environment became also a requirement

• Separated developer program for the two platforms (99$ x 2)

• Mac OS applications can be distributed and tested App Store

6.36. Architecture of the iOS applications

Mobilszoftverek - iOS

6.37. Model View Controller

6.38. The MVC concept

• The classes of the program can be grouped into three categories

• Model: all kinds of data used by the program

• View: UI - what the user sees on the screen

• Controller: control interaction between the view and the model

• Directive: If any component is modified (model/view/controller) the others remains unchanged

• Ie. if we modify the view in order to support iPad not only iPhone, than the model and the controller should not be modified

6.39. iOS application base classes

6.40. View

• Everything which is drawing directly to the screen and the user can see

• The View can catch the user interaction (touch), but controller can react!

• Base view class: UIView

• Everything that draws directly to the screen

• Interaction with the user: controls (UIControl)

• Can embed each other: a UIView can contain child UIViews

• Views can be built from code, but usually they are generated with the Interface Builder (IB)

6.41. UIView examples

• UIButton

• UILabel

• UIImageView

• UITableView

• UIWebView

• UISegmentedControl

• UISplitView

6.42. View hierarchy

• Views can be embed in each other: a view can contain several child views

• Parent-Child

• A screen of the application is usually a parent view with many child views

6.43. Controller

• Connection between the view and the model

• Handles the events of the view(ie. a button tapped)

• Performs transformations on the data of the model

• "Everything which is neither belonging to the view nor to the model"

• In the UIKit we are working with View Controllers

• The name contains View because the controller owns and manages the view

• The controller never draws to the screen, that is the task of the view

• The parent class of all view controllers is the UIViewController

6.44. View controller in details

• Controlls the coherent views

• Usually one "screen" is controlled by one controller

• Generally each view is controlled by one view controller

• The events generated by the view (ie. touch events) handled in its the controller

• The sheme of the UI of the application is based on the hierarchy of the view controllers

• The root view of the view controller can be reached through the view property of the view controller

• The subviews (childrens) of the root view are also controlled by the same view controller

6.45. View and ViewController in the source

• The view is usually defined in the Interface Builder

6.46. Model

• Task belongs to the model:

• File handling

• Serializing

• Database access (SQLite, Core Data...)

• There is no dedicated "model" parent class, the developer should define the classes belong to the model

• ie. NewsModel, GameWorld, ClientDb, stb...

7. 7 The Objective-C

7.1. Lecture 7 - Outline

• Objective-C basics

• Connection between source code and Interface builder

7.2. OBJECTIVE-C BASICS

7.3. Objective-C

• Based fully on the C language, can be interpreted as an extension of C language

• 100% C compatible, all standard C code can be compiled with an Objective-C compiler

• Beyond C

• Object-oriented programming

• Dynamic type handling

• reflection

• Syntax: Comes from Smalltalk

• Non object oriented components can be accessed with standard C syntax

• Only on Mac OS X and on iOS get popular

• Dynamic language: tries to do everything in run time instead of compile time

[fragile]

7.4. Primitive types, strings

• C primitives: int, float, char, \dots

• Unique boolean: BOOL

• NO = 0

• YES = 1

• Special types: id, Class, SEL, IMP

• Null pointer value: nil

• Strings: NSString is used instead of char*

• NSString* string = @,,This is a string'';

• @ marks, that this is an NSString literal, omitting can cause crash [fragile]

7.5. Objects

• Each object has a class (Class)

• An object can contain

• Methods

• Instance variables

• Properties (setter/getter methods definitions)

• The class of the object is stored in the isa instance variable of object's Class

• Can be obtained [fragile]

7.6. Method calling = Message sending

• In Objective-C messages can be sent to the objects

• If the format/identifiers of the message conforms to the method of the object, the method will be called

• Message syntax: [recipient message];

• Parameters:

• Examples:

• Each message should contain as much parameter as the number of colons are in the signature of the message

7.7. Message sending: Java vs. Objective-C

[fragile]

7.8. Message sending

• Messages sent to nil-make no effect and returns with 0(nil)

• Functions should be called with the regular syntax of C function call [fragile]

7.9. The id type

• The base class of the object pointers: id

• Can point to any kind of object

• If it has no value, than it should be set to nil

• Essentially pointer! ('*' not needed)

• like C++ void*, but works only with objects

[fragile]

7.10. Classes - Class object

• Each class contains a Class type met object

• It can be referred by the name of the class i.e. MyClass, NSString, etc.

• Messages can be sent to it like to other objects: \[MyClass doSomething\];

• Class instances (objects) can be generated through this meta object

• Class methods can also be reached through this : static method

• Examples:

[fragile]

7.11. Classes - Instantiation

• The creation of a new object is done by sending the alloc message to the classid myEgg;myEgg = [TurtleEgg alloc];

• alloc(ates) the required memory for the object and fills with 0

• The initialization of an "empty" object is done by the init method ( constructor)

• The implementation of the init method should be done by the programmer

7.12. NSObject

• Base class of all classes

• "Services":

• Instantiation (alloc class method)

• Reference to the class of the object (class, superClass)

• Message sending (performSelector:)

• Introspection / reflection, ie.

• conformsToProtocol

• isKindOfClass

7.13. Defining classes

• The class definition consists of two parts

• Interface Class.h

• Implementation Class.m

• Should be imported before use: #import MyPrinter.h

• Same as #include, protects against multiple insertion

[fragile]

7.14. Method definition 1/2

• Method definition should be inside the @interface block

• Closes the @end keyword

• Method implementation inside the @implementation block

• Referring to the object with self ( this pointer)

• Each method are "public", if a proper a message arrives the method will be called

• At the beginning of the method +/- indicates, whether it is a class or an instance method (not UML public/private!)

7.15. Method definition 2/2

• The parameter identifiers (name, age) are only relevant at the definition of the method

[fragile]

7.16. Dynamic binding

• Dynamic binding at message sending

• Always the current object defines which method should be called the pointer type is irrelevant

• Message sending to self and to super

• self: accessing the methods of the current class

• super: accessing the methods of the super class

7.17. Classes - Instance variables

• Declaration of the instance variables should be placed into the interface definition between the { } brackets

• The default visibility of the instance variables are @protected, but it can be @private and @public

7.18. Classes - Properties

• Property: Property of an object which can be get or set

• Always public

• Property declaration:@property (strong) NSString* address;

• The setting and getting of the value of the property:

7.19. Creating properties in 2 steps

1. The property should be declared in the header (.h) file of the class

2. The property can be synthesized in the implementation (.m) file of the class

7.20. Property synthesizing

• The property is actually a pair of methods and an instance variable which can be generated with the

7.21. Inheritance1/2

• Each class has only one parent class

• The inherited methods can be overridden (each method is "virtual")

• Multiple inheritance only with protocols

• Protocol = in other languages "interface"

• List of method definition without instance variables and implementations

[fragile]

7.22. Inheritance 2/2

• A class can adopt any number of protocols

• Methods declared in the protocol should not be listed in the interface, only implementation required

• Methods of the protocol could be the following:

• Optional (@optional - implementation is optional)

• Required (@required - implementation is required)

7.23. Example - Protocol implementation

7.24. Memory management

• From iOS 5 Automatic Reference Counting: ARC

• The commands responsible for the reference counting, will be inserted by the compiler automatically

• Not Garbage Collector, no run time analysis

• An object becomes deallocated when there is no strong reference to it

• The objects are kept alive by pointers

• It can be set how the properties and variables should behave respect to the reference counting

• It can be set how the properties and variables should behave respect to the reference counting

In document Mobile Softwares (Pldal 84-0)