Simply migrate your Android JNI code logic to iOS-write an alternative to iOS under Jni.h-ocni.h

Source: Internet
Author: User

1. The code logic in JNI interacts with the upper platform language.

2. Use a non-Xcode IDE development tool and want to use pure C + + code without doping other platform-related language syntax.

3. Simply replace the JNI code with a function call to the upper platform language.

By the way, this article is a starting point.

Let's talk about it. The jni.h header file is a C or C + + interface set provided for JNI code (c + +) to use the Java Platform Layer's objects (functions). If the Java object of the same function can be implemented easily in the environment of the OC layer of iOS platform, your JNI code will not have to change the logic, as long as the place that accesses to the Java platform layer is changed to the same function OC object. But you do not want to use OC syntax to write m code files, yes you get to, will jni.h replace ocni.h on it, I want is ocni.h.

In the Jni.h interface file, the most commonly used is to access Java object methods, introspection, reference these interfaces, respectively, call# #RtnType # #Method (Jobject, Jmethodid, ...), Findclass (const char* ), Newlocalref,deletelocalref and so on, put these one by one with OC Runtime and CF library to fix it.

First Look at call# #RtnType # #Method (Jobject, Jmethodid, ...) How this prototype looks so familiar, is not objc_msgsend (ID, SEL, ...), so you do not have square brackets syntax.

Then look at the relevant findclass,getobjectclass,instanceof, etc., the NSObject interface in the foundation is not-[nsobject class] and-[nsobject Iskindofclass :], the runtime is not objc_getclass and so on.

The third is to refer to the relevant interface, Java layer is GC management, object new comes out without going to delete,release, this is the OC arc mode. However, in JNI code, you must use the Jni.h interface to manually manage the count of references. That is, your JNI code migrated over the iOS platform in MRC mode, corresponding to the completion of Retain/release.

The most important is the weak reference, our goal is the migration process does not add the OC characteristics of the keywords, such as __weak,__strong,__bridge, etc. (these in MRC is Air). But __weak can only be used in ARC mode, which is fine, as long as you understand what LLVM did for the __weak keyword at compile time, you can use the C interface function of the runtime to do the same work, but unfortunately, these functions can be seen in the source code project at run time, But it's not an outward function, so it can't be used. This is hard to fall, the weak reference in Java does not depend on compilation, but rather a WeakReference object, I also come to a ocweakreference class, run by it in Arc mode.

Finally there is jstring, jarray with CF class to replace it.

The code is uploaded to the https://github.com/bbqz007/xw.

Ocni.h

Ocni.cpp

OCWeakReference.h

Ocweakreference.m

Simply migrate your Android JNI code logic to iOS-write an alternative to iOS under Jni.h-ocni.h

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.