Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloudinary SDK in Swift project

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)

like image 464
AmiH Avatar asked Dec 12 '22 02:12

AmiH


2 Answers

These are working for me:

  1. Create or append to the Podfile file: pod "Cloudinary"
  2. Run pod install and then open the workspace file, not the project.
  3. Add an Obj-C file to your project (http://cl.ly/image/272V1Z2Q3g0e) and when asked say yes to adding an Objective-C bridging header
  4. In this file add #import "Cloudinary/Cloudinary.h"
  5. 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
    
    })
    
like image 113
abinop Avatar answered Jan 08 '23 01:01

abinop


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

like image 27
Boguste Hameyie Avatar answered Jan 08 '23 01:01

Boguste Hameyie