Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Lessons from developing an Iphone App + Server backend


Published on

I share some of the lessons learned while building an iphone app (Discounts For Me) and building the server back end in ruby on rails.

This one geared more towards rails programmers.
(presented at Silicon Valley Ruby Meetup on Sept 2009)

web :

Published in: Technology, Sports

Lessons from developing an Iphone App + Server backend

  1. Lessons from developing anIphone App + Server backend<br />Sujee Maniyam<br /><br /><br /><br />Sep 2009<br />
  2. Target Audience<br />Iphone app developers<br />Server backend developers for mobile apps<br />Expert level: Beginner - Intermediate<br />
  3. My Background<br />Developer (enterprise, web)<br />Java / Php / Ruby<br />First iphone/mobile app<br />
  4. App: DiscountsForMe<br />Shows member benefits<br />Based on location<br />V2.0 in app store<br />Memberships:<br />Public radio (KQED, WHYY)<br />Bank of America card<br />AAA, AARP<br />More…<br />
  5. Architecture<br />Server ( serves data<br />Server is Rails app<br />Iphone app talks to the server<br />&lt;Insert usual SERVER ---- INTERNET CLOUD ---- IPHONEpicture here&gt;<br />
  6. Connectivity : Simple Start<br />First cut : App made three server calls at startup<br />ping()<br />get_A()<br />get_B()<br />Simulator <br />Iphone over Wi-fi<br />Iphone over 3G <br />LAG-TIME is a problem<br />
  7. Connectivity : Minimize Lag Time<br />Noticeable lag time over 3G/Edge<br />Reducing lag time<br />Download in background<br />Condense network calls (especially if the user is waiting for data)<br />So<br />Get_A()<br />Get_B()<br />get_A_and_B()<br />
  8. Iphone Connectivity<br />BIG LESSON 1 : <br />Test on IPHONE (not just simulator)<br />Test with WiFi OFF! (3G can be slow to connect, EDGE even worse)<br />You may need to reorganize the logic to improve response time (I had to)<br />LESSON 2<br />Test in AirPlane Mode (all RADIOS off)(a frequent reason network apps are rejected )<br />
  9. Talking to Server : Format<br />Two choices : XML, JSON<br />JSON smaller size than XML (50% less)<br />Json : use TouchJSON library<br />XML : NSXML(sdk) / TouchXML / KissXML<br />Rails makes it real easy to send Json/xml<br />Some_obj.to_json<br />Some_obj.to_xml<br />
  10. Keeping it small<br />Trim objects<br />No need to send all attributes<br />Active records have extra attributes (created_at, updated_at ..etc)<br />Example:<br />my_response = {}<br />my_response[:book_name] =<br />my_response[:book_description] = book.description<br />my_response[:author_name] =<br />render(:json =&gt; my_response.to_json())<br />- Compress (zip) response<br />
  11. Agenda<br />Connectivity<br />Data format<br />Secure Data transfer<br />UDIDs, Keys, analytics<br />Controlling app from server<br />
  12. Secure Data Transfer<br />Plain HTTP is fine most of the time<br />If you want to secure data<br />Symmetric key encryption (shared ‘seckr3t’ key on Iphone app and server)<br />Public-private key encryption (e.g. SSH) : private key on server, public key on iphone<br />httpS<br />
  13. Secure data transfer : httpS<br />SSL is ‘good enough’ for most of us<br />Get a proper SSL certificate ($30). Self-signed certs don’t work by default<br />Beware connection time is a little longer for httpS<br />Verify your ssl certificate is installed properly<br />
  14. Verify SSL Cert…<br />
  15. Agenda<br />Connectivity<br />Data format<br />Secure Data transfer<br />UDIDs, Keys, multiple versions, analytics<br />Controlling app from server<br />
  16. What do I send to the server?<br />Think about including<br />UDID (device id)<br />And a Key (compiled within the app)<br /><br />Why?<br />
  17. Unique Device ID (UDID)<br />Each iphone has a unique ID, etched in hardware (just like MAC address)<br />Your app can send UDID with each request<br />Uses<br />metrics on app usage<br />Easy account creation (no signup)<br />
  18. Identify a User (Device)<br />UDID can help you ‘auto –create’ accounts on server<br />Eg. High scores of games<br />Allow users to create a custom user name later<br />Beware of a user using multiple devices (multiple UDIDs)<br />
  19. Metrics<br />A) Client Side metrics<br />Code embedded in your iphone app<br />Users (new, repeat), session length<br />Few companies (Flurry, Pinch Media ..etc)<br />Server side metrics<br />Response time<br />Response size<br />Other<br />
  20. Metrics : Client Side<br />
  21. Server Side Metric : Response Time<br />Log every thing in PARAMS plus more<br />E.g. Want to measure the time spent on each request<br />use around_filter in Controllerclass MyControlleraround_filter :log_access, :only =&gt; [:get_A]<br />
  22. Response Time …<br />def log_access<br />start_time =<br />yield<br />end_time =<br />elapsed = ((end_time - start_time)*1000.0).to_int<br />End <br />
  23. Response Size<br />def log_access<br />start_time =<br />yield<br />end_time =<br />elapsed = ((end_time - start_time)*1000.0).to_int<br />response_data_size = response.body.length<br />End <br />
  24. Response Time Chart<br />Time (ms)<br />
  25. Response Size Chart<br />Response size (kbytes)<br />
  26. Access keys<br />Keys are random, ‘sekret’ strings compiled into the iphone app<br />Sample key = “iphone_v1.0_xklajdfoi2” (human readable + ‘hard to guess’)<br />Start using ‘access keys’ from day-1<br />Each request to server must have a valid key<br />Uses<br />Easy to control client access (Prevent scraping, DOS ..etc)<br />Monitoring (what versions are being used)<br />Support multiple versions, easy upgrade<br />
  27. Access Keys<br />In controller:<br /> @@keys = [ &quot;iphone_v0.0_foobar” , <br /> &quot;iphone_v1.0_afajiu” ,<br /> &quot;iphone_v2.0_fi98d”,<br /> &quot;iphone_v2.0_plus_fsafa” ,<br /> &quot;android_v1.0_fasjlkuo”<br /> ]<br />@@keys_premium = [&quot;iphone_v2.0_plus_fsfa&quot;]<br />
  28. Supporting multiple versions<br />May be supporting 2-3 client versions at a time (users don’t always run the latest)<br />Keep old ‘API’ around, build-out new API if (is_v2_or_later(key)) { do something } else {do some thing else}<br />This can get convoluted (see next page…)<br />
  29. Supporting multiple clients…<br />
  30. Supporting Multiple Clients…<br />Have different controllers handle different client versions#define SERVER @””#define SERVER @””<br />Make sure to avoid code duplication<br />Plan-B : End-of-life<br /> If ( ! is_supported_version(key)){send_msg(“please upgrade”);}<br />
  31. Server side : keeping it secure<br />Make sure ‘secret stuff’ doesn’t get logged in log-files<br />In Rails : class Mobile::MobileController &lt; ApplicationControllerfilter_parameter_logging [:key, :uid]<br /> end<br />Output:<br />Processing IphoneController#get_memberships_and_discounts (for at 2009-07-02 16:07:41) [POST]<br /> Session ID: 126e5a73742f92f85c1158ea63fd960a<br /> Parameters: {&quot;loc&quot;=&gt;&quot;39.282440,-76.765693&quot;, &quot;action&quot;=&gt;&quot;get_memberships_and_discounts&quot;, &quot;uid&quot;=&gt;”[FILTERED]&quot;, &quot;controller&quot;=&gt;&quot;mobile/iphone&quot;, &quot;dist&quot;=&gt;&quot;25&quot;, &quot;mems&quot;=&gt;&quot;&quot;, &quot;key&quot;=&gt;&quot;[FILTERED]&quot;}<br />
  32. Example : Controllers<br />MobileController<br />IPhoneController &lt; MobileController<br />AndroidController &lt; MobileController<br />Most of the shared logic in ‘MobileController’<br />
  33. Example …<br />Class MobileController<br /> @@valid_keys = [……]<br /> def ping<br />to_ret = {}<br /> begin<br /> validate<br />to_ret[:status] = “OK”<br /> rescue<br />to_ret[:error] = $1.message<br /> end<br /> render (:json =&gt; to_ret.to_json)<br /> end<br />end<br />
  34. Example …<br />Def validate<br /> #verify the key<br /> if (params[:key].blank?)<br /> raise DiscountsError, &quot;dude, where is my key?&quot;<br /> end<br /> if (params[:uid].blank?)<br /> raise DiscountsError, &quot;dude, who are you?&quot;<br /> end<br /> unless (@@valid_keys .has_key?(params[:key]))<br /> raise DiscountsError, &quot;un supported version, please upgrade&quot;<br /> end <br /> end<br />end<br />
  35. Controlling app behavior from Server<br />
  36. Control …<br />Apps changes are not easy to ‘get out’<br />Approval process takes time<br />Users may not upgrade to latest version<br />Server changes are under your control and easy to deploy<br />So build in control-switches in the app, that can be directed from server<br />
  37. Control…<br />One example: Choosing if you are going to show ads?<br />show_ads : {none | admob | tapjoy}<br />
  38. Hosting<br />Shared hosting is fine, but others might swamp your DB, CPU ..etc <br />If you can, get a VPS (Virtual Private Server)<br />Plans start from $20 / month (SliceHost, Hosting-Rails ..etc)<br />You have full ROOT access to the server (install packages, run CRON jobs ..etc)<br />EC2 is great (for testing, scaling)<br />
  39. Thanks!<br />Sujee Maniyam<br /><br /><br /><br />Questions?<br />