RubyMotion Kaigi で質問があったので、RubyMotion を使ったスタティックライブラリの作り方と、Xcode プロジェクトへの組み込み方について書いてみました。
RubyMotion プロジェクトを作成する
まずは、RubyMotion のプロジェクトを作成してみます。
1
2
3
4
5
6
7
% motion create TestStatic
Create TestStatic
Create TestStatic/.gitignore
Create TestStatic/app/app_delegate.rb
Create TestStatic/Rakefile
Create TestStatic/resources/Default-568h@2x.png
Create TestStatic/spec/main_spec.rb
アプリケーションが起動する際にインスタンス化され呼び出されるクラスとして、AppDelegate
があります。Xcode でプロジェクトを作成する際にも同盟のクラスが自動的に作成されます。シンボルが衝突したりすると面倒なので、クラス名を AppDelegate
から StaticAppDelegate
などに変更しておきます (AppDelegate
そのものを削除しても良いのですが、RubyMotion で動作確認できなくなってしまうので、このようにしてます)。
app/app_delegate.rb
1
2
3
4
5
class StaticAppDelegate
def application ( application , didFinishLaunchingWithOptions : launchOptions )
true
end
end
Rakefile
を修正し app.delegate_class
を設定して delegate クラスを変更したことを設定しておきます。
Rakefile
1
2
3
4
5
Motion : :Project :: App . setup do | app |
# Use `rake config' to see complete project settings.
app . name = 'TestStatic'
app . delegate_class = 'StaticAppDelegate'
end
コーディング
ライブラリ化するコードを記述していきます。ここでは、文字列から UIColor オブジェクトを作成するものを書きました。
app/app_delegate.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class NSString
def to_color
# string like a "#0c92f2"
raise "Unknown color scheme" if ( self [ 0 ] != '#' ) || ( self . length != 7 )
color = self [ 1 . . - 1 ]
r = color [ 0 . . 1 ]
g = color [ 2 . . 3 ]
b = color [ 4 . . 5 ]
UIColor . colorWithRed (( r . hex / 255 . 0 ), green :( g . hex / 255 . 0 ), blue :( b . hex / 255 . 0 ), alpha : 1 )
end
end
class StaticAppDelegate
def application ( application , didFinishLaunchingWithOptions : launchOptions )
NSLog ( "%@" , "#0c92f2" . to_color )
true
end
end
ライブラリを作成する
rake static
コマンドを実行してスタティックライブラリを作成します。
1
2
3
4
5
6
7
% rake static
Build ./build/iPhoneSimulator-6.1-Development
Create ./build/iPhoneSimulator-6.1-Development/TestStatic.a
Build ./build/iPhoneOS-6.1-Development
Compile ./app/app_delegate.rb
Create ./build/iPhoneOS-6.1-Development/TestStatic.a
Create ./build/TestStatic-universal.a
できあがったライブラリは build
ディレクトリの中に XXXXX-universal.a
という名前で格納されます。
Xcode プロジェクトからライブラリを利用する
先ほど作成した XXXXX-universal.a
に加えて、libstdc++.dylib
と libicucore.dylib
というライブラリをプロジェクトに追加します。
あと、RubyMotion で用意したメソッドを呼び出そうとすると、「メソッドの宣言がされていない!」と ARC がオンになっていると怒られてしまうので、とりあえず ARC はオフにしています。(Ruby で用意したメソッドにそったヘッダファイルを作成すれば良いのですが・・・、面倒だったので省いています)
次に main.m
をに以下のようにコードを追加します。RubyMotionInit()
を呼び出すと、RubyMotion で用意したコードの初期処理が行われ、クラスなどが構築され Objective-C から利用できるようになります。
main.m
1
2
3
4
5
// RubyMotion のライブラリを初期処理
void RubyMotionInit ( int argc , char ** argv );
RubyMotionInit ( argc , argv );
return UIApplicationMain ( argc , argv , nil , NSStringFromClass ([ AppDelegate class ]));
あとは Objective-C から利用します。
AppDelegate.m
1
2
3
4
5
- ( BOOL ) application: ( UIApplication * ) application didFinishLaunchingWithOptions: ( NSDictionary * ) launchOptions
{
NSLog ( @"%@" , [ @"#0c92f2" to_color ]);
return YES ;
}
実行すると、以下のようにアウトプットコンソールに表示され、RubyMotion で作ったスタティックライブラリが動作していることが確認できます。