Closed ghost closed 7 years ago
Hi Fabio,
Unfortunately, the SDK isn't setup very well for Swift, but it can be done. You need to use a bridging header. If you look at https://github.com/syoung-smallwisdom/BrickBot you can see an example of how to setup the project.
Have fun and good luck,
S
Hey @fabioknoedt and @syoung-smallwisdom!
Unfortunately we haven't been able to dedicate time to making the Bean SDK easy to use in Swift yet. But I can try to help by detailing the steps I used to get the SDK working in my Swift app.
Cocoapods works with Swift stuff now. Set up your project with Cocoapods, then add this to your Podfile:
use_frameworks!
pod 'Bean-iOS-OSX-SDK'
Then run pod install
.
You may want also want to add inhibit_all_warnings!
to your Podfile to hide warnings from the SDK.
There's an error regarding the preprocessor macros that I haven't figured out yet. In the meantime, you'll have to open Pods/Bean-iOS-OSX-SDK/AppMessageTypes.h and replace its contents with the following:
#ifndef APPMESSAGETYPES_H
#define APPMESSAGETYPES_H
typedef UInt32 PTD_UINT32;
typedef UInt16 PTD_UINT16;
typedef UInt8 PTD_UINT8;
typedef SInt16 PTD_INT16;
#endif
Add this line to the Swift files that use the SDK:
import Bean_iOS_OSX_SDK
Here's an example that scans for Beans:
import UIKit
import Bean_iOS_OSX_SDK
class MyViewController: UITableViewController, PTDBeanManagerDelegate {
var beanManager: PTDBeanManager?
override func viewDidLoad() {
super.viewDidLoad()
beanManager = PTDBeanManager()
beanManager!.delegate = self
}
func startScanning() {
var error: NSError?
beanManager!.startScanningForBeans_error(&error)
if let e = error {
print(e)
}
}
func beanManager(beanManager: PTDBeanManager!, didDiscoverBean bean: PTDBean!, error: NSError!) {
print("Found a Bean: \(bean)")
}
}
Let me know if this doesn't work out for you! I am happy to help.
followed all the steps mentioned above in 3 different new projects. also i am opening . xcworkspace
have to correct AppMessages.h,
from
PTD_INT8 rssi; ///< Advertisement or SCAN_RSP RSSI
to
PTD_UINT8 rssi; ///< Advertisement or SCAN_RSP RSSI, made to UINT8
i am getting Bean-iOS-OSX-SDK/Bean OSX Static Library/Frameworks/OCMock.framework/Headers/OCMock.h:17:9: 'OCMock/OCMockObject.h' file not found
i am using xcode 8 with swift3,
Pls guide me.
@vtbalaji I just had this problem too...Here is a workaround:
Pods/Bean-iOS-OSX-SDK
group (move to trash)Pods/Support Files/Bean-iOS-OSX-SDK-umbrella.h
, delete all of the lines referencing the now-deleted OCMock files (I had 4 copies of this set of files in the umbrella)Here is a workaround for Bean-iOS-OSX-SDK (3.0.1) pod and Xcode8+Swift3:
Add to the Podfile
:
pre_install do |install|
puts(`fix-bean-pod.sh`)
end
Create executable (chmod +x) file fix-bean-pod.sh
:
#!/bin/sh
echo "Fixing Bean-iOS-OSX-SDK Swift 3 problems..."
echo "- removing OCMock"
rm -rf "Pods/Bean-iOS-OSX-SDK/Bean OSX Static Library/Frameworks/OCMock"
rm -rf "Pods/Bean-iOS-OSX-SDK/Bean OSX Static Library/Frameworks/OCMock.framework"
echo "- patching AppMessageTypes.h"
chmod 644 "Pods/Bean-iOS-OSX-SDK/App Message Definitions/AppMessageTypes.h"
patch -p1 < "fix-bean-pod.patch"
echo "done"
Create fix-bean-pod.patch
file:
--- 1/Pods/Bean-iOS-OSX-SDK/App Message Definitions/AppMessageTypes.h 2016-10-05 12:24:43.000000000 +0300
+++ 2/Pods/Bean-iOS-OSX-SDK/App Message Definitions/AppMessageTypes.h 2016-10-05 12:26:16.000000000 +0300
@@ -66,7 +66,11 @@
typedef SInt16 PTD_INT16;
#else
-#error define a platform/language that you are building for
+typedef UInt32 PTD_UINT32;
+typedef UInt16 PTD_UINT16;
+typedef UInt8 PTD_UINT8;
+typedef SInt8 PTD_INT8;
+typedef SInt16 PTD_INT16;
#endif
Run pod install
@alex-vasenin đź‘Ť
I haven't tried your script, but does it also need to remove OCMock references from the umbrella.h
file?
@wasnotrice *-umbrella.h
files generated by Cocoa Pods automatically while pod install
ah, right, I see, the script runs inpre_install
Hi @alex-vasenin -- thanks for posting that workaround. Super useful!
It does look like there might be something off with my setup although I can't really wrap my head around it. When I try to apply the solution you've provided this is the output I get:
Fixing Bean-iOS-OSX-SDK Swift 3 problems...
- removing OCMock
- patching AppMessageTypes.h
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- 1/Pods/Bean-iOS-OSX-SDK/App Message Definitions/AppMessageTypes.h 2016-10-05 12:24:43.000000000 +0300
|+++ 2/Pods/Bean-iOS-OSX-SDK/App Message Definitions/AppMessageTypes.h 2016-10-05 12:26:16.000000000 +0300
--------------------------
File to patch:
Skip this patch? [y]
Skipping patch.
patch unexpectedly ends in middle of line
1 out of 1 hunk ignored
done
Any suggestions on what could this be?
It sounds like you are missing the AppMessageTypes.h file. This typically happens when your submodules have not been cloned along with the full repo. Try git submodule update --init --recursive
– which may be a bit redundant, but it's what I remember working.
@mseijas -- I had the same problem with the patch failing. After loosing some hair, I finally figured out it was because the file path has spaces in it and patch can't handle it. I ended up renaming the directory, doing the patch, and changing the directory back. In the shell script:
mv './Pods/Bean-iOS-OSX-SDK/App Message Definitions' ./Pods/Bean-iOS-OSX-SDK/App_Message_Definitions
patch -p1 < "fix-bean-pod.patch"
mv ./Pods/Bean-iOS-OSX-SDK/App_Message_Definitions './Pods/Bean-iOS-OSX-SDK/App Message Definitions'
and in the patch file:
--- 1/Pods/Bean-iOS-OSX-SDK/App_Message_Definitions/AppMessageTypes.h 2016-10-05 12:24:43.000000000 +0300
+++ 2/Pods/Bean-iOS-OSX-SDK/App_Message_Definitions/AppMessageTypes.h 2016-10-05 12:26:16.000000000 +0300
@sigpoggy ah that makes total sense. thanks for sharing that! that was super helpful :)
For anyone that is still having trouble with this, I just took another stab at this and this is how it's working for me now.
#!/bin/sh
echo "Fixing Bean-iOS-OSX-SDK Swift 3 problems..."
echo "- removing OCMock"
rm -rf "Pods/Bean-iOS-OSX-SDK/Bean OSX Static Library/Frameworks/OCMock"
rm -rf "Pods/Bean-iOS-OSX-SDK/Bean OSX Static Library/Frameworks/OCMock.framework"
echo "- patching AppMessageTypes.h"
mv "Pods/Bean-iOS-OSX-SDK/App Message Definitions" "Pods/Bean-iOS-OSX-SDK/App_Message_Definitions"
chmod 644 "Pods/Bean-iOS-OSX-SDK/App_Message_Definitions/AppMessageTypes.h"
patch -p1 < "fix-bean-pod.patch"
mv "Pods/Bean-iOS-OSX-SDK/App_Message_Definitions" "Pods/Bean-iOS-OSX-SDK/App Message Definitions"
echo "Done"
--- 1/Pods/Bean-iOS-OSX-SDK/App_Message_Definitions/AppMessageTypes.h
+++ 2/Pods/Bean-iOS-OSX-SDK/App_Message_Definitions/AppMessageTypes.h
@@ -66,7 +66,15 @@ typedef int8_t PTD_INT8;
typedef SInt16 PTD_INT16;
#else
-#error define a platform/language that you are building for
+
+/* ----------- Swift ----------- */
+
+typedef UInt32 PTD_UINT32;
+typedef UInt16 PTD_UINT16;
+typedef UInt8 PTD_UINT8;
+typedef SInt8 PTD_INT8;
+typedef SInt16 PTD_INT16;
+
#endif
^ (that extra line at the end of the patch file is important! i was getting errors interpreting the patch file when I didn't have it in there)
target 'MyApp' do
pod 'Bean-iOS-OSX-SDK'
end
pre_install do |install|
puts(`./fix-bean-pod.sh`)
end
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
Good luck! :)
Hi everyone! I tried out the solution that @mseijas posted (thanks, dude!), which has helped me progress in getting the BlueBean SDK to work with Swift 3 (and Xcode 8.2.1). However, I ran into an issue that is new(?) to the latest version of the BlueBean SDK (version 3.0.2). When trying to compile my swift iOS app, after applying @mseijas's fix, I get the following compilation error:
Semantic Issue: Pods/Bean-iOS-OSX-SDK/source/Profiles/Gatt Serial/Transport/GattSerialMessage.m:43:13: Implicit declaration of function 'PTDLog' is invalid in C99
PTDLog() seemed to work for past versions of the Bean-iOS-OSX-SDK, but not this most recent version. Could this be an issue with my Build Settings of my project? Is it a bug in the latest version of the SDK? Has anyone else run into this issue yet? What can be done to fix this?
Cheers!
Hi all,
Thank you for your patience while we fixed this extremely frustrating issue, and special thanks to those of you who took the time to develop and post these workarounds. In the latest release (3.0.5), we updated the podspec to exclude all OCMock-related files and fix the order of imported headers in the auto-generated umbrella file from Cocoapods. The latter should fix the issues with the missing typedefs. Please let us know if any of these problems persist.
Wha's the best way to use the SDK in a Swift project? Bridge header?
use_frameworks!
in Cocoapods? Another approach? Thanks!