Upload
shengwen-chiou
View
76
Download
0
Embed Size (px)
Citation preview
Xamarin.iOS中引用自製Objective-C的 Class Library
HappyMan2015/02/03
目標• 在 Xamarin.iOS 中使用我們自行在
Xcode 中開發的 Objective-C Class Library
步驟
1. 在 Xcode 中建立 Static Library2. 編譯此 Static Library,使它能同時支援
iOS Device 與 iOS simulator硬體架構3. 使用 Objective Sharpie Tool產生轉換
Objective-C到 C#的程式碼4. 在 Xamarin 中建立 iOS Binding Project5. 在 Xamarin中建立 iOS APP 專案,引用
iOS Binding Project
建立 Static Library• 在 Xcode創建新專案,選擇 iOS ->
Framework & Library -> Cocoa Touch Static Library。在此命名為 ShareCode
專案結構• 我們來宣告並實作一個帶有參數型態
NSString且回傳值型態 NSString的方法
ShareCode.h• #import <Foundation/Foundation.h>
• @interface ShareCode : NSObject
• - (NSString *)sayHello:(NSString *)name;
• @end
ShareCode.h• #import "ShareCode.h"
• @implementation ShareCode
• -(NSString *)sayHello:(NSString *)name {• return [NSString stringWithFormat:@"Hello %@ :)", name];• }
• @end
專案中的目錄結構• 在命令提示字元介面中以 Xcodebuild編譯這個專案
產生 iOS Simulator用的檔案• xcodebuild -sdk iphonesimulator -configuration
Debug– Build settings from command line:– SDKROOT = iphonesimulator8.1– === BUILD TARGET ShareCode OF PROJECT ShareCode
WITH CONFIGURATION Debug ===– Check dependencies– Write auxiliary files– …– ** BUILD SUCCEEDED **
產生 iOS Device 用的檔案armv7
• xcodebuild -sdk iphoneos -arch armv7 -configuration Debug– Build settings from command line:– ARCHS = armv7– SDKROOT = iphoneos8.1– === BUILD TARGET ShareCode OF PROJECT ShareCode
WITH CONFIGURATION Debug ===– Check dependencies– Write auxiliary files– ** BUILD SUCCEEDED **
產生 iOS Device 用的檔案armv7s
• xcodebuild -sdk iphoneos -arch armv7s -configuration Debug– Build settings from command line:– ARCHS = armv7s– SDKROOT = iphoneos8.1– === BUILD TARGET ShareCode OF PROJECT ShareCode
WITH CONFIGURATION Debug ===– Check dependencies– Write auxiliary files– ** BUILD SUCCEEDED **
產生 iOS Device 用的檔案arm64
• xcodebuild -sdk iphoneos -arch arm64 -configuration Debug– Build settings from command line:– ARCHS = arm64– SDKROOT = iphoneos8.1– === BUILD TARGET ShareCode OF PROJECT ShareCode
WITH CONFIGURATION Debug ===– Check dependencies– Write auxiliary files– ** BUILD SUCCEEDED **
專案中的目錄結構• ShareCode -> build• 我在名稱後綴加入 arm7 arm7s
arm64以利分辨
Static Library• 檔案來源: build -> Debug-
iphoneos_arm*• 重新命名: libShareCode.a– libShareCode_arm64.a– libShareCode_armv7s.a– libShareCode_armv7.a– libShareCode_simulator.a
• 轉放至專案資料夾: ShareCode
合併為一• 使用 lipo指令將 .a檔案包成單一檔案• lipo -create -output ShareCode.a
libShareCode-arm64.a libShareCode-armv7s.a libShareCode-armv7.a libShareCode-simulator.a
• 於是產生 ShareCode.a
建立 Xamarin.iOS Binding Project• C# -> iOS -> Unified API -> iOS Binding Project
檔案結構 (前 )
加入 Static Library
加入 Static Library
加入 Static Library
檔案結構 (後 )
ShareCode.linkwith.cs• using System;
using ObjCRuntime;
[assembly: LinkWith (“ShareCode.a”, LinkTarget.Arm64 | LinkTarget.ArmV7s | LinkTarget.ArmV7 | LinkTarget.Simulator, SmartLink = true, ForceLoad = true)]
使用 Objective Sharpie• Objective Sharpie is a command line
tool (provided by Xamarin) that can assist in creating the definitions required to bind a 3rd party Objective-C library to C#.
• 下載並安裝– http://files.xamarin.com/~abock/Objecti
veSharpie/ObjectiveSharpie-1.1.1.pkg
查看 Xcode SDK• sharpie xcode -sdks– macosx10.8– macosx10.9– iphoneos7.1– iphonesimulator7.1– macosx10.10– iphoneos8.2– iphonesimulator8.2– iphoneos8.1– iphonesimulator8.1
轉換 ShareCode.h• sharpie bind -output ShareCode -namespace ShareCode -sdk
iphoneos8.2 ShareCode.h –unified– Compiler configuration:– -isysroot
/Applications/Xcode-Beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk -miphoneos-version-min=8.2 -resource-dir /Library/Frameworks/ObjectiveSharpie.framework/Versions/1.1.1/clang-resources -arch armv7 -ObjC
– Parsing...– [ 0%] parsing /Users/jason/Xcode/ShareCode/ShareCode.h– [100%] parsing complete– Binding...– [bind] generating ShareCode.cs– Submitting usage data to Xamarin...– Submitted - thank you for helping to improve Objective Sharpie!– Done.
生成 ShareCode.cs• namespace ShareCode {
// @interface ShareCode : NSObject[BaseType (typeof (NSObject))]interface ShareCode {
// -(NSString *)sayHello:(NSString *)name;[Export ("sayHello:")]string SayHello (string name);
}}
• 將它複製到 ApiDefinition.cs
ApiDefinition.cs• using System;
using System.Drawing;
using ObjCRuntime;using Foundation;using UIKit;
namespace ShareCode {
// @interface ShareCode : NSObject[BaseType (typeof (NSObject))]interface ShareCode {
// -(NSString *)sayHello:(NSString *)name;[Export ("sayHello:")]string SayHello (string name);}
}
編譯釋出
產生 BindingProjectTest2.dll• BindingProjectTest2 BindingProjectTest2 ▸ ▸
bin Release▸ BindingProjectTest2.dll▸
建立 Xamarin.iOS Project
檔案架構
引用 BindingProjectTest2產生的檔• Project -> Edit References
引用 BindingProjectTest2產生的檔• Browse -> BindingProjectTest2 ▸
BindingProjectTest2 bin Release ▸ ▸ ▸BindingProjectTest2.dll
MainStoryboard.storyboard• 建立一個 Label
MainStoryboard.storyboard• 設定 Label屬性
BindingSample2ViewController.cs• public override void ViewDidLoad ()
{base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
var obj = new ShareCode.ShareCode ();this.helloLabel.Text = obj.SayHello
("HappyMan");}
編譯執行• iOS Simulator (iPhone 6)• iOS Device (iPhone 6)
結論• 使用 Objective-C Library轉到 C#
Library角色有三個專案,範例:– ShareCode (Xcode)– BindingProjectTest (Xamarin)– BindingSample (Xamarin)
• https://github.com/happymanx/Binding-Library-from-ObjC-to-CSharp.git
參考• [Xamarin.iOS] 如何引用 Objective-C 寫
的 Class Libraryhttp://www.dotblogs.com.tw/toysboy21/archive/2013/08/27/115697.aspx
• Xamarin - Walkthrough: Binding an Objective-C Libraryhttp://developer.xamarin.com/guides/ios/advanced_topics/binding_objective-c/Walkthrough_Binding_objective-c_library/