I am starting a new Cocoa Swift Project that is incorporating the PubNub SDK via CocoaPods with the following Podfile:
target 'myProject' do
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
pod 'PubNub', '~>4.0'
pod 'Alamofire', '~> 1.3'
end
target 'myProjectTests' do
end
In my auto-generated bridging header I have the import for PubNub as:
#import <PubNub/PubNub.h>
And my AppDelegate.swift file:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var client:PubNub?
func applicationDidFinishLaunching(aNotification: NSNotification) {
let config = PNConfiguration( publishKey: "Your_Pub_Key", subscribeKey: "Your_Sub_Key")
client = PubNub.clientWithConfiguration(config)
client?.addListener(self)
client?.subscribeToChannels(["Your_Channel"], withPresence: false)
client?.publish("Swift + PubNub!", toChannel: "demo", compressed: false, withCompletion: nil) }
func client(client: PubNub!, didReceiveMessage message: PNMessageResult!) {
println(message)
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
The project fails to build due to compiler errors on use of undeclared type PubNub. I've checked the build settings and the Swift Compiler - Code Generation section shows it's pointed to the bridging header file of the target (auto-populated).
Using Xcode 6.4 and pods version 0.38.2
No Bridging-Header when Importing External Frameworks
Straight from Apple Developer Documentation:
Fix
Add
import PubNub
framework.With a single
import
,PubNub
is declared, auto-completes in Xcode editor, compiles, links, builds and runs.Step-by-step Swift Framework Tutorial
Since many comments below imply that Bridging-Headers are always required, wrongly so when using External Frameworks as is presently the case with the
use_frameworks!
directive in thePodfile
, find here a pure Swift solution. It is followed by anXcode
project you can download and experience with.Unambiguously documented in the iOS Developer Library, in concept Using Swift with Cocoa and Objective-C, chapter Mix and Match, section Swift and Objective-C in the Same Project, paragraph Importing External Frameworks:
Podfile
Install the pods
Import Framework
► Find this solution on GitHub and additional details on Swift Recipes.