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.

iOS Development with RubyMotion

Basic introduction to how using RubyMotion can simplify iOS development

  • Login to see the comments

  • Be the first to like this

iOS Development with RubyMotion

  1. 1. iOS Development with RubyMotion
  2. 2. Kieran Johnson @kieranj
  3. 3. https://www.invisiblelines.com
  4. 4. What is RubyMotion?
  5. 5. Why Use RubyMotion? No Xcode (unless that's what you want) Command line toolchain ­ Rake Less boilerplate More succinct code REPL for working with your app live Native performance (compiles to bytecode)
  6. 6. No More Objective-C? Objective­C NSString *list = [NSString alloc] initWithString: @"Karin, Carrie, David"]; NSArray *listItems = [list componentsSeparatedByString:@", "]; [list stringByReplacingOccurrencesOfString:@", " withString:@" - "] Ruby list = 'Karin, Carrie, David' list_items = list.split(', ') list.gsub!(/, /, ' - ')
  7. 7. RubyMotion classes, methods and objects are Objective­C classes methods and objects
  8. 8. [].class # => Array [].class.ancestors # => [Array, NSMutableArray, NSArray, Enumerable, NSObject, Kernel]
  9. 9. Syntax [string drawAtPoint:point, withAttributes:attributes] string.drawAtPoint(point, withAttributes: attributes) string.send('drawAtPoint:withAttributes', point, attributes)
  10. 10. Method Overloading def tableView(table_view, cellForRowAtIndexPath: indexPath) # ... end def tableView(tableView, heightForRowAtIndexPath: indexPath) # ... end
  11. 11. REPL
  12. 12. Common Pitfalls
  13. 13. There's No Escaping Cocoa/CocoaTouch
  14. 14. No Standard Library
  15. 15. No Require
  16. 16. Metaprogramming No :eval or access to bindings But :send, :method_missing, :define_method, :instance_eval, :class_eval etc are all present
  17. 17. Examples
  18. 18. AppDelegate.m @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; CGRect frame = CGRectMake(20, 50, 280, 50); UILabel *label = [[UILabel alloc] init]; label.text = @"Hello World"; label.textColor = [UIColor whiteColor]; label.frame = frame; [self.window makeKeyAndVisible]; [self.window addSubView label]; return YES; }
  19. 19. Creating an Application $ motion create hello_world Create hello_world Create hello_world/.gitignore Create hello_world/app/app_delegate.rb Create hello_world/Gemfile Create hello_world/Rakefile Create hello_world/resources/Default-568h@2x.png Create hello_world/resources/Default-667h@2x.png Create hello_world/resources/Default-736h@3x.png Create hello_world/spec/main_spec.rb
  20. 20. app_delegate.rb class AppDelegate def application(application, didFinishLaunchingWithOptions:launchOptions) @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) label = UILabel.new label.text = "Hello World" label.textColor = UIColor.whiteColor label.frame = [[20, 50], [280, 50]] @window.addSubview(label) @window.makeKeyAndVisible true end end
  21. 21. Running an Application
  22. 22. Beyond HelloWorld
  23. 23. class CameraController < UIViewController def viewDidLoad view.backgroundColor = UIColor.underPageBackgroundColor @picker = UIImagePickerController.alloc.init @picker.delegate = self @picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum @picker.setMediaTypes([KUTTypeImage]) load_buttons end def load_buttons btn = UIButton.buttonWithType(UIButtonTypeRoundedRect) btn.frame = [[50, 20], [200, 50]] btn.setTitle('Select an image', forState:UIControlStateNormal) btn.addTarget(self, action: :select_image, forControlEvents:UIControlEventTouchUpInside view.addSubview(btn) end def select_image self.presentModalViewController(@picker, animated: true) end def imagePickerController(picker, didFinishPickingMediaWithInfo: info) dismissModalViewControllerAnimated(true) image_view = UIImageView.alloc.initWithImage(info.valueForKey("UIImagePickerControllerOr image_view.frame = [[50, 200], [200, 180]] view.addSubview(image_view) end end
  24. 24. Bubblewrap and RMQ class CameraController < UIViewController def viewDidLoad view.backgroundColor = UIColor.underPageBackgroundColor load_buttons end def load_buttons btn = UIButton.buttonWithType(UIButtonTypeRoundedRect) btn.setTitle('Select an image', forState: UIControlStateNormal) btn = rmq.append(btn).layout(l: 50, t: 20, w: 200, h: 50) btn.on(:tap) { select_image } end def select_image BW::Device.camera.any.picture(media_types: [:image]) do |result| image_view = UIImageView.alloc.initWithImage(result[:original_image]) rmq.append(image_view).layout(l: 50, t: 200, w: 200, h: 180) end end end
  25. 25. Libraries Bubblewrap Sugarcube TeaCup Motionkit RMQ Promotion RedPotion
  26. 26. Thanks!

×