Is there a way to use Obj-C SDK in a Swift file? I've tried to add cloudinary SDK to my project. I found a way to add libcloudinary.a to my project Frameworks but its not working.
UPDATE: Cloudinary have just published new SDKs written in the Swift language: https://github.com/cloudinary/cloudinary_ios (Swift 3 and Swift 2.3 on separate brunches)
These are working for me:
pod "Cloudinary"
pod install
and then open the workspace file, not the project.#import "Cloudinary/Cloudinary.h"
You may now use the Cloudinary API. Here is some code to upload:
let cloudinary_url = "cloudinary://API_KEY:API_SECRET@CLOUD_NAME"
var uploader:CLUploader = CLUploader(cloudinary, delegate: self)
uploader.upload(UIImageJPEGRepresentation(new_image, 0.8), options: ["format":"jpg"], withCompletion: { ([NSObject : AnyObject]!, errorResult:String!, code:Int, context:AnyObject!) -> Void in
}, andProgress: { (p1:Int, p2:Int, p3:Int, p4:AnyObject!) -> Void in
})
Yes, you can.
First, you will need to get your xcode project setup as per their instructions found here. You will then need to create a bridging header file as defined in the Swift docs.
example
#ifndef Fun_Bridging_Header_h
#define Fun_Bridging_Header_h
#import "Cloudinary.h"
#endif
Unfortunately their objective-c API is not fully interoperable with Swift, namely their upload API (CLUploader).
What I've had to do to make it work was to create a factory class in objective-c for it
The interface (CloudinaryFactory.h)
#ifndef Fun_Wrappy_h
#define Fun_Wrappy_h
#import "Cloudinary.h"
@interface CloudinaryFactory : NSObject
+ (CLUploader*)create:(CLCloudinary*)cloudinary delegate:(id <CLUploaderDelegate> )delegate;
@end
#endif
The implementation (CloudinaryFactory.m):
#import "CloudinaryFactory.h"
@implementation CloudinaryFactory
+ (CLUploader*)create:(CLCloudinary*)cloudinary delegate:(id <CLUploaderDelegate> )delegate
{
return [[CLUploader alloc] init:cloudinary delegate:delegate];
}
@end
And the updated bridging file
#ifndef Fun_Bridging_Header_h
#define Fun_Bridging_Header_h
#import "CloudinaryFactory.h"
#endif
Now, to use it:
var image:UIImage? //todo: i'm assuming you would set this somewhere
@IBAction func uploadGarment(sender: AnyObject) {
let clouder = CLCloudinary(url: "cloudinary://your:cloudinary@url")
let forUpload = UIImagePNGRepresentation(image) as NSData
let uploader = CloudinaryFactory.create(clouder, delegate: self)
uploader.upload(forUpload, options: ["public_id":"testo"])
}
I hope it helps! for more, here's the related blog post
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With