Adam W
Adam W

Reputation: 1012

How to Integrate Swift CocoaPods with a bridging header file

I've recently started doing iOS and I'm attempting to integrate the Vitamio Objective-C library with my Swift Project. I use CocoaPods to import various swift libraries. Vitamio is integrated with a bridging-header file.

Bridging-Header File

#ifndef inclub_Bridging_Header_h
#define inclub_Bridging_Header_h

#import <Foundation/Foundation.h>
#import "VDefines.h"
#import "VPlayerManageDef.h"
#import "VMediaPlayer.h"
#import "VMediaPlayerDelegate.h"
#import "VMediaExtracterDef.h"
#import "VMediaExtracter.h"

#endif /* inclub_Bridging_Header_h */

PodFile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target 'inclub' do
    pod "Player", "~> 0.2.0"
    pod 'Alamofire', '~> 4.0'
    pod 'Kingfisher', '~> 3.0'
    pod 'ObjectMapper', '~> 2.2'
    pod 'UIImageViewAlignedSwift'
    post_install do |installer|
        installer.pods_project.targets.each do |target|
            target.build_configurations.each do |config|
                config.build_settings['SWIFT_VERSION'] = '3.0'
            end
        end
    end
end

Sample Swift File

import Alamofire
import Foundation

class BackEnd {
    typealias JsonResponseHandler = (_ response: DataResponse<String>) -> Void
    typealias ErrorHandler = (_ error: Error) -> Void

    let defaultSession = URLSession(configuration: URLSessionConfiguration.default)
    var dataTask: URLSessionDataTask?


    static func tabletLogIn(user: String, password: String, responseHandler: @escaping JsonResponseHandler,  errorHandler: @escaping ErrorHandler) {
        let url = UrlFactory.getLogInUrl(user: user, pass: password)
        print("Calling URL: " + url);

        makeRequest(urlString: url, method: .post, responseHandler: responseHandler, errorHandler: errorHandler)
    }

    static func getAdOrderInfo(orderId: String, responseHandler: @escaping JsonResponseHandler,  errorHandler: @escaping ErrorHandler) {
        let url = UrlFactory.getAdOrderInfoUrl(orderId: orderId)
        print("Calling URL: " + url);

        makeRequest(urlString: url, method: .post, responseHandler: responseHandler, errorHandler: errorHandler)
    }

    static func getDetailedMusicChannels(responseHandler: @escaping JsonResponseHandler,  errorHandler: @escaping ErrorHandler) {
        let url = UrlFactory.getMusicChannelUrl()
        print("Calling URL: " + url);

        makeRequest(urlString: url, method: .post, responseHandler: responseHandler, errorHandler: errorHandler)
    }

    static func getTvChannels(responseHandler: @escaping JsonResponseHandler,  errorHandler: @escaping ErrorHandler) {
        let url = UrlFactory.getTvChannelUrl()
        print("Calling URL: " + url);

        makeRequest(urlString: url, method: .post, responseHandler: responseHandler, errorHandler: errorHandler)
    }

    static func makeRequest(urlString: String, method: HTTPMethod, responseHandler: @escaping JsonResponseHandler, errorHandler: @escaping ErrorHandler) {
        Alamofire.request(urlString, method: method).validate().responseString {response in
            switch response.result {
            case .success:
                responseHandler(response)
                break
            case .failure(let error):
                errorHandler(error)
                break
            }
        }
    }
}

In this example file the Alamofire import works but the first error message displayed in no class for type DataResponse.

Whenever, I add the Bridging Header File to project -> target -> Objective-C Briding Header the Vitamio library compiles; however, none of the classes added by my CocoaPods work.

If I remove the bridging header file the CocoaPods work but Vitamio does not.

Any help would be appreciated.

Upvotes: 3

Views: 5677

Answers (1)

Fernando Mazzon
Fernando Mazzon

Reputation: 3591

When you use "use_frameworks!" in the Podfile as you are, you don't need to import anything in the bridging header. Cocoapods is making frameworks for each of those pods for you, so you can just import them as if they were common frameworks directly on your swift file:

import Vitamio

The name might be different, as I don't know this particular library.

Edit: seems I misunderstood the problem a bit, waiting on more info.

Upvotes: 3

Related Questions