Binding Objective-C libraries – Xamarin iOS

by nguyendinhquang.it
377 views

Content

  1. Create project Objective-C lib
  2. Define functions
  3. Config project
  4. Create makefile
  5. Create project binding library in solution Xamarin
  6. Add binding library to project iOS Xamarin
  7. Call Library API trong code Xamarin.

1. Create project objective-C

Open Xcode, File -> New project -> Cocoa Touch Static Library> Next

Tiếp theo, nhập tên project, phần language nhớ chọn Objective-C nhé

2. Define functions

Define các functions vào file .m(implement) và file .h (header) phần logic các bạn tự code nhé @@

3. Config project

Setting target version sang iOS mà bên Xamarin các bạn sẽ chọn, ở đây project của mình để là 10.0

4. Run terminal commands

4.1 Create 1 makefile

Với content như dưới đây, chú ý thay chỗ in đậm bằng tên của project

XBUILD=/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild
PROJECT_ROOT=./TênProject
PROJECT=$(PROJECT_ROOT)/ TênProject.xcodeproj
TARGET= TênProject

all: lib$(TARGET).a

lib$(TARGET)-i386.a:
$(XBUILD) -project $(PROJECT) -target $(TARGET) -sdk iphonesimulator -configuration Release clean build
-mv $(PROJECT_ROOT)/build/Release-iphonesimulator/lib$(TARGET).a $@

lib$(TARGET)-armv7.a:
$(XBUILD) -project $(PROJECT) -target $(TARGET) -sdk iphoneos -arch armv7 -configuration Release clean build
-mv $(PROJECT_ROOT)/build/Release-iphoneos/lib$(TARGET).a $@

lib$(TARGET)-arm64.a:
$(XBUILD) -project $(PROJECT) -target $(TARGET) -sdk iphoneos -arch arm64 -configuration Release clean build
-mv $(PROJECT_ROOT)/build/Release-iphoneos/lib$(TARGET).a $@

lib$(TARGET).a: lib$(TARGET)-i386.a lib$(TARGET)-armv7.a lib$(TARGET)-arm64.a
xcrun -sdk iphoneos lipo -create -output $@ $^

clean:
-rm -f *.a *.dll

4.2 Execute makefile

Vào terminal đi đến folder chứa Makefile gõ:
•“make” – để chạy makefile
•“make clean” – để clean các file thư viện .a được generate ra sau khi đã chạy ”make” ( Hàm này để đỡ phải xoá file .a = tay thôi)

4.3 The API definition file

Sau khi đã có file .a, chạy lệnh sau:

sharpie bind -sdk iphoneos12.4 /Users/apple/Documents/UrlFilterLib2/UrlFilterLib2/*.h -scope /Users/apple/Documents/UrlFilterLib2/UrlFilterLib2/

Chú ý đường dẫn vào library mỗi máy khác nhau, và cái số ”12.4” là hỗ trợ iOS version mới nhất của Xcode,
• Ex :  Xcode 10.3 có hỗ trợ iOS mới nhất là 12.4
• Sau khi chạy lệnh trên, sẽ generate ra file ApiDefinition.cs

Đến đây là chúng ta đã hoàn thành bước tạo library rồi đó. Tiếp theo là sử dụng nó trong project Xamarin nhé .

5. Add binding library to project Xamarin

1. Click chuột phải vào Solution ->  Add ->  Add new project

2. Chọn iOS -> Library -> Bindings Library.

3. Next.

4. Kéo file .a vào binding library project vừa tạo

5. Alert hiện lên, chọn “Copy the file in the directory”  -> OK
Copy content trong file ApiDefinition.cs tại step 4  vào file ApiDefinition.cs trong project  Xamarin này.

6. Sau đó nhấn chọn build Library project.

6. Add binding library to project Xamarin

  1. Click chuột phải vào “References”, chọn “Edit references”.
  2. Tích chọn Library project..
  3. OK

7. Call library API

Sau khi thực hiện các bước ở trên chúng ta có thể gọi chúng như với các lib của Xamarin rồi

Kết Luận

Như vậy là chúng ta đã hoàn thành việc binding lib từ objective-c vào Xamarin iOS. Phần tiếp theo mình sẽ hướng dẫn binding với android.
Tham khảo tại: https://docs.microsoft.com/en-us/xamarin/cross-platform/macios/binding/?context=xamarin/ios

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.