我嘗試在我正在開發的OS X應用程序中使用Core藍牙框架,它基本上將Mac連接到您的iPhone,以通過藍牙LE執行不同的操作。如何在OSX應用程序中使用Core藍牙?
問題是,當我只是初始化CBCentralManager和它的委託方法我得到一個奇怪的錯誤:
Undefined symbols for architecture x86_64:
"_CBAdvertisementDataLocalNameKey", referenced from:
__TFC5test311AppDelegate14centralManagerfS0_FTGSQCSo16CBCentralManager_21didDiscoverPeripheralGSQCSo12CBPeripheral_17advertisementDataGSQGVSs10DictionaryCSo8NSObjectPSs9AnyObject___4RSSIGSQCSo8NSNumber__T_ in AppDelegate.o
"_OBJC_CLASS_$_CBCentralManager", referenced from:
__TMaCSo16CBCentralManager in AppDelegate.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
這是我下面的代碼:
import Cocoa
import CoreBluetooth
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, CBCentralManagerDelegate {
@IBOutlet weak var window: NSWindow!
var centralManager: CBCentralManager!
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
var localName: String = advertisementData[CBAdvertisementDataLocalNameKey] as String
if (countElements(localName) > 0) {
println("Found Mac: \(localName)")
self.centralManager.stopScan()
} else {
println("Not Found")
}
}
func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
println("1")
}
func centralManager(central: CBCentralManager!, didDisconnectPeripheral peripheral: CBPeripheral!, error: NSError!) {
println("2")
}
func centralManagerDidUpdateState(central: CBCentralManager!) {
switch (central.state) {
case .PoweredOff:
println("Powered Off")
case .PoweredOn:
println("Powered On")
self.centralManager.scanForPeripheralsWithServices(nil, options: nil)
case .Unauthorized:
println("Unauthorized")
case .Unknown:
println("Unknown")
case .Unsupported:
println("Unsupported")
default:
println("Default")
}
}
func applicationDidFinishLaunching(aNotification: NSNotification) {
self.centralManager = CBCentralManager(delegate: nil, queue: nil)
// Insert code here to initialize your application
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
我不知道爲什麼我得到那個錯誤,或者我做錯了什麼
哈哈,是的我忘了將其添加爲鏈接的框架,感謝的人 – user4860206
很高興我能幫忙。我讓那些「doh!」不斷有錯誤。 – Tobias
與我創建的CocoaPods庫有相同的問題。需要將'spec.framework ='CoreBluetooth''添加到podspec文件 –