IOS Learning Series-how to bind a native Obj-C static library to MonoTouch

Source: Internet
Author: User

MonoTouch is a cross-platform solution for iOS development using the C # language. It supports iPhone/iPad development and currently supports the latest iOS 6 version.

Official Address: http://xamarin.com/

Github Sample: https://github.com/xamarin/monotouch-samples

App: http://xamarin.com/apps/all

MonoTouch can use the C # language for iOS development, which means that it can be easily used for iOS development as a. Net programmer. Of course, understanding the objective-c syntax is also helpful for your iOS learning.

 

This article mainly teaches you how to use MonoTouch to bind the native Obj-C static library and directly call the methods in the static library for implementation.

First, use xcode to create a native static Library:

Two files added to the MBProgressHUD Project (MBProgressHUD is a framework waiting to load special effects, github: https://github.com/jdg/MBProgressHUD)

Then compile it. By default, you can find the BindingLibraryl application in/Library/Developer/Xcode/DerivedData, libBindingLibrary.

Next, open MonoDevelop, create a BindingLibrary solution, and add a BindingLibrarySDK project:

Select "MonoTouch Binding Project", which is used to bind a native library.

Then, add libBindingLibrary. a to the project, and you will find that in the project, it will automatically generate a file called libBindingLibrary. linkwith. cs:

Using System;
Using MonoTouch. ObjCRuntime;
 
[Assembly: LinkWith ("libBindingLibrary. a", LinkTarget. Simulator, ForceLoad = true)]

In this way, the libBindingLibrary. a static library is imported, which is similar to importing non-C # (such as C ++) dynamic library by DllImport.

You can find that two files are automatically generated in the project, ApiDefinition. cs and StructsAndEnums. cs files. ApiDefintion. cs is mainly used as the ing of native class definitions.

For more information, see Binding Objective-C Types.

Write the code here:

[BaseType (typeof (UIView)]
Interface MBProgressHUD
{
[Static, Export ("showHUDAddedTo: animated:")]
MBProgressHUD ShowHUDAddedTo (UIView view, bool animated );
 
[Export ("show:")]
Void Show (bool animated );
 
[Export ("hide:")]
Void Hide (bool animated );
 
[Export ("hide: afterDelay:")]
Void Hide (bool animated, double delay );
 
[Export ("labelText", ArgumentSemantic. Copy)]
String LabelText {get; set ;}

The corresponding native interfaces include:

+ (MBProgressHUD *) showHUDAddedTo :( UIView *) view animated :( BOOL) animated;
-(Void) show :( BOOL) animated;
-(Void) hide :( BOOL) animated;
-(Void) hide :( BOOL) animated afterDelay :( NSTimeInterval) delay;
@ Property (copy) NSString * labelText;

In this example, BaseType indicates that its inheritance type is UIView, and the base class corresponding to the native MBProgressHUD class is UIView;

Export is the name of the imported method. Static indicates that it is used as a Static method;

ArgumentSematic is its parameter type: Copy;

 

Then you can directly compile BindingLibrarySDK to generate a BindingLibrarySDK. dll

 

Now, create a new iOS project:

Here, I select an iphone application with a Single Window.

Reference BindingLibrarySDK. dll:

Next, I designed a button control in the xib file to generate the code for clicking the event:

In the Click Event code, implement:

Partial void showWindow (MonoTouch. Foundation. NSObject sender)
{
BindingLibrarySDK. MBProgressHUD hud = BindingLibrarySDK. MBProgressHUD. ShowHUDAddedTo (this. View, true );
Hud. Show (true );
Hud. LabelText =;
Hud. Hide (true, 1); // hide hud after 1 s
}

Finally, run the program to view the effect:

Click the button to display the loading effect box. After one second, the effect box disappears.

 

In this way, MonoTouch is used to bind the native Obj-C static library and call related native methods.

Example: https://github.com/sunleepy/monotouch_binding

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.