440 likes | 611 Views
Wednesday 2-4, DH 1046. COMP 446 / ELEC 446 Mobile Device Applications. Scott Cutler Professor in the Practice of Computer Technology Department of Computer Science Department of Electrical and Computer Engineering cutler@rice.edu 9/28/11. Today’s Agenda. Events of the week
E N D
Wednesday 2-4, DH 1046 COMP 446 / ELEC 446 Mobile Device Applications Scott Cutler Professor in the Practice of Computer Technology Department of Computer Science Department of Electrical and Computer Engineering cutler@rice.edu 9/28/11
Today’s Agenda • Events of the week • Final Project Schedule • Web Services(abbreviated) • Memory Management Summary Sheet • Controllers of Controllers, Gesture Recognizers • Review of current assignment • Next Week COMP 446 / ELEC 446 - Week 8
Events of the Week • Apple holding iPhone 5 event on October 4 • HP Ousts Apotheker, Names Whitman CEO • Facebook's Zuckerberg unveils revamped profile: Timeline • AT&T, T-Mobile Antitrust Case Set for Trial in February 2012 • Amazon Kindle Book Loans Debut at 11,000 Libraries • Amazon Kindle Tablet Landing Sept. 28 • Apple slashes fourth-quarter iPad orders, report says • Analysts dubious about iPad supply issues COMP 446 / ELEC 446 - Week 8
COMP 446 Syllabus • 8/24/11 Introduction / What makes mobile apps special Videos: Lecture 1. Introduction to Cocoa Touch, Objective-C, Tools, and MVC (September 21, 2010) Lecture 2. Building a Simple Calculator (September 23, 2010) Assignment: #1 and #1 Walkthrough - Calculator Due: Preferably watch videos this week, definitely by 8/30. Assignment due 9/1/11. • 8/31/11 Discussion of first applications, Development Environment Videos: Lecture 3. Objective-C and Foundation Frameworks (September 28, 2010) Lecture 4. Foundation and Memory Management (September 30, 2010) Assignment: #2 Function Calculator (Due 9/15/11) • 9/7/11 Intro to Cocoa Touch, Objective-C, Using Objective-C, Foundation framework Videos: Assignment: • 9/14/11Memory Management Videos: Lecture 5. Protocols and Views (October 5, 2010) Lecture 6. Application & View Controller Lifecycle, Navigation Controller (October 7, 2010) Assignment: #3 Graphing Calculator (Due 9/22/11) • 9/21/11 iPhone (pseudo) Web Apps / ASP.Net / Browser Capabilities Videos: Lecture 7. More Controllers of Controllers, iPad, Universal Applications (October 12, 2010) Lecture 8. Gesture Recognizers (October 14, 2010) Assignment: #4 Universal Calculator (Due 9/29/11) COMP 446 / ELEC 446 - Week 8
COMP 446 Syllabus • 9/28/11 Creating and Consuming Web Services Videos: Lecture 9. Image View, Web View, and Scroll View (October 19, 2010) Lecture 10. Table View (October 21, 2010)Assignment: #5 Pictures Places (Due 10/13/11) • 10/5/11 SQL 101 Videos: Lecture 11. Persistence (October 26, 2010) Lecture 12. Core Data and Table Views (October 28, 2010) Assignment: #6 Core Data Places (Due 10/20/11, but watch lectures prior to 10/12/11 class) • 10/12/11 Final Project discussion and Pairing Videos: Debugging Tools (October 22, 2010)Kleiner Perkins iFund (October 15, 2010) Assignment: Final Project Proposal (Due 10/18/11 along with Core Data Places due 10/20/11) • 10/19/11 iOS 5 Videos: Lecture 13. Blocks and Multithreading (November 2, 2010) Lecture 14. Core Location and Map Kit (November 9, 2010) Assignment: Final Project, Stage 1 (Due 10/27/11) • 10/26/11 Android Videos: Lecture 15. Editable Text, Modal View Controllers, and View Animation (Nov 11, 2010) Lecture 16. Core Motion, Segmented Control, and Alerts (November 16, 2010) Assignment: Advanced Feature App (Due 11/03/11) COMP 446 / ELEC 446 - Week 8
COMP 446 Syllabus • 11/2/11 Windows Phone 7, Final Project Prep Videos: Lecture 17. Media (November 18, 2010) Lecture 18. Accessibility on iOS: Make an App for Everyone (November 30, 2010) Assignment: Final Project, Stage 2 (Due 11/10/2011) • 11/09/11 Final Project Prep / iPhone v. Android v. Windows Phone 7 group discussion Videos: Lessons from Bezos, Pincus, Young: CEO 2.0 (November 12, 2010) (Optional) LinkedIn: Shipping with CoreData (November 5, 2010) (Optional) Assignment: Final Project, Stage 3 (Due 11/17/2011) • 11/16/11 Final Project Prep Videos: Building Flipboard (November 19, 2010) (Optional) Assignment: Final Project Presentation (Due 11/30/2011) • 11/23/11 Thanksgiving – Continue Final Projects on your own • 11/30/11 Demonstrate Final Projects to class • 12/4/11 Class Offsite – Sunday (tentative) • Finals Submission of final code due by official finals date COMP 446 / ELEC 446 - Week 8
Duncan Hall Kiosk Final Project • Possible Final project for one or two teams of one or two people. • Replace Duncan Hall Kiosk with iPad version • Person Lookup • Location map • Event Database • 911 function (template) • Billboard mode • Management Console • Locked Application • Possible second semester continuation project COMP 446 / ELEC 446 - Week 8
Creating and Consuming Web Services COMP 446 / ELEC 446 - Week 8
Web Services (abbreviated) • The ability to programmatically access remote servers over a web like interface • The web as a subroutine • The web without a browser • The ability to make a large serve be an extension of your iOS app. COMP 446 / ELEC 446 - Week 8
Three Popular Types • Simple Object Access Protocol (SOAP) • Protocol relying on XML, RPC and HTTP • More of a Client/Server tunnel over HTTP • Easy to use, Rigid (defined), development tools • REpresentationalState Transfer (RESTful) • Developed in 2000 by Roy Fielding in his doctoral dissertation. • Interface uses standard HTTP protocols • Lightweight, human readable, no toolkit required • JSON – JavaScript Object Notation • Alternative to and lighter version of XML • Freeform “key” : value pairs • Not as secure as some other methods • Similar to iOS Property Lists • Jayrock – open sourced JSON interface for ASP.Net COMP 446 / ELEC 446 - Week 8
RESTFul Example SubMoviesInfo() Dim URL AsNewStringBuilder URL.Append("http://www.example.com/movies.main") URL.Append("?var1=movies") ‘? Followed by key=valueURL.Append("&zip=") ‘& Followed by key=valueURL.Append(tbZip.text) ‘assumes a textbox named zip MovieInfoDS= NewDataSet Try MovieInfoDS.ReadXml(XmlReader.Create(URL.ToString())) IfShowInfoDS.Tables.Count = 0 Then . . . EndIf Catchex AsException . . . EndTry EndSub COMP 446 / ELEC 446 - Week 8
REST Used Extensively by Many • Amazon (Offers both REST and SOAP) • eBay • Facebook • Yahoo! • YouTube COMP 446 / ELEC 446 - Week 8
SOAP with ASP.Net • SOAP is more like a natural subroutine call • Parameters and results have strong data types • Various Discovery services • Development toolkits handle complexity • Many publically available • http://www.webservicex.net/WS/wscatlist.aspx • http://www.xmethods.net • Amazon.com COMP 446 / ELEC 446 - Week 8
Create a Web Service • 2 Steps • Create a Web Services File • Right-Click your web project, select ‘Add New Item’ • Chose Web Service and give it a Name (uses the .asmx extension) • Write your functions • Code placed in App_Code folder under the name.vb file • Functions preceded with the ‘<WebMethod()> _’ tag <WebMethod()> _ Public Function ByTwo(ByValInputValue As Integer) As Integer Return InputValue * 2 End Function COMP 446 / ELEC 446 - Week 8
Using JSON • Reading a JSON string into Foundation objects • #import <JSON/JSON.h> • // Get a JSON string from the cloud • NSString *jsonString = [NSStringstringWithContentsOfURL:urlencoding:NSUTFBStringEncodingerror:nil]; • // Parsing will result in Foundation objects • // Top level may be an NSDictionaryor an NSArray • id object = [jsonStringJSONValue]; • Writing a JSON string from Foundation objects • // Create some data in your app • NSDictionary *dictionary = ...; • // Convert into a JSON string before sending to the cloud • jsonString = [dictionary JSONRepresentation]; COMP 446 / ELEC 446 - Week 8
Using JSON in Comp446 • Assignment #5 gets images from Flickr using provided FlickrFetcher class • FlickrFetcher uses JSON to get data • Most of this behind the scenes; just add classes to project and call class methods. • Will have to apply for a free FlickrAPI key • A bit more on Assignments 5 & 6 later COMP 446 / ELEC 446 - Week 8
Assignment #4 Comments COMP 446 / ELEC 446 - Week 8
Assignment #3 Comments • 75% of submissions had obvious memory management issues • Instantiating objects with only class methods • Missing boarder cases; “X Sqrt Graph” • Good news: Everyone passed on this assignment. COMP 446 / ELEC 446 - Week 8
Memory Management Cheat Sheet COMP 446 / ELEC 446 - Week 8
Memory Management of iVars • Declare in class header between { } NSArray *myArray; • @property declaration in header after { } @property (nonatomic, retain) NSArray *myArray; //nonatomic required when overriding setter • @synthesize iVar in implementation file @synthesize myArray; • Release in dealloc method; [myArray release]; COMP 446 / ELEC 446 - Week 8
@property Memory Attribute • Use (assign) for delegates and nothing for objects not needing memory management (assign is the default if nothing provided) • Use (copy) for NSStrings or other object which might get changed by other methods behind your back • Use (retain) for most other objects • Use nothing (equivalent to assign) for non-object iVars like double or for derived properties (properties not based on iVars) assign retain copy @property (assign) id <FooDelegate> delegate; @property (retain) NSArray *myArray; @property (copy) NSString *name; @synthesize delegate; // would create: @ synthesize myArray; // would create: @synthesize name; // would create: • (void)setDelegate: (id) newDelegate • { • delegate= newDelegate; • } //no memory management • (void)setMyArray: (NSArray *)value • { • if (value != myArray) { • [myArray release]; • myArray= [value retain]; } • } //Old released, new retained • (void)setName: (NSString *)value • { • if (value != name) { • [name release]; • name = [value copy]; } • } // Creates a copy which you own COMP 446 / ELEC 446 - Week 8
Handling IBOutlets • Declare in class header between { } UILabel *myLabel; • @property declaration in header after { } @property (retain) IBOutletUILabel *myLabel; • @synthesize iVar in implementation file @synthesize myLabel; • Create private release method to set values to nil -(void) releaseOutlets {self.myLabel=nil;} • Call releaseOutlets in dealloc AND viewDidUnload [self releaseOutlets]; COMP 446 / ELEC 446 - Week 8
Handling IBOutlets • Declare in class header between { } UILabel *myLabel; • @property declaration in header after { } @property (retain) IBOutletUILabel *myLabel; • @synthesize iVar in implementation file @synthesize myLabel; • Create private release method to set values to nil -(void) releaseOutlets {self.myLabel=nil;} • Call releaseOutlets in dealloc AND viewDidUnload [self releaseOutlets]; • Alternate to 4 & 5self.myLabel=nil; //in viewDidUnload [myLabel release]; //in dealloc COMP 446 / ELEC 446 - Week 8
Objects Created in Methods • For methods that return objects: • iVar objects created in setters or getters should be retained taking care to release any previous values • Methods starting with new, copy or init should return retained objects • Other methods should return autoreleased objects • Objects temporarily created in methods and not returned: • should be released, not autoreleased, before end of method • use autorelease only for temporary objects needed after end of method COMP 446 / ELEC 446 - Week 8
ViewController Memory Management • View Controllers retain their subviews • If you alloc/init a view controller or view and push it onto another controller, you should release it. COMP 446 / ELEC 446 - Week 8
Homework Memory Management • A good 75 percent of Assignment #3 had memory management issues covered by this cheat sheet. • I will be looking at the obvious areas covered in this cheat sheet. • If I find any leaks, I will reject the homework only letting you know that you are leaking. • You will have to find, fix and resubmit your homework. COMP 446 / ELEC 446 - Week 8
Videos 7 & 8 summary Slides based on CS 193p fall 2010 iTunes University videos COMP 446 / ELEC 446 - Week 8
UITabBarController • Switch between major independent sections of an app. • Each tab has its own view controller, often a UINavigationController - (BOOL)application:(UIApplication *) didFinishLaunchingWithOptions:(NSDictionary *) { UIViewController*vc1 = ...; UIViewController*vc2 = ...; UITabBarController*tbc = [[UITabBarControlleralloc] init]; tbc.viewControllers= [NSArrayarrayWithObjects: vc1, vc2, ..., nil]; [vc1 release]; [vc2 release]; [window addSubview:tbc.view]; [window makeKeyAndVisible]; return YES; } COMP 446 / ELEC 446 - Week 8
UITabBarItem • Property of UIViewController • Default button text = UIViewController’s title. • Can be set with custom values or system values - (void)setup // call from initWithNibName:bundle: and awakeFromNib { UITabBarItem*item = [[UITabBarItemalloc] initWithTitle:@”Timer” image:[UIImageimageNamed:@”timer.png”] // from Resources tag:0]; // identifying tag, can ignore self.tabBarItem= item; [item release]; } - (void)somethingHappenedToCauseUsToNeedToShowABadgeValue { self.tabBarItem.badgeValue= @“R”; } COMP 446 / ELEC 446 - Week 8
Combine NavController with TabBar • UITabBarController is top level • UINavigationController inside UITabBarController - (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)options { UINavigationController*nav1 = [[UINavigationControlleralloc] init]; UINavigationController*nav2 = [[UINavigationControlleralloc] init]; [nav1 pushViewController:...]; [nav2 pushViewController:...]; // here we would likely to want to release the view controllers pushed onto the navigation controllers UITabBarController*tbc = [[UITabBarControlleralloc] init]; // should be ivar released in dealloc tbc.viewControllers= [NSArray arrayWithObjects:nav1, nav2, nil]; [nav1 release]; [nav2 release]; [window addSubview:tbc.view]; [window makeKeyAndVisible]; return YES; } COMP 446 / ELEC 446 - Week 8
UISplitViewController • Side by side, master/slave controller added with introduction of iPad. - (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)options { UIViewController*left = ...; UIViewController*right = ...; UINavigationController*leftNav = [[UINavigationControlleralloc] init]; UINavigationController*rightNav = [[UINavigationControlleralloc] init]; [leftNavpushViewController:leftanimated:NO]; [rightNavpushViewController:rightanimated:NO]; [left release]; [right release]; // if they were alloc’ed above, that is UISplitViewController *svc = [[UISplitViewController alloc] init]; // “leak”, but app killed afte leak svc.viewControllers= [NSArrayarrayWithObjects:leftNav, rightNav, nil]; [leftNav release]; [rightNav release]; [window addSubview:svc.view]; [window makeKeyAndVisible]; return YES; } COMP 446 / ELEC 446 - Week 8
UISplitViewController Rotation - (void)splitViewController:(UISplitViewController*)sv willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController:(UIPopoverController*)popover { barButtonItem.title= aViewController.title; // this is the title of the left-hand vc self.navigationItem.rightBarButtonItem= barButtonItem; // if we are not in a UINavigationController this method (appropriately) does nothing } - (void)splitViewController:(UISplitViewController*)sv willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem*)barButtonItem { self.navigationItem.rightBarButtonItem= nil; } - (void)splitViewController:(UISplitViewController*)sv popoverController:(UIPopoverController *)popover willPresentViewController:(UIViewController *)leftViewController { // do nothing here because we don’t “know about” the left view controller } COMP 446 / ELEC 446 - Week 8
UIPopoverController • Does not inherit from UIViewController • Automatically generated with UISplitviewController • Can also be created independently • Create with - initWithContentViewController: • Present with - presentPopoverFromRect: …or • - presentPopoverFromBarButtonItem: … COMP 446 / ELEC 446 - Week 8
Universal Apps • Can create at onset or upgrade • Two different main.xib files • iPhone and iPad • Correct one automatically loaded • May have to choose if overlaying inside program • Programmatically check for type in many ways • if (self.splitViewController != nil) • if (self.view.window) • Check value of [[UIScreenmainScreen] bounds]; • BOOL iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad); COMP 446 / ELEC 446 - Week 8
Universal Application Launch - (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options { UIViewController*master = ...; UIViewController*detail = ...; master.subservientController= detail; // master pushes detail on iPhone only (subservient is created) UINavigationController*masterNav = [[UINavigationControlleralloc] init]; [masterNavpushViewController:masteranimated:NO]; if (iPad) { UINavigationController*detailNav = [[UINavigationControlleralloc] init]; [detailNavpushViewController:detailanimated:NO]; UISplitViewController*splitVC = [[UISplitViewControlleralloc] init]; splitVC.delegate= detail; // note: detail implements UISplitViewControllerDelegate splitVC.viewControllers= [NSArrayarrayWithObjects:masterNav, detailNav, nil]; [masterNav release]; [detailNav release]; rootVC= splitVC; // rootVC is an instance variable } else { rootVC= masterNav; // master will push detail on iPhone when appropriate } [window addSubview:rootVC.view]; return YES; } COMP 446 / ELEC 446 - Week 8
UIGestureRecognizer • Perfect example of a complex technology which is easy to implement on iOS. • Set of classes derived from UIGestureRecognizer • UIPanGestureRecognizer • UITapGestureRecognizer • UIPinchGestureRecognizer • UIRotationGestureRecognizer • UISwipeGestureRecognizer COMP 446 / ELEC 446 - Week 8
Two Phase Setup • Controller typically: • instantiates a specific gesture recognizer • Uses target/action to select object and method which will handle recognized gesture • Adds instance to the UIView class (and releases memory) • View typically: • Is target for recognizer. • Has methods to handle recognizer • Checks state and other property values to determine how to properly respond COMP 446 / ELEC 446 - Week 8
Recognizer Properties • UIPanGestureRecognizer • @property NSUIntegermaximumNumberOfTouches • @property NSUIntegerminimumNumberOfTouches • UIPinchGestureRecognizer • @property CGFloat scale; // note that this is NOT readonly (can reset each movement) • @property (readonly) CGFloat velocity; // note that this IS readonly; scale factor per second • UIRotationGestureRecognizer • @property CGFloat rotation; // note that this is NOT readonly; in radians • @property (readonly) CGFloat velocity; // note that this IS readonly; radians per second • UISwipeGestureRecognizer • This one you “set up” (w/the following) to find certain swipe types, then look for Recognized state • @property UISwipeGestureRecognizerDirection direction; // what direction swipes you want • @property NSUIntegernumberOfTouchesRequired; // two finger swipes? or just one finger? more? • UITapGestureRecognizer • Set up (w/the following) then look for Recognized state • @property NSUIntegernumberOfTapsRequired; // single tap or double tap or triple tap, etc. • @property NSUIntegernumberOfTouchesRequired; // e.g., require two finger tap? COMP 446 / ELEC 446 - Week 8
Videos 9 & 10 Preview Slides based on CS 193p fall 2010 iTunes University videos COMP 446 / ELEC 446 - Week 8
New Classes • UIImage and UIImageView • Image created with NSData, from file, from URL • Animations, Highlights, • UIWebView • Displays HTML, PDF, etc. inside UIView • Handles some JavaScript • UIScrollView • A UIView that can be panned and scaled • Parent class of UITextView and UITableView • UITableView COMP 446 / ELEC 446 - Week 8
UITableView • The canonical class used for efficiently displaying tables of data. • Creates and reuses UITableViewCells • Delegates • UITableViewDataSource for data • UITableViewDelegate for display information • Both often part of UTTableViewController COMP 446 / ELEC 446 - Week 8
Assignment #5 • No more calculator • Problem involves two major technologies • Getting data from the web (Flickr) • Using a UITableViewController to quickly scroll through items. • Flickr top places • Photos from the top places • Will require you to obtain a free FlickrAPI key. COMP 446 / ELEC 446 - Week 8
Assignment #6 Preview • Will start in 1 week. • Staggered overlapping due date with Assignment #5 • Three major parts • Core Data version of Assignment #5 • Addition of Favorite Places • Caching of image data to sandbox • Item 1 required, Items 2 and 3 required if you want possibility of A+ in class • While not required, if you want to create great apps, you should also do Assignment #7 – multi thread • Items 2 and 3 can be submitted at any time before last class COMP 446 / ELEC 446 - Week 8
Week 7 • Watch CS193p lectures #9 and #10 • Preferably before Saturday. Definitely before next class. • Start CS193p Assignment #5 • Try and complete before next class. Due 10/13/2011. • Assignment #6 (partial) will start next week • Assignment #6 due 10/20/2011. COMP 446 / ELEC 446 - Week 8