Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A head start with MonoTouch 6.0

Chris Hardy
October 18, 2012

A head start with MonoTouch 6.0

MonkeySpace 2012 presentation on "A head start with MonoTouch 6.0"

Chris Hardy

October 18, 2012
Tweet

More Decks by Chris Hardy

Other Decks in Technology

Transcript

  1. Chris Hardy • .NET Contractor • Work with Xamarin •

    Wrote some books • First spoke about MonoTouch in January 2010 • http://twitter.com/chrisntr
  2. What’s new for developers? • Social • UIKit Improvements •

    Maps • Reminders • UICollectionView • PassKit
  3. No more Snow Leopard • Xcode 4.5/ iOS 6.0 works

    on Lion and Mountain Lion, requires MonoTouch 6.0 • Xcode 4.5 removes support for ARMv6 devices • Xcode 4.4 / iOS 5.1.1 works on Snow Leopard, Lion and Mountain Lion with MonoTouch 5.4 and can build for ARMv6 devices • ARMv6 devices = iPhone (1st Generation), iPhone 3G, iPod Touch (1st and 2nd Generation) • Download Xcode 4.4 and install in /Applications/Xcode44.app and change location in MonoDevelop’s ‘SDK Locations’
  4. Handling the iPhone 5 Referencing “Default.png” splashView = new UIImageView(new

    RectangleF(0f, 0f, 320f, 480f)); splashView.Image = UIImage.FromFile("Default.png");
  5. Handling the iPhone 5 Referencing “Default.png” splashView = new UIImageView(new

    RectangleF(0f, 0f, 320f, 480f)); splashView.Image = UIImage.FromFile("Default.png"); splashView = new UIImageView(UIScreen.MainScreen.Bounds); if (UIScreen.MainScreen.Bounds.Height > 480f) splashView.Image = UIImage.FromFile("[email protected]"); else splashView.Image = UIImage.FromFile("Default.png"); Becomes
  6. Social Framework Twitter in iOS 5 var tweet = new

    TWTweetComposeViewController(); tweet.SetInitialText ("I'm in '" + DisplaySession.Title + "' at #monkeyspace" ); PresentModalViewController(tweet, true); if (TWTweetComposeViewController.CanSendTweet) { // Do Tweet option displaying here } Check for Twitter access
  7. Social Framework Twitter, Facebook and SinaWeibo in iOS 6 if

    (UIDevice.CurrentDevice.CheckSystemVersion(6,0) && SLComposeViewController.IsAvailable(SLServiceKind.Facebook)) { // Do Facebook option displaying here } var facebook = SLComposeViewController.FromService(SLServiceKind.Facebook); facebook.SetInitialText("I'm in '" + DisplaySession.Title + "' at #monkeyspace" ); PresentViewController(facebook, true, null);
  8. Social Framework UIActivityViewController from UIKit var message = "I'm in

    '" + DisplaySession.Title + "' at #monkeyspace"; var social = new UIActivityViewController(new NSObject[] { new NSString(message) }, new UIActivity[] { new UIActivity() }); PresentViewController(social, true, null); • Provide multiple objects - Strings, Images, Custom data that implement UIActivityItemSource • Include types of Activities you want to share with, can exclude activities such as PostToFacebook or E-mail
  9. UIKit Improvements UIRefreshControl • Implement ValueChanged event • Handle Being/EndRefreshing

    • Optional Title and TintColor NavigationItem.SetRightBarButtonItem (new UIBarButtonItem (UIBarButtonSystemItem.Refresh), false); NavigationItem.RightBarButtonItem.Clicked += (sender, e) => { Refresh(); }; Becomes if (UIDevice.CurrentDevice.CheckSystemVersion (6,0)) { // Display UIRefreshControl here } else if { NavigationItem.SetRightBarButtonItem (new UIBarButton.... NavigationItem.RightBarButtonItem.Clicked += (sender, e) => ... }
  10. UIKit Improvements UIRefreshControl RefreshControl = new UIRefreshControl(); RefreshControl.ValueChanged += HandleValueChanged;

    AppDelegate.Conference.OnDownloadSucceeded += (jsonString) => { Console.WriteLine ("OnDownloadSucceeded"); InvokeOnMainThread (() => { RefreshControl.EndRefreshing (); }); }; AppDelegate.Conference.OnDownloadFailed += (err) => { Console.WriteLine ("OnDownloadFailed"); InvokeOnMainThread (() => { RefreshControl.EndRefreshing (); }); };
  11. Maps • New Class - MKMapItem • Maps app in

    the simulator • Can send MKMapItems to the Maps app • Can receive MKMapItem data from
  12. Maps Sending data to the Maps app if(UIDevice.CurrentDevice.CheckSystemVersion(6,0)) segmentedControl.InsertSegment ("Directions",

    3, false); var conferenceMapItem = new MKMapItem(new MKPlacemark(ConferenceLocation, null)); conferenceMapItem.Name = "MonkeySpace"; var conferenceHotel = new MKMapItem(new MKPlacemark(new CLLocationCoordinate2D(42.36346, -71.0863), null)); conferenceHotel.Name = "MonkeySpace Hotel"; var mapItems = new MKMapItem[] { conferenceMapItem, conferenceHotel }; MKMapItem.OpenMaps(mapItems, new MKLaunchOptions() { DirectionsMode = MKDirectionsMode.Walking });
  13. Maps Receiving data from the Maps app 1. Create a

    GeoJson file with “.geojson” extension { "type": "MultiPolygon", "coordinates": [ [[[-122.7, 37.3], [-121.9, 37.3], [-121.9, 37.9], [-122.7, 37.9], [-122.7, 37.3]]], [[[-87.9, 41.5], [-87.3, 41.5], [-87.3, 42.1], [-87.9, 42.1], [-87.9, 41.5]]] ] }
  14. Maps Receiving data from the Maps app 2. Set the

    Maps options in the plist file (MonoDevelop 3.0.5+)
  15. Maps Receiving data from the Maps app 3. Override OpenUrl

    on AppDelegate and check for Url if (MKDirectionsRequest.IsDirectionsRequestUrl (url)) { var request = new MKDirectionsRequest(url); // Do something with the request }
  16. Reminders App.Current.EventStore.RequestAccess (EKEntityType.Reminder, (bool granted, NSError e) => { if

    (granted) { // Create a EKReminder and save reminder } else { new UIAlertView ( "Access Denied", "No Access", null, "OK", null).Show (); } ); • EKReminder is part of EventKit, subclasses EKCalendarItem • Need to request access to Reminders for Read/Write • Fetch, Save, Delete Reminders and with Predicates for searching Add a reminder for session
  17. Reminders Add a reminder for session EKReminder reminder = EKReminder.Create

    (App.Current.EventStore); reminder.Title = DisplaySession.Title; reminder.Calendar = App.Current.EventStore.DefaultCalendarForNewReminders; reminder.Notes = DisplaySession.Abstract; var date = new NSDateComponents(); date.Day = DisplaySession.Start.Day; date.Month = DisplaySession.Start.Month; date.Year = DisplaySession.Start.Year; date.Hour = DisplaySession.Start.Hour; date.Minute = DisplaySession.Start.Minute; date.Second = DisplaySession.Start.Second; reminder.StartDateComponents = date; reminder.DueDateComponents = date; App.Current.EventStore.SaveReminder (reminder, true, out e);
  18. UICollectionView View Bing images uploaded with the name “monkeyspace” •

    UITableView for a collection of items • Handles layout automatically, can be customized with UICollectionViewFlowLayout
  19. UICollectionView View Bing images uploaded with the name “monkeyspace” •

    Override ViewDidLoad to handle registering classes • Override GetItemCount • Override GetCell • Can override ItemSelected, ItemDeselected, ItemHighlighted, ItemUnhighlighted • Can override GetViewForSupplementaryElement to display a “Header” or “Section” view.
  20. PassKit An overview • Types - Event Ticket, Boarding Pass,

    Store Card, Coupon or Generic • ZIP file containing JSON files, optional images and localization strings • Can contain a Barcode, Location, Date Time and more • Passes are signed against a Apple certificate and identifier • Can be sent using E-mail, Link on a Website, Through an App
  21. PassKit Check availability of the PKPassLibrary if (PKPassLibrary.IsAvailable) { //

    Display PassKit views } library = new PKPassLibrary (); noteCenter = NSNotificationCenter.DefaultCenter.AddObserver (PKPassLibrary.DidChangeNotification, (not) => { BeginInvokeOnMainThread (() => { // refresh the pass passes = library.GetPasses (); RenderPass(passes); }); }, library); passes = library.GetPasses (); RenderPass(passes); Get the passes for the application