2016年6月2日木曜日

XamarinでCocoapodsのライブラリを使う

iOSアプリを作る際、多くの場合Cocoapodsを使うかと思います。
Xcodeで開発する場合は以下のサイトが詳しいのでそちらを参考に。


※El Capitanの場合は以下


今回はXamarinでiOSアプリを開発する際に、CocoaPodsライブラリを使用する方法を紹介します。

1.Objective Sharpieインストール

XamarinのドキュメントサイトからObjective Sharpieをダウンロードしてインストールします。


2.Objective Sharpieでバインディング

Xamarin.iOSで作ったアプリ(C#)からObjective-Cで作られたライブラリを呼び出すには
バインディングの定義ファイルを作らないといけないのですが、Objective Sharpieを使うとバインディングの
定義ファイル、ライブラリ本体(lib*.aファイル)を自動で作ってくれます。


・CocoaPodsのサイトで使用したいライブラリを探します。


・ターミナルを起動し、任意のディレクトリで以下のコマンドを実行します。


$ sharpie pod init ios LGSideMenuController

※今回は「LGSideMenuController」を入れます。


成功すると↓のような感じになります。


Setup completed
** Searching for requested CocoaPods ...
** Working directory: 
**   - Writing Podfile ...
**   - Installing CocoaPods ...
**     (running `pod install --no-integrate --no-repo-update`)
[!] Unable to load a specification for the plugin `/Library/Ruby/Gems/2.0.0/gems/cocoapods-deintegrate-1.0.0`
Analyzing dependencies
Downloading dependencies
Installing LGSideMenuController (1.0.10)
Generating Pods project
Sending stats
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

** 🍻  Success! You can now use other `sharpie pod` commands.



成功すると「Pods」ディレクトリができます。













ターゲットのiOSバージョンを変更する場合は、Podsディレクトリ以下のxcodeprojファイルを開いて
Deployment Targetを変更します。


















ターミナルを起動し、以下のコマンドを実行します。

$ sharpie pod bind


長々とログがでますが「Done.」と出てきたら成功です。

Binding Analysis:
  Automated binding is complete, but there are a few APIs which have been flagged with [Verify] attributes. While the entire binding should
  be audited for best API design practices, look more closely at APIs with the following Verify attribute hints:

  MethodToProperty (4 instances):
    An Objective-C method was bound as a C# property due to convention such as taking no parameters and returning a value (non-void return).
    Often methods like these should be bound as properties to surface a nicer API, but sometimes false-positives can occur and the binding
    should actually be a method.

  Once you have verified a Verify attribute, you should remove it from the binding source code. The presence of Verify attributes
  intentionally cause build failures.
  
  For more information about the Verify attribute hints above, consult the Objective Sharpie documentation by running 'sharpie docs' or
  visiting the following URL:

    http://xmn.io/sharpie-docs

Submitting usage data to Xamarin...
  Submitted - thank you for helping to improve Objective Sharpie!

Done.



成功するとライブラリ本体(lib*.a)とバインディング定義ファイルが作られます。























あとは出来上がったファイルを使ってXamarin StudioでBindings Libraryプロジェクトを作れば使えるようになります。

3.シミュレータで動作するライブラリを生成

前述までの手順で作ったライブラリは実機では動きますが、シミュレータでは動きません。

出来上がったライブラリをlipoコマンドで確認してみると対象アーキテクチャに「i386」が含まれていないことがわかります。
シミュレータで使えるようにするには「i386」が含まれている必要があります。




lipo -info libLGSideMenuController.a 

Architectures in the fat file: libLGSideMenuController.a are: armv7 arm64 




ですので、少々面倒くさいのですが、Podsディレクトリ以下のxcodeprojファイルを開いてシミュレータ用にライブラリを生成しなおします。















上記ショットの通り、「Build Active Architecture Only」を「No」にして「Valid Architecture」に「i386」を追加します。

あとはシミュレータを対象にビルドを実行すればシミュレータで動作するライブラリが出来上がります。









build/Debug-iphonesimulatorディレクトリ以下にライブラリが出力されてます。

lipoコマンドで確認すると対象アーキテクチャに「i386」が含まれていることがわかります。

lipo -info libLGSideMenuController.a 

Architectures in the fat file: libLGSideMenuController.a are: i386 x86_64 


シミュレータで実行する際はこのライブラリを使えば動きます!

しかしもうちょっとスマートなやりかたないんですかねー。
ご存知の方教えてくださいm(_ _)m



それじゃまた。


0 件のコメント:

コメントを投稿