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
Straight from Apple Developer Documentation:
You can import external frameworks that have a pure Objective-C codebase, a pure Swift codebase, or a mixed-language codebase. [...] You can import a framework into any Swift file within a different target using the following syntax:
import FrameworkName
Add import PubNub
framework.
import UIKit
import PubNub
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var client:PubNub?
// ...
}
With a single import
, PubNub
is declared, auto-completes in Xcode editor, compiles, links, builds and runs.
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 the Podfile
, find here a pure Swift solution. It is followed by an Xcode
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:
The process for importing an external framework is the same whether the framework is written in a single language or contains files from both languages.
Podfile
platform :ios, '8.0'
use_frameworks!
target 'SO-31642385' do
pod 'PubNub', '~>4.0'
pod 'Alamofire', '~> 1.3'
end
Install the pods
] pod install
Downloading dependencies
Installing Alamofire (1.3.1)
Installing CocoaLumberjack (2.0.0)
Installing PubNub (4.0.4)
Generating Pods project
Integrating client project
Please close any current Xcode sessions and use `SO-31642385.xcworkspace` for this project from now on.
Import Framework
import UIKit
import PubNub
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var client:PubNub?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.client = PubNub()
return true
}
// ...
}
► Find this solution on GitHub and additional details on Swift Recipes.
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