29
9/2/14 1 Mobile Computing CSE 40814/60814 Fall 2014 Mobile App Development Binary executable files on the device. Can access all API’s made available by OS vendor. SDK’s are plaKormspecific. Each mobile OS comes with its own unique tools and GUI toolkit. 1 Different tools, languages and distribuUon channels associated with leading mobile operaUng systems Native Apps 2 PROS Easy lowlevel hardware access services. Easy access to high level services important to personal mobile experience. Full use of all funcUonaliUes that modern mobile devices have to offer. High usability. CONS Code Reusability : Low Development & maintenance: Timeconsuming & expensive. Designers are required to be familiar with different UI components of each OS. Upgrade flexibility: Low.

Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

1  

Mobile Computing CSE  40814/60814  

Fall  2014  

Mobile App Development •  Binary  executable  files  on  the  device.  •  Can  access  all  API’s  made  available  by  OS  vendor.  •  SDK’s  are  plaKorm-­‐specific.  •  Each  mobile  OS  comes  with  its  own  unique  tools  and  GUI  

toolkit.  

1

Different  tools,  languages  and  distribuUon  channels  associated  with  leading  mobile  operaUng  systems  

Native Apps

2

PROS  

Easy  low-­‐level  hardware  access  services.  

Easy  access  to  high  level  services  important  to  personal  mobile  experience.  

Full  use  of  all  funcUonaliUes  that  modern  mobile  devices  have  to  offer.  

High  usability.  

CONS  

Code  Reusability  :  Low  

Development  &  maintenance:  Time-­‐consuming  &  expensive.  

Designers  are  required  to  be  familiar  with  different  UI  components  of  each  OS.  

Upgrade  flexibility:  Low.  

Page 2: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

2  

Virtual Machine Approach •  A  virtual  machine  is  used  to  abstract  the  target  plaKorm  details  from  the  

applicaUon’s  running  code.    •  The  framework  provides  both  the  API  and  runUme  environment.  •  The   runUme  executes  on   the  mobile  device   and  enables   interoperability  

between  the  device’s  OS  and  the  mobile  applicaUon.  

ADVANTAGES:  •  Improved  performance  and  user  experience.  •  Full   access   to   funcUonaliUes  of  underlying  mobile  OS  and  device   specific  

capabiliUes.  •  Portability:  VM’s  are  easier  to  maintain  &  more  flexible  to  extend.  

DISADVANTAGES:  •  Slower  due  to  runUme  interpretaUon  latency.  

3

Cross-Platform Development •  Separates  build  environment  from  target  environment.  •  PlaKorm-­‐independent   API   using   a   mainstream   programming   language   like  

JavaScript,  Ruby  or  Java.  •  The  cross-­‐compiler  then  transforms  the  code  into  plaKorm-­‐specific  naUve  apps.  •  The   soaware   arUfact   generated   can   be   deployed   and   executed   naUvely   on   the  

device.  

ADVANTAGES:  •  Improved  performance  and  user  experience.  •  Full   access   to   funcUonaliUes   of   underlying   mobile   OS   and   device   specific  

capabiliUes.  

DISADVANTAGES:  •  Highly  complex  as  cross-­‐compilers  are  difficult  to  program.    •  Need   to   be   kept   consistent   with   fragmented   mobile   plaKorms   and   operaUng  

systems  available.  

4

Cross-Platform Development

5

PROS  

Code  Reusability  

Plugins  

Easy  for  web  developers  

Reduced  development  costs  

Support  for  enterprise  &  cloud  services  

Easy  Deployment  

CONS  

Might  not  support  every  feature  of  OS  

Cannot  use  own  tools/IDE  

Slower  

High  end  graphics  &  3D  support  limited  

Vendor  lock-­‐in  

Page 3: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

3  

Mobile Web Apps •  Use  standard  web  technologies  such  as  HTML  5,  CSS  3  &  JavaScript.  •  Features  of  HTML  5  -­‐  Advanced  UI  components,  access  to  rich  media  types,  geo-­‐

locaUon  services  &  offline  availability.  •  Increasing  popularity  of  HTML  5  in  rendering  engines  such  as  WebKit.  •  Runs  on  a  standalone  mobile  web  browser.  •  Installed  shortcut,  launched  like  a  naUve  app.  •  UI  logic  resides  locally;  makes  the  app  responsive  and  accessible  offline.  

ADVANTAGES:  •  MulUplaKorm  support.  •  Low  development  cost.  •  Leverage  exisUng  knowledge.  

DISADVANTAGES:  •   Limited  access  to  OS  APIs.  

6

UI Design Considerations

7

Cross-Platform Development Tools

8

Page 4: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

4  

Mobile website

AdapUve  design,  responsive  design,  media  queries  

Mobile website

AdapUve  design,  responsive  design,  media  queries  

Making a Decision

App  funcUonality   Time  and  budget  

Target  audience   (IT)  Resources  

Page 5: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

5  

App Distribution

12

Comparison

Right  Now:  Mostly  Na;ve  Apps  

Mobile'Web''

Apps'

0"

20"

40"

60"

80"

100"

120"

140"

160"

Mar+11" Apr+11" May+11" Jun+11" Jul+11" Aug+11" Sep+11" Oct+11" Nov+11" Dec+11" Jan+12" Feb+12" Mar+12" Apr+12" May+12" Jun+12" Jul+12" Aug+12"

Minutes'Spe

nt'Per'M

onth'(B

iillio

ns')'

Total'Mobile'App'And'Web'Dura:on'On'Android'And'iOS'

Source:(Nielsen(Smartphone(Analy5cs(

Page 6: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

6  

Only 20% of our time on mobile is spent in our browser

Games&&32%&

Facebook&18%&

Entertainment&8%&

U7lity&8%&

Other&Social&6%&

News&2%&

Produc7vity&2%&

Other&6%&

Browser&20%&

Source:(Flurry,(comScore,(NetMarketShare(

HTML5 is meant to work seamlessly across mobile platforms and browsers

HTML5  Web  apps  can  be  installed  from  the  Web  as  icons  on  your  home  screen  across  any  phone  

Personal  diary  app:  Penzu  

Page 7: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

7  

Example:  tradeMONSTER  Ranked  best  mobile  stock-­‐trading  app  of  2012  by  Barron’s  

Source:  Google  Play  

Many  serious  apps  are  being  built  around  HTML5  

HTML 5 •  Works  across  all  operaUng  systems.  •  Many  developers  familiar  with  HTML.  •  Lower  development  costs  (don’t  have  to  start  from  scratch  for  each  OS).  

19

HTML5 faces its own fragmentation problem: different mobile browsers

Safari&62%&

Android&Bowser&22%&

Opera&Mini&8%&

Chrome&2%&

Internet&Explorer&2%&

Other&4%&

Source:(Net(Applica0ons,(March(2013(

Page 8: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

8  

The  mobile  browsers  are  inconsistent  in  their  support  of  HTML5  features  

58#

90#

45#

76#

24#

72#

93#

98#

18#15#

17#

12#8# 8# 9#

15#

57#

28#

71#

45#

101#

53#

31#

20#

0#

20#

40#

60#

80#

100#

120#

iOS#Safari#4.234.3#

iOS#Safari#6.0# Android#Browser#2.2#

Android#Browser#4.1#

Opera#Mini#5.037.0#

Opera#Mobile#11.5#

Firefox#For#Android#

Chrome#For#Android#18.0#

Numbe

r'Of'Features'A

nd'Le

vel'O

f'Sup

port'On

'Mob

ile'Browsers'

Source:'Caniuse,'Dec.'20'2012'Supported# ParHally#Supported# Not#Supported#

HTML5   NaUve  

DefiniUon   Mobile  browser  access  to  HTML   NaUve  app  code  and  data  

Coding  Language   HTML5   Java,  ObjecUve-­‐C,  etc.  

Pros   -­‐  Full  code  reuse  -­‐  Single  app  experience  

-­‐  NaUve  UI  -­‐  Full  plaKorm  capabiliUes  -­‐  SubstanUal  code  reuse  

Cons   -­‐  Limited  offline  funcUonality  -­‐  Inferior  UI  

Limited  to  no  code  use  

HTML5 apps also don’t work as well offline

“I  think  the  biggest  mistake  that  we  made,  as  a  company,  is  bePng  too  much  on  HTML5  as  opposed  to  na;ve  …  because  it  just  wasn’t  there  …..  ”    –  Mark  Zuckerberg,  September  2012  

And  Mark  Zuckerberg’s  cri;cism  didn’t  help  HTML5’s  cause…  

Page 9: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

9  

LinkedIn  recently  dumped  HTML5  for  na;ve  too…  

Mobile  Web   Mobile  App  

Accessed  aaer  being  installed  

InteracUve  user  interface  (UI)  

Available  offline  

Can  make  use  of  phone  features  like    LocaUon  services,  camera,  etc.  

Accessed  through  browsing  

StaUc,  navigaUonal  user  interface  

Requires  connecUon  

Somewhat  limited  features  

Fast   Speed   Very  Fast  

Reasonable   Development  Cost   Expensive  

App  Store  Not  necessary  

Available  Approval  Process  None  

SomeUmes  mandatory  

Comparison Mobile Web & Native

Source:  MDG  

More Pros & Cons •  No  centralized  distribuUon  (pro  &  con)  •  No  money  to  Google  &  Apple  •  Security  “beoer”  for  naUve  apps  •  Immediate  updates  &  distribuUon  

26

Page 10: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

10  

Item   Winner   Why    

Rich  user  experience  and  performance  

 NaUve  Apps  

 HTML5  sUll  faces  challenges  in  accessing  device-­‐naUve  features  across  all  mobile  browsers.    

Cross-­‐plaKorm  deployment  costs    HTML5  

The  W3C  is  in  talks  with  the  auto,  publishing,  and  TV  industries  to  bring  the  Web  to  new  devices.  

Immediate  updates  and  distribuUon  control  

 HTML5  

HTML5  is  Web-­‐based,  so  no  single  company  can  act  as  the  gatekeeper,  slow  down  updates,  or  take  a  cut  of  app  revenue.    

 MoneUzaUon  

 NaUve  Apps  

There  hasn’t  been  a  convincing  model  for  HTML5  moneUzaUon  beyond  individual  vendors  selling  their  apps  online.  

 Available  programming  experUse  

 HTML5  

iOS  engineers  are  in  short  supply  and  expensive.  HTML5  is  the  common  language  of  the  Web.  

 FragmentaUon  challenges  

 HTML5  

Both  naUve  apps  and  HTML5  face  fragmentaUon  challenges,  but  of  different  sorts.  UlUmately,  it’s  more  likely  HTML5  will  win  out  here.  

 Security  

   NaUve  Apps  

Currently,  naUve  apps  sUll  offer  greater  ease  of  access  to  advanced  security  features  and  encrypUon.  

Comparison

Goal

•  Create a very simple application

•  Run it on a real device

•  Run it on the emulator

•  Examine its structure

Google Tutorial

•  We will follow the tutorial at: http://developer.android.com/

resources/tutorials/hello-world.html •  Start Eclipse (Start -> All Programs -

> Eclipse) •  Create an Android Virtual Device

(AVD) •  Create a New Android Project

Page 11: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

11  

Android SDK •  Once  installed  open  the  SDK  Manager  •  Install  the  desired  packages  •  Create  an  Android  Virtual  Device  (AVD)  

SDK Manager

AVD

Page 12: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

12  

Creating a Project (1)

Creating a Project (2)

Need    the    items    circled    Then  click  Finish  

Package Content Java code for our activity All source code here

Generated Java code Helps link resources to Java code

Layout of the activity

Strings used in the program

All non-code resources

Android Manifest

Images

Page 13: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

13  

Android Manifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.helloandroid" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HelloAndroid" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

Activity

•  An Android activity is focused on a single thing a user can do.

•  Most applications have multiple activities

Activities start each other

Page 14: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

14  

Revised HelloAndroid.java package com.example.helloandroid;

import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello, Android – by hand"); setContentView(tv); } } Set the view “by

hand” – from the program

Inherit from the Activity Class

Run it!

/res/layout/main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>

Further redirection to

/res/values/strings.xml

Page 15: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

15  

/res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, HelloAndroid – by resources!</string> <string name="app_name">Hello, Android</string> </resources>

HelloAndroid.java

package  com.example.helloandroid;    import  android.app.AcUvity;  import  android.os.Bundle;  public  class  HelloAndroid  extends  AcUvity  {            

 /**  Called  when  the  acUvity  is  first  created.  */            @Override            public  void  onCreate(Bundle  savedInstanceState)  {      super.onCreate(savedInstanceState);                              setContentView(R.layout.main);            }  

}   Set the layout of the view as described in the main.xml layout

Introduce a bug package com.example.helloandroid;

import android.app.Activity; import android.os.Bundle; public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Object o = null; o.toString(); setContentView(R.layout.main); } }

Page 16: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

16  

Run it!

References •  This  tutorial  is  a  brief  overview  of  some  major  concepts…  Android  is  much  richer  and  more  complex  

•  Developer’s  Guide  –  hop://developer.android.com/guide/index.html  

•  API  Reference  –  hop://developer.android.com/reference/packages.html  

XML •  Used  to  define  some  of  the  resources  – Layouts  (UI)  – Strings  

•  Manifest  file  •  Shouldn’t  usually  have  to  edit  it  directly,  Eclipse  can  do  that  for  you  

•  Preferred  way  of  creaUng  UIs  – Separates  the  descripUon  of  the  layout  from  any  actual  code  that  controls  it  

– Can  easily  take  a  UI  from  one  plaKorm  to  another  

Page 17: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

17  

R Class •  Auto-­‐generated:  you  shouldn’t  edit  it  •  Contains  IDs  of  the  project  resources  •  Enforces  good  soaware  engineering  •  Use  findViewById  and  Resources  object  to  get  access  to  the  resources  – Ex.  Buoon  b  =  (Buoon)findViewById(R.id.buoon1)  – Ex.  getResources().getString(R.string.hello));  

Layouts (1) •  Eclipse  has  a  great  UI  creator  – Generates  the  XML  for  you  

•  Composed  of  View  objects  •  Can  be  specified  for  portrait  and  landscape  mode  – Use  same  file  name,  so  can  make  completely  different  UIs  for  the  orientaUons  without  modifying  any  code  

Layouts (2)

Page 18: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

18  

Layouts (3) •  Click  ‘Create’  to  make  layout  modificaUons  •  When  in  portrait  mode  can  select  ‘Portrait’  to  make  a  

res  sub  folder  for  portrait  layouts  –  Likewise  for  Landscape  layouts  while  in  landscape  mode  –  Will  create  folders  Utled  ‘layout-­‐port’  and  ‘layout-­‐land’  

•  Note:  these  ‘port’  and  ‘land’  folders  are  examples  of  ‘alternate  layouts’,  see  here  for  more  info  –  hop://developer.android.com/guide/topics/resources/providing-­‐resources.html    

•  Avoid  errors  by  making  sure  components  have  the  same  id  in  both  orientaUons,  and  that  you’ve  tested  each  orientaUon  thoroughly  

Layouts (4)

Manifest File – Adding an Activity

Page 19: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

19  

Android Programming Components

•  AcUvity  –  hop://developer.android.com/guide/topics/fundamentals/acUviUes.html  

•  Service  –  hop://developer.android.com/guide/topics/fundamentals/services.html  

•  Content  Providers  •  Broadcast  Receivers  

•  Android  in  a  nutshell:  –  hop://developer.android.com/guide/topics/fundamentals.html  

Activities •  The  basis  of  Android  applicaUons  •  A  single  AcUvity  defines  a  single  viewable  screen  –  the  acUons,  not  the  layout  

•  Can  have  mulUple  per  applicaUon  •  Each  is  a  separate  enUty  •  They  have  a  structured  life  cycle  – Different  events  in  their  life  happen  either  via  the  user  touching  buoons  or  programmaUcally  

Services •  Run  in  the  background  

–  Can  conUnue  even  if  AcUvity  that  started  it  dies  –  Should  be  used  if  something  needs  to  be  done  while  the  user  is  not  

interacUng  with  applicaUon  •  Otherwise,  a  thread  is  probably  more  applicable  

–  Should  create  a  new  thread  in  the  service  to  do  work  in,  since  the  service  runs  in  the  main  thread  

•  Can  be  bound  to  an  applicaUon  –  In  which  case  will  terminate  when  all  applicaUons  bound  to  it  unbind  –  Allows  mulUple  applicaUons  to  communicate  with  it  via  a  common  

interface  •  Needs  to  be  declared  in  manifest  file  •  Like  AcUviUes,  has  a  structured  life  cycle  

Page 20: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

20  

USB Debugging •  Should  be  enabled  on  phone  to  use  developer  features  

•  In  the  main  apps  screen  select  Sexngs  -­‐>  ApplicaUons  -­‐>  Development  -­‐>  USB  debugging  (it  needs  to  be  checked)  

Android Debug Bridge •  Used  for  a  wide  variety  of  developer  tasks  – Read  from  the  log  file  – Show  what  android  devices  are  available  –  Install  android  applicaUons  (.apk  files)  

•  In  the  ‘plaKorm-­‐tools’  directory  of  the  main  android  sdk  directory  – Recommend  puxng  this  directory  and  the  ‘tools’  directory  on  the  system  path  

•  adb.exe  

Debugging •  Instead  of  using  tradiUonal  System.out.println,  use  the  Log  class  

–  Imported  with  android.uUl.Log  –  MulUple  types  of  output  (debug,  warning,  error,  …)  –  Log.d(<tag>,<string>)  

•  Can  be  read  using  logcat.  –  Print  out  the  whole  log,  which  auto-­‐updates  

•  adb  logcat  –  Erase  log  

•  adb  logcat  –c  –  Filter  output  via  tags  

•  adb  logcat  <tag>:<msg  type>  *:S  •  can  have  mulUple  <tag>:<msg  type>  filters  •  <msg  type>  corresponds  to  debug,  warning,  error,  etc.  •  If  use  Log.d(),  then  <msg  type>  =  D  

•  Reference  –  hop://developer.android.com/guide/developing/debugging/debugging-­‐log.html  

Page 21: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

21  

60

Introduction to Objective-C and iPhone/iOS Development

Introduction •  ObjecUve-­‐C  is  implemented  as  set  of  extensions  to  the  C  language.    

•  It's  designed  to  give  C  a  full  capability  for  object-­‐oriented  programming,  and  to  do  so  in  a  simple  and  straighKorward  way.    

•  Its  addiUons  to  C  are  few  and  are  mostly  based  on  Smalltalk,  one  of  the  first  object-­‐oriented  programming  languages.  

Why Objective C •  ObjecUve-­‐C  incorporates  C,  you  get  all  the  benefits  of  C  when  

working  within  ObjecUve-­‐C.    •  You  can  choose  when  to  do  something  in  an  object-­‐oriented  

way  (define  a  new  class,  for  example)  and  when  to  sUck  to  procedural  programming  techniques  (define  a  struct  and  some  funcUons  instead  of  a  class).  

•  ObjecUve-­‐C  is  a  simple  language.  Its  syntax  is  small,  unambiguous,  and  easy  to  learn.  

•  ObjecUve-­‐C  is  the  most  dynamic  of  the  object-­‐oriented  languages  based  on  C.  Most  decisions  are  made  at  run  Ume.  

Page 22: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

22  

Object-Oriented Programming •  The  insight  of  object-­‐oriented  programming  is  to  combine  state  and  behavior,  data  and  operaUons  on  data,  in  a  high-­‐level  unit,  an  object,  and  to  give  it  language  support.    

•  An  object  is  a  group  of  related  funcUons  and  a  data  structure  that  serves  those  funcUons.  The  funcUons  are  known  as  the  object's  methods,  and  the  fields  of  its  data  structure  are  its  instance  variables.    

The Objective-C Language •  The  ObjecUve-­‐C  language  is  fully  compaUble  with  ANSI  standard  C.  

•  ObjecUve-­‐C  can  also  be  used  as  an  extension  to  C++.    

•  Although  C++  itself  is  a  Object-­‐Oriented  Language,  there  are  differences  in  the  dynamic  binding  from  ObjecUve-­‐C.  

Objective-C Language (cont.) •  ObjecUve-­‐C  source  files  have  a  “.m”  extension  •  “.h”  file  is  the  interface  file  •  For  example:  – main.m    – List.h  (Interface  of  List  class.)    – List.m  (ImplementaUon  of  List  class.)    

Page 23: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

23  

ID •  “id” is a data type used by Objective-C to

define a pointer of an object (a pointer to the object’s data).

•  Any type of object, as long as it is an object, we can use the id data type.

•  For example, we can define an object by: id anObject; •  nil is the reserved word for null object.

Dynamic Typing •  “id”  data  type  has  no  informaUon  about  the  object.  

•  Every  object  carries  with  it  an  isa  instance  variable  that  idenUfies  the  object's  class,  that  is,  what  kind  of  object  it  is.  

•  Objects  are  thus  dynamically  typed  at  run  Ume.  Whenever  it  needs  to,  the  run-­‐Ume  system  can  find  the  exact  class  that  an  object  belongs  to,  just  by  asking  the  object.  

Messages •  To  get  an  object  to  do  something,  you  send  it  a  message  telling  it  to  apply  a  method.  In  ObjecUve-­‐C,  message  expressions  are  enclosed  in  square  brackets  

[receiver message]

•  The  receiver  is  an  object.  The  message  is  simply  the  name  of  a  method  and  any  arguments  that  are  passed  to  it.  

Page 24: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

24  

Messages (cont.) •  For  example,  this  message  tells  the  myRect  object  to  perform  its  display  method,  which  causes  the  rectangle  to  display  itself  

[myRect display];

[myRect setOrigin:30.0 :50.0];    •  The  method  setOrigin::,  has  two  colons,  one  for  each  of  its  arguments.  The  arguments  are  inserted  aaer  the  colons,  breaking  the  name  apart    

Polymorphism •  Each  object  has  defined  its  own  method  but  for  a  different  class,  they  can  have  the  same  method  name  which  has  a  totally  different  meaning.  

•  The  two  different  objects  can  respond  differently  to  the  same  message.  

•  Together  with  dynamic  binding,  it  permits  you  to  write  code  that  might  apply  to  any  number  of  different  kinds  of  objects,  without  having  to  choose  at  the  Ume  you  write  the  code  what  kinds  of  objects  they  might  be.  

 

71

Inheritance

•  Root  class  is  NSObject  •  Inheritance  is  cumulaUve.  A  

Square  object  has  the  methods  and  instance  variables  defined  for  Rectangle,  Shape,  Graphic,  and  NSObject,  as  well  as  those  defined  specifically  for  Square.  

Page 25: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

25  

Inheritance (cont.) •  Instance  Variables:  The  new  object  contains  not  only  the  

instance  variables  that  were  defined  for  its  class,  but  also  the  instance  variables  defined  for  its  super  class,  all  the  way  back  to  the  root  class.  

•  Methods:  An  object  has  access  not  only  to  the  methods  that  were  defined  for  its  class,  but  also  to  methods  defined  for  its  super  class.  

•  Method  Overriding:  Implement  a  new  method  with  the  same  name  as  one  defined  in  a  class  farther  up  the  hierarchy.  The  new  method  overrides  the  original;  instances  of  the  new  class  will  perform  it  rather  than  the  original.    

Class Objects •  Compiler  creates  one  class  object  to  contain  the  informaUon  for  the  name  of  class  and  super  class.  

•  To  start  an  object  in  a  class:   id myRectx; myRect = [[Rectangle alloc] init];

•  The alloc method returns a new instance and that instance performs an init method to set its initial state.  

Defining a Class •  In  ObjecUve-­‐C,  classes  are  defined  in  two  parts:    – An  interface  that  declares  the  methods  and  instance  variables  of  the  class  and  names  its  super  class    

– An  implementaGon  that  actually  defines  the  class  (contains  the  code  that  implements  its  methods)    

Page 26: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

26  

The Interface •  The  declaraUon  of  a  class  interface  begins  with  the  compiler  direcUve  @interface  and  ends  with  the  direcUve  @end  

 @interface ClassName : ItsSuperclass { instance variable declarations } method declarations

@end

Declaration •  Instance Variables float width; float height; BOOL filled; NSColor *fillColor;

•  Methods: –  names of methods that can be used by class objects, class

methods, are preceded by a plus sign + alloc

–  methods that instances of a class can use, instance methods, are marked with a minus sign: - (void) display;

Declaration (cont.) •  Impor;ng  the  Interface:  The  interface  is  usually  included  with  the  #import  direcUve    

#import "Rectangle.h"

•  To reflect the fact that a class definition builds on the definitions of inherited classes, an interface file begins by importing the interface for its super class

•  Referring to Other Classes: If the interface mentions classes not in this hierarchy, it must declare them with the @class directive:

@class Rectangle, Circle;

Page 27: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

27  

The Implementation

#import "ClassName.h" @implementation ClassName method definitions

@end - makeIdenticalTwin { if ( !twin ) { twin = [[Sibling alloc] init]; twin->gender = gender; twin->appearance = appearance; } return twin;

}

Implementation (cont.) Example:  @interface Worker : NSObject { char *name;

@private int age; char *evaluation;

@protected id job; float wage;

@public id boss;

}

Implementation (cont.) - promoteTo:newPosition {

id old = job; job = newPosition;

return old;

}

Page 28: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

28  

GCC and Objective-C •  ObjecUve-­‐C  is  layered  on  top  of  the  C  language  •  iPhone  &  iPad  “naUve”  applicaUons  are  wrioen  in  ObjecUve  C  

•  The  Apple  dev  kit  for  ObjecUve-­‐C  is  called  “Cocoa”  •  Can  be  wrioen  on  any  computer  that  has  GCC  plus  “GNUstep”  plug-­‐in  

•  Apple  computers  have  all  of  this  pre-­‐installed,  and  also  have  an  iPhone  simulator  in  the  XCode  IDE  

Tools •  Apple:  pre-­‐installed  with  the  Cocoa  frameworks  –  XCode  or  GCC  in  terminal  window  

•  Ubuntu:  GnuStep  is  a  free  clone  of  Cocoa  –  sudo  aptget  –  install  buildessenUals  –  gobjc  gnustep  –  gnustepmake  –  gnustepcommon  –  libgnustepbasedev  

•  Windows:  hop://www.gnustep.org/  

iOS programming •  Event  driven  framework  •  Interface  Designer  has  some  extra  macros  for  the  code  that  act  like  hooks  to  variables;  –  IBAcUon  -­‐  trigger  events  like  mouse  clicks  –  IBOutlet  -­‐  captures  outputs  from  events  

•  These  tags  are  not  compiled  (don't  affect  the  code)  but  sit  as  an  extra  before  variables  that  the  Interface  Designer  can  see.  

Page 29: Mobile Computing - University of Notre Damecpoellab/teaching/cse40814_fall14/Lecture3-A… · 9/2/14 4 Mobile website AdapUve&design,&responsive&design,&mediaqueries& Mobile website

9/2/14  

29  

Start a New XCode iOS Project •  New  Project  →  iPhone/iPad  OS  •  NavigaUon-­‐Based  -­‐  “navigaUon  controller”  as  used  in  Contacts  app.  

•  OpenGL  ES  -­‐  cut-­‐down  OpenGL  •  Tab  Bar  –  app  in  style  of  iPod  app.  •  UUlity  –  Flipside  app  in  style  of  stock  quote  app.  •  View-­‐Based  –  single  view  that  you  draw  into  and  display  to  screen.  

•  Window-­‐Based  –  basic  app  with  a  main  window.  (we  will  use  this  one  for  examples.)  

Try Out - Hello World

•  Watch  the  Hello  World  tutorial  and  build  your  first  iPhone  applicaUon.  

•  The  tutorial  can  be  found  at:          hop://www.youtube.com/watch?v=lR-­‐t5vEpueQ