Home » Android » Developing a shared Android / iOS network library

Developing a shared Android / iOS network library

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have been primarily developing iOS apps but now, I’m developing applications for both iOS and Android. As much as possible, I’m trying to write code that is platform-independent as possible.

What have people done for networking functionality? I can’t seem to find any resources on making GET/POST HTTP requests in Android.

How would you approach the issue of developing a shared networking library for both iOS and Android.

EDIT: I’ll be doing all my Android network programming in C/C++ and not Java!

Are there any C/C++ networking libraries for Android (and subsequently can be used for iOS)?

How to&Answers:

I don’t think this is as impossible as most people here make it sound like. I interviewed someone recently who worked for a large mobile game development company and he mentioned that they do as much work as they can in C/C++ libs in order to reuse the code on multiple platforms including Android and iOS. I haven’t tried this myself, but I’ve heard this numerous times.

As an example, check out this blog post:
http://thesoftwarerogue.blogspot.com/2010/05/porting-of-libcurl-to-android-os-using.html

If the cURL library can be used on Android, I don’t see why your networking library couldn’t. The difficulties will most likely be related to interacting with the GUI since you don’t have access to the platform SDKs from your C/C++ code base. But for a networking lib, that’s perfectly OK. Here is what I suggest:

  1. Write some proof of concept code to see how you can link with the lib from both platforms. Consider the limitations in the design of your library.
  2. Develop your networking library as a standalone project with a test driven development approach. That is, write unit tests to drive your code to test it as it’s being developed.
  3. Write wrapper classes in native code in both Android and iOS projects to isolate the library code.

Also, as was previously mentioned, Apple is ok with developing apps in C/C++ as long as they are done with XCode. They’ve even relaxed on that requirement last year.

Good luck

Answer:

If you can stand the horrid excuse of a language, use Javascript.

Answer:

Considering the differences it’s not really possible. It’s a pain in the ass to write a Blackberry/Android shared library and they both use “Java”.

If you really want to do cross platform, then you will be writing webpages in javascript and then delivering them on web-views on the devices.

You can’t really share code across Objective C and Java, it’s a impossibility afaik, and you can’t run Java on the iPhone and you can’t run ObjectiveC on Android. So there is no possible sane way to do this with native code.

Edit: As far as networking goes, don’t think you’ll find many places where you can access low-level network across platforms in a uniform way.

Answer:

I suppose you are talking about native (not web-based) applications. To make GET/POST HTTP requests in Android you should take a look to HTTPClient. This Java library comes with Android and it allows to develop the features you need:

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try {
    HttpResponse response = client.execute(request);
    ...
} catch(Exception ex) {
    // handle
}

About how to develop a shared networking library for both iOS and Android, it is an interesting question, I can’t figure out any solution because the platforms have too much differences (Java vs Objective C the most obvious), but it would be great.

I can say that Android supports C/C++ code via NDK, but I’m not sure if that works in iOS.

Answer:

You can try Titanium, source code is in JS then cross compiles to iOS or Android.

Answer:

As others have said, it won’t be easy, if it’s possible at all. If you decide to make an attempt, I would recommend working in C/C++ since both platforms can handle that language. (iOS is Objective-C, as you know, and Android can handle C via the NDK.) Don’t bother trying to get Java to run on iOS…