Running On Mobile How mobile device is different from desktop, and what you can do about it
Running On MobileHow mobile device is different from desktop, and what you can do about it
Mobile Device Characteristics
• Always around
• Always connected
• Used for both urgent tasks and fun
Users’ Expectations
• Battery lasts forever
• Apps work smoothly
• Seamless app switching
Developer’s Perspective
• Interruptions will happen all the time
• Consider battery in everything you do
Some stats
Android apps ad-serving responsible for
75% of app related battery drain
Mobile Architecture• Application Sandbox
• Getting out:
• Using OS services
• Talking to other apps
• Background Execution
Let’s start with a desktop
• On a desktop:
• File separation is user based
• A user can access all her files, everywhere
Let’s start with a desktop
1 file = File.open(“/Users/ynonperek/tmp/test.sh") 2 file.each do |line| 3 print line 4 end
Mobile Is Different
• Basic entity is the App
WhatsApp Mail Facebook
Mobile OS
Mobile Apps Storage: iOS
An app storage options
• Saving files on the file system
• Saving data in preferences (registry)
• Hybrid: Leverage browser storage
HTML5 Storage
Online/Offline functionality Persistant storage
Local Data Storage
• How do you store data on a client’s machine ?
Cookies
What’s wrong with cookies?• Before HTML5: cookies were used to store data on the
client side
• Cookies Disadvantages:
• Limited size (4k)
• Sent with every request
• Complex to use
Local Storage• A JavaScript API for storing
client side data
• Can store up to 5MB (per site)
• Simple API
• Never run out of space
Local Storage API
• Store data:
• Read data:
• Remove data:
localStorage.setItem('username', 'bob');
var stored_username = localStorage.getItem('username'); username = stored_username || 'Anonymous';
localStorage.removeItem('username');
Q & A
Offline Application
CacheDealing with low connectivity
(without stealing)
Offline Web App
• Online-Offline Apps
• Sync with the Cloud, but can suffer a downtime (Think Google Gears)
• Can run completely offline app as a standalone
• Mobile - Save bandwidth
Offline Web App
GET MANIFEST
CACHE MANIFEST index.html
style/main.css script/main.js
Cache Manifest File
• Lists all the files that should be stored for offline access
• Enable with:<html manifest="example.appcache">
Demo
• Application with a cache manifest
• Modifying manifest and updating
Manifest - The Good
• Can store any file locally
• Provides offline/online app functionality
• Transparent to the user
Manifest - The Bad
• Not suitable for data storage
• Complex update logic
Sensitive Data Storage
• Both iOS and Android support a keychain
• Safe storage for sensitive data managed by the OS
iOS Keychain Demo
KeychainItemWrapper *keychainItem = ! [[KeychainItemWrapper alloc] ! initWithIdentifier:@"YourAppLogin" ! accessGroup:nil];!
[keychainItem setObject:@"password you are saving" forKey:kSecValueData];![keychainItem setObject:@"username you are saving" forKey:kSecAttrAccount];
Init
Save username/password
NSString *password = [keychainItem objectForKey:kSecValueData];!NSString *username = [keychainItem objectForKey:kSecAttrAccount];
Retrieve username/password
Pop Quiz
• Planning a twitter clone, what data would you store and how?
Storage: Keep In Mind
• Storage helps you be prepared for interruptions
• Store application state whenever it matters
• Don’t let a user type twice
Q & A
Talking to Other Apps
The problem…
• In a sandbox, we can’t access another app’s data
• Desktop “start external process” is also not an option
Solution: URL Schemes
• Each app can register itself as a valid handler for some link type
• Each app can open an external link
• OS connects handler with action
Solution: URL Schemes
Instagram iOSregister scheme
instag://
Solution: URL Schemes
CoolCam open link
instag://photo/uploadiOS
iOS Instagraminstag://photo/upload
other request data
Demo: CoolNotes
• Register a scheme handler for : cnotes://!
• Add notes to app from another app or web
What Next
• Both Android and iOS support attaching files to other applications
• That requires launching from a native app
Online Resources
• Registering custom URL scheme on iOS: https://github.com/phonegap/phonegap/blob/master/lib/ios/guides/Cordova%20Custom%20URL%20Scheme%20Handling.md
• Registering custom URL scheme on Android:http://blog.cttapp.com/p/phonegap-handleopenurl-for-android/
Q & A
Background execution
Abusing a user’s phone when you’re not running
iOS vs. Android
• iOS is more limited in background execution
• Both allow apps to run when not in background
Android Services
• Non-GUI part of a program
• Can run while app is not active
• Can be used from other apps
iOS Application States
• iOS4 Added support for background execution
iOS Moving To Background
• By default all apps continue some sort of background activity
• Acting friendly is your responsibility
Background Activity
• By default main thread of the application is suspended
• Other threads (network) keep working
• Demo
Background Activity
• It’s possible to request main thread to stay active ~10 minutes after suspension
• Demo
Background Activity
• Apps that integrate with OS services can request to handle events
• Services include: audio, location, voip, external-accessory, bluetooth, network fetch, remote notifications
Discussion
• What are user’s expectations from apps running in the background ?
Abusing Mobile Devices
• Keep GPS on while app not running
• Send small analytics network packets to your servers
• Perform CPU intensive tasks while app not running
Q & A
Thanks For Listening
• Ynon Perek
• http://ynonperek.com