Reputation: 53
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)
Upvotes: 4
Views: 3305
Reputation: 81
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
Upvotes: 5
Reputation: 384
Create Bridging Header File in your project. After that import .h file into bridging-header file.
See here : 1 step: xyz-Bridging-header.h
Import Cloudinary.h file into Bridging-header file
#import "Cloudinary.h"
2 Step : ViewController.swift
Class viewController : UIViewController {
cloudinary: CLCloudinary = CLCloudinary()
override func viewDidLoad() {
super.viewDidLoad()
// Cloudinary Setup
cloudinary.config().setValue("xxxxxxxx", forKey: "cloud_name")
cloudinary.config().setValue("xxxxxxxx", forKey: "api_key")
cloudinary.config().setValue("xxxxxxxx", forKey: "api_secret")
}
}
Your Cloudinary setup is done. now you can able to use.
Upvotes: 1
Reputation: 3183
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
})
Upvotes: 6
Reputation: 1931
You can have a look at the iOS sample code here: https://github.com/cloudinary/cloudinary_ios#upload.
In addition, here's a code reference: https://github.com/cloudinary/cloudinary_ios/blob/master/CloudinaryTests/UploaderTests.m
Also, you can use Cloudinary's unsigned upload. Here's some more information including an iOS example: http://cloudinary.com/blog/direct_upload_made_easy_from_browser_or_mobile_app_to_the_cloud#api_example_2
Upvotes: 1