38
RubyMotion iOS 開発 2013/04/27 【豊橋】第1iPhoneアプリ開発勉強会 加藤匡邦 (エアーズ株式会社)

RubyMotionでiOS開発

Embed Size (px)

Citation preview

Page 1: RubyMotionでiOS開発

RubyMotion で iOS 開発

2013/04/27 【豊橋】第1回 iPhoneアプリ開発勉強会 加藤匡邦 (エアーズ株式会社)

Page 2: RubyMotionでiOS開発

自己紹介

@mackato

Mr. kato (bruce lee) in Green Hornet.

Page 4: RubyMotionでiOS開発

エアーズ株式会社

毎日つかえるアプリを作る

Page 5: RubyMotionでiOS開発

Hi-Cube

浜松本社

Page 7: RubyMotionでiOS開発

ウェブ開発Ruby on Rails

Page 8: RubyMotionでiOS開発

おいしいはiPhoneが覚えてる

Page 9: RubyMotionでiOS開発

お財布はスリムに、お店はスマートに

Page 10: RubyMotionでiOS開発

WWDC 2012 attendee

Page 11: RubyMotionでiOS開発

WWDC 2013 sells out in 2 minutes!

Page 12: RubyMotionでiOS開発

Hamamatsu.rb #1Hamamatsu.rb 2011.03.09~

Page 13: RubyMotionでiOS開発

Hamackathon

Hamackathon#2 Mobile - 2010.12.04

Hamackathon

Page 15: RubyMotionでiOS開発

Ruby

Page 16: RubyMotionでiOS開発

Rubyオブジェクト指向

シンプルな文法

動的な型付け

Page 17: RubyMotionでiOS開発

Web開発

サーバー管理

パッケージ管理

Page 18: RubyMotionでiOS開発

RubyMotion

Page 19: RubyMotionでiOS開発

RubyMotionRubyで書ける

iOSネイティブ

iOSライブラリが使える

Page 20: RubyMotionでiOS開発

$199.99有料です

Page 21: RubyMotionでiOS開発

¥20,513アベノミクス

2013/4/27現在

Page 22: RubyMotionでiOS開発

Write with Rubyclass AppDelegate def application(application, didFinishLaunchingWithOptions:launchOptions) @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) @window.makeKeyAndVisible

@blue_view = UIView.alloc.initWithFrame(CGRectMake(10, 10, 100, 100)) @blue_view.backgroundColor = UIColor.blueColor @window.addSubview(@blue_view)

true endend

app_delegate.rb

Page 23: RubyMotionでiOS開発

Use Power of Rubyclass AppDelegate def application(application, didFinishLaunchingWithOptions:launchOptions) @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) @window.makeKeyAndVisible  %w(blue-lagoon red-ocean black-coffee).each_with_index do |str, i| UIView.alloc.init.tap do |view| view.frame = [[50 * i, 50 * i + 20], [100, 100]] view.backgroundColor = UIColor.send(:"#{$1}Color") @window.addSubview(view) end if str =~ /^(?!black)(\w+)\-.+/ end  true endend

app_delegate.rb

Page 24: RubyMotionでiOS開発

Metaprogramming

http://clayallsopp.com/posts/rubymotion-metaprogramming/

def tableView(tableView,

didSelectRowAtIndexPath:indexPath)

menu_row = self.menu_items[indexPath.row]

# => 'profile'

self.send("open_#{menu_row}")

end

def open_profile; #...; end

def open_messages; #...; end

def open_feed; # ...; end

class Profile

# EX: has_one :user

def self.has_one(name)

klass = make_klass_from_name(name)

# EX: => User

# Effect: (a_profile.user = a_user)

# === a_user.profile_id = 4

define_method("#{name}=") do |value|

value.send("#{self.class.stringify}_id=", self.id)

value.save

value

end

end

def self.stringify; "profile"; end

end

send define_method

Page 25: RubyMotionでiOS開発

Hello Motion% motion create HelloMotion Create HelloMotion Create HelloMotion/.gitignore Create HelloMotion/Rakefile Create HelloMotion/app Create HelloMotion/app/app_delegate.rb Create HelloMotion/resources Create HelloMotion/resources/[email protected] Create HelloMotion/spec Create HelloMotion/spec/main_spec.rb

Page 26: RubyMotionでiOS開発

Goodbye XcodeHelloMotion/"## Rakefile"## app$   %## app_delegate.rb"## resources$   %## [email protected]%## spec %## main_spec.rb

GoodbyeXcode/"## GoodbyeXcode$   "## [email protected]$   "## Default.png$   "## [email protected]$   "## GXAppDelegate.h$   "## GXAppDelegate.m$   "## GoodbyeXcode-Info.plist$   "## GoodbyeXcode-Prefix.pch$   "## en.lproj$   $   %## InfoPlist.strings$   %## main.m"## GoodbyeXcode.xcodeproj$   "## project.pbxproj$   "## project.xcworkspace$   $   "## contents.xcworkspacedata$   $   %## xcuserdata$   $   %## kato.xcuserdatad$   $   %## UserInterfaceState.xcuserstate$   %## xcuserdata$   %## kato.xcuserdatad$   %## xcschemes$   "## GoodbyeXcode.xcscheme$   %## xcschememanagement.plist%## GoodbyeXcodeTests "## GoodbyeXcodeTests-Info.plist "## GoodbyeXcodeTests.h "## GoodbyeXcodeTests.m %## en.lproj %## InfoPlist.strings

Page 27: RubyMotionでiOS開発

Run Application% rake Build ./build/iPhoneSimulator-6.1-Development Compile ./app/app_delegate.rb Create ./build/iPhoneSimulator-6.1-Development/HelloMotion.app Link ./build/iPhoneSimulator-6.1-Development/HelloMotion.app/HelloMotion Create ./build/iPhoneSimulator-6.1-Development/HelloMotion.app/Info.plist Create ./build/iPhoneSimulator-6.1-Development/HelloMotion.app/PkgInfo Copy ./resources/[email protected] Create ./build/iPhoneSimulator-6.1-Development/HelloMotion.dSYMwarning: no debug symbols in executable (-arch i386) Simulate ./build/iPhoneSimulator-6.1-Development/HelloMotion.app(main)>

Page 28: RubyMotionでiOS開発

REPL - interactive console% rake Build ./build/iPhoneSimulator-6.1-Development Compile ./app/app_delegate.rb Link ./build/iPhoneSimulator-6.1-Development/HelloMotion.app/HelloMotion Create ./build/iPhoneSimulator-6.1-Development/HelloMotion.app/Info.plist Create ./build/iPhoneSimulator-6.1-Development/HelloMotion.app/PkgInfo Create ./build/iPhoneSimulator-6.1-Development/HelloMotion.dSYM Simulate ./build/iPhoneSimulator-6.1-Development/HelloMotion.app(main)> 2013-04-27 15:31:48.161 HelloMotion[1990:c07] Application windows ...(main)> self=> main(#<UIView:0x7648df0>)> self.backgroundColor = UIColor.whiteColor=> #<UICachedDeviceWhiteColor:0xa96f180>(#<UIView:0x7648df0>)>

Page 29: RubyMotionでiOS開発

motion-cocoapods

Rakefile

#  -­‐*-­‐  coding:  utf-­‐8  -­‐*-­‐$:.unshift("/Library/RubyMotion/lib")require  'motion/project'require  'rubygems'require  'motion-­‐cocoapods'  Motion::Project::App.setup  do  |app|    #  Use  `rake  config'  to  see  complete  project  settings.    app.name  =  'HelloMotion'      app.pods  do        pod  'JSONKit'    endend

Page 30: RubyMotionでiOS開発

Bundler

Rakefile

#  -­‐*-­‐  coding:  utf-­‐8  -­‐*-­‐$:.unshift("/Library/RubyMotion/lib")require  'motion/project'require  'bundler'Bundler.require  Motion::Project::App.setup  do  |app|    #  Use  `rake  config'  to  see  complete  project  settings.    app.name  =  'HelloMotion'      app.pods  do        pod  'JSONKit'    endend

Gemfile

source  'https://rubygems.org'  gem  'rake'gem  'motion-­‐cocoapods'gem  'bubble-­‐wrap'gem  'sugarcube'

Page 31: RubyMotionでiOS開発

BubbleWrap# Uses the front cameraBW::Device.camera.front.picture(media_types: [:movie, :image]) do |result| image_view = UIImageView.alloc.initWithImage(result[:original_image])end

Camera

BW::Location.get do |result| p "From Lat #{result[:from].latitude}, Long #{result[:from].longitude}" p "To Lat #{result[:to].latitude}, Long #{result[:to].longitude}"end

Location

BW::HTTP.get("https://api.github.com/users/mattetti") do |response| p response.body.to_strend

HTTP

Page 32: RubyMotionでiOS開発

Sugarcube0xffffff.uicolor  #  =>  UIColor.colorWithRed(1.0,  green:1.0,  blue:1.0,  alpha:1.0)

5.days.ago    #  =>  2012-­‐12-­‐29  11:42:24  -­‐0700

Fixnum

#  UIApplication.sharedApplication.openURL(NSURL.URLWithString("https://github.com"))"https://github.com".nsurl.open

NSURL

#  UIImage  from  name"my_image".uiimage    #  =>  UIImage.imageNamed("my_image")

#  UIFont  from  name"my_font".uifont(20)  #  =>  UIFont.fontWithName("my_font",  size:20)

#  NSLocalizedString  from  string"hello".localized    #  =>  NSBundle.mainBundle.localizedStringForKey("hello",  value:nil,  table:nil)"hello"._                    #  ==  "hello".localized

NSString

Page 34: RubyMotionでiOS開発

Community

RubyMotionもくもく会次回 2013/05/22

RubyMotion Kaigi 20132013/05/29

Page 35: RubyMotionでiOS開発

Books

RubyMotion RubyMotion入門@clayallsopp @naoya_ito

Page 37: RubyMotionでiOS開発

ConclusionRubyが書けてiOS未経験の方にはお薦め

RubyMotion以外にRuby使わない人には??

xcodeprojが無いので共同作業はしやすい