Using SNMP++ library in application written in Swift - ios

I'm new in Swift and I would like if someone could tell me how can I use SNMP++ lib (written in C++) in a simple iOS application written in Swift? how to import the lib and begin coding? Thank You.

In 2016, Dariusz Stojaczyk wrote C code to construct probes and deconstruct response of SNMP v1 messages hidden in a project focused on BER. That project contains the code necessary to take one or more SNMP OIDs and construct a full SNMP message, then take a response and decode it, but has no networking code to actually do this.
I took this code and added an ObjectiveC wrapper that can be used to use the existing code to construct a SNMPv1 message, send it to an endpoint using a Socket, and receive a response with a user settable timeout using a dispatch block.
This code is far simpler than any of the big libraries that offer additional services like walking, v2/v3, and traps. If all you want to do is read one or more OIDs, then you should appreciate this code.

Objective-C can be mixed with C++.
That's called Objective-C++ (.mm files)
So if you can get the code into those files, in theory you can create a bridging header to deal with linking your swift files: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

Swift 2.2 does not work with C++, and neither will Swift 3. So it's not coming any time soon which means you simply can't develop a Swift app with a C++ library.
However Objective-C works with C++, so although you don't have to write your entire app in Objective-C, any code that deals with a C++ library will have to be written in Objective-C (or in C...).
You can then link your Objective-C code to you Swift app with a bridging header.

Related

Data Serialisation in Objective-C (Avro alternative)

I've been the last week trying to serialise all of the classes of one iOS project (Objective-C).
Due to I use to do it in Android projects with Avro (Apache) library, I've been trying to do the same in the iOS project, however, the first thing that I noticed is that there is not too much information about Avro in iOS.
First thing I tried:
Implement the library ObjectiveAvro using CocoaPods but some classes in the library Avro C were not found when imported.
Second thing I tried:
Remove and do again the previous process but this time only with the Avro C library but the same error appeared.
Third thing I tried:
Implement BlueSteel framework using Carthage. This is a swift library but I wanted to try if I could use it somehow in an Objective-C project. I couldn't.
I just need to serialise and deserialise data providing a schema as you can do with Avro (Android) in an Objective-C project and the last thing that I want to do is convert the whole project to Swift.
So do you know what's the current way to do that in Objective-C?
I could figure it out how to do the Third thing I tried:
Basically, it's necessary to import the library through Carthage, then create a new Swift class which is the one that is going to "talk" with the Swift library, due to we are in an Objective-C project. XCode will ask you to add a "header-class" and press "yes".
Then it's as simple as using the Swift to call the library methods and call the Swift class from the current classes. Swift class in Objective-C project
Finally just to inform that the BlueSteel framework is a nightmare due to is really limited and really poor documented therefore I still don't know a real alternative.
However, if you're going to use it, have in mind that the classes are not autogenerated so you need to do manually the conversion to Avro and sign results.

importing Pure Swift Framework in Objective-C Project

I want to import a Swift framework called "Beethoven" into Objective-C project. I import this framework via cocoapods.
The problem is that framework is written in pure swift. Since the classes are not subclass of NSObject they can't be used directly in my objC classes.
I am newbie in Swift and intermediate in objC, but I think there may be 2 solutions for that:
1-Modifying the whole library: which is probably not an optimal solution.
2-Using a new class which behaves as an interface between swift framework and my objC code. I think second solution will be a better alternative in terms of time and effort needed.
Actually below given post explains modifying the pure swift classes but I don't know how to apply this in my case.
How to use Swift non-NSObject subclass in Objective-C
As I mentioned I am not a very capable programmer and there may be better solutions for that problem. I would be grateful if someone can help me and suggest the best solution for integrating pure Swift framework into my objC project.
NSObject is usually not the only problem. Swift has many features that are not supported by Objective-C. To name a few: swift value types (struct's), swift enums with attached values. If a library uses any of those, it will not be possible to auto-generate an Objective-C header (.h) for it.
Adapting the code of the library on the spot might work, but it is likely more work long term in case if you ever need to update that library again.
Your 2nd approach sounds better: create a layer that is compatible with Objective-C on top of the library which exports methods that need to be exported with #objc and adapts the types. If you go that way consider making a PR contribution to the original library so that everyone could use it in Objective-C projects, and you share responsibility of updating it when the swift code changes.
The 3rd approach would be actually to rewrite some parts of you app to Swift. That might or might not be easier depending on the size of the part of the app that is using the library and how well it is isolated from the rest of the app.

Swift framework for Swift and Objective C projects

I am building a new framework. The project is to be coded into Swift language, however the clients using this framework have the freedom of using either swift or Objective-C framework.
The question is how do I start. There could be numerous issues like
using structs in swift code but it cannot be made available in
objective C framework.
optionals are missing objective c
Even if I
write different set of files for Swift and Objective C, how will I
map them onto different frameworks under the same project.
Enums with other than Int as rawValue can't be used.
Tuples would not work
I know there have been a few questions around this but none have any satisfactory answer.
PS - Any link to a tutorial or blog would be super helpful too
I did this and got some unexpected results: I have trouble integrating the framework in Swift application. Objective-C works just fine.
You mentioned some of the caveats here.
I suggest doing this iteratively while writing test application in Objective-C which uses all the features. This way if there is some feature that does not cross Swift to Objective-C boundary well, it will be discovered as early as possible.
Your remarks about issues are generally correct with one small exception: optionals are not missing from Objective-C, they appear as nullable/nonnull modifiers on variables and method parameters. Although this does not replace optionals fully, it helps detecting issues early in the process.
Here is a random list of some other issues I discovered:
Bridging between Swift Error and NSError used in Objective-C. The conversion is not always as smooth at it could be, so better use NSError in exported code.
If you mix Objective-C and Swift in your framework, you cannot use bridging header, instead using modulemap files which tend to turn pretty large and complex.
Since you cannot embed frameworks inside a framework, you have to make sure that the application sets ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES flag for its target. This has to be clearly indicated in the documentation. Also, when creating fat library for your framework, you have to strip these files from the distributed SDK.
And, as I said in the beginning, I still have no success using the resulting mixed language framework in Swift application.
Hope, this will add to your list of things to take into account when developing the library.

Using data types from a objective-c++ framework in a swift datamodel

I want to use the openCV2 framework in my iOS app, however, I am more comfortable with swift. I can read and understand obj-c well enough to understand the framework and write the methods I need from it in a bridging header, but I'm not comfortable writing the whole app in obj-C. Unfortunately, there are some data types, (cv::Mat, cv::MserManager, etc) that I will need to use in my datamodel, or possibly elsewhere. Is there a way to include datatypes in my bridging header so that I can work with them in swift?
You cannot use C++ types in code called from Swift. However, you can use them in Objective-C++ files, the ones that have the .mm extension. You can mix Objective-C and C++ code in Objective-C++, and can expose Objective-C methods that don't reference C++ in their declarations to Swift via the bridging wrapper. These functions can still use C++ in their implementations, which are not visible in Swift via the bridging header.
You also need to be careful about the language linkage (remember extern "C"?).
Here are some answers that provide examples:
1) Video processing with OpenCV in IOS Swift project
2) Include C++ header file in Swift
3) How to access Swift-objects from a c++ class?
Unfortunately, you cannot bridge C++ and Objective-C++ directly into Swift. On the bright side, you can still work with the openCV2 framework in your app, but you'll need to write C or Objective-C wrappers for your types as described in a related question here: Can I mix Swift with C++? Like the Objective - C .mm files

How do I build a Cocoapod framework for swift, while keeping implementation details hidden?

I am trying to build an iOS swift framework to display encrypted photos. Photos are sent by my server (something like a hashed binary file) after calling a specific API with specific details. I will then decrypt the photo, and display it to the user.
Correct and point me in the right direction if I am wrong, but swift only allows frameworks - meaning no static library. And this will expose my implementation details (such as the method to decrypt my photo).
What I would like to achieve is to create a cocoapod distributable framework for paid developers to implement (once they subscribe to me). It is supposed to expose simple public APIs, and hide implementation details.
I have tried various ways to achieve that but to no avail. I would really like to keep the implementation to swift codes only, with minimal Obj-C codes.
Build a swift framework, and build an objective-c static library as a wrapper
But I cannot seem to get it to work. Any idea if this is possible?
Build a swift framework in a swift framework
Stupid idea, i'm able to see the framework's implementation details within the other framework...
Build a swift framework, and build an objective-c framework as a wrapper
I cannot seem to get this to work either...
I have been working on this project for about 2 weeks now, and have been all over Googling for it. Just in case anybody would like to try, you may try to do the following and check if it works.
Cocoapods Friendly Framework
Implementation Details Hidden
Uses Alamofire (or any public framework that connects to internet)
Any help would be appreciated, thanks!

Resources