Please select Into the mobile phone version | Continue to access the computer ver.
Close
You need to log in before you can reply       Login | Register now

[Bug] Swift, beginner setup guide?

Author: cboxdk 2015-9-1 20:42
20 7397
Hi,

I'm trying to create a simple app. I don't have much expirience with IOS development and SWIFT seems to be the easy way for me.
I have created a project and imported the DJISDK and created a bridging header. The app also compiles, so i got this part right.

However, i do strugle a bit with the registerapp for a start.
How should this translate into swift code?
[DJIAppManager registerApp:appKey withDelegate:self];

Second, i don't have ll the options in xcode described in the DJI guide. E.g. the header search path.
How can i include the VideoPreviewer in the swift project?

Thank you.

Brgds
Sylvester Nielsen

Reply
Replies
jmiller
To the rescue!

func camera(camera: DJICamera!, didReceivedVideoData videoBuffer: UnsafeMutablePointer<UInt8>, length: Int32) { // camera data comes through here
        var newInt = length as CInt
        var thirdInt = Int(newInt)
        var pbuffer = UnsafeMutablePointer<UInt8>(malloc(thirdInt))
        memcpy(pbuffer, videoBuffer, thirdInt)
        VideoPreviewer.instance().dataQueue.push(pbuffer, length: length)
    }
2015-9-3 21:48
ZEN UAV
Last edited by ZEN UAV In 2015-9-1 14:31 Editor

It's going to be a very steep learning curve for you my friend ;)

I will help you get started with the register app bit and you will have to move forward from there.

1. In you AppDelegate.swift file, add the DJIAppManagerDelegate delegate protocol as below. This says that this class adhears to the app manager protocol.
class AppDelegate: UIResponder, UIApplicationDelegate, DJIAppManagerDelegate {

2. Now in the same file, make your application method look like this. This executes the register app when the app is launched.

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // call dji register app
        DJIAppManager.registerApp(DJIAppKey, withDelegate: self)

        // return true
        return true

    }

3. Now, when the resisterApp method is done (i.e. going to DJI servers if not already registered) the following delegate method handles to responce.

    // appManagerDidRegisterWithError
    func appManagerDidRegisterWithError(errorCode: Int32)
    {
        // check for app registration error
        if (errorCode == RegisterSuccess)
        {
            // app is registered
        }
        else
        {
            // app registration failed
        }

    }

I wish you well with your development. The SDK is not documented as such, so It's a case of trial and error often. Big tip 1: Install the PC Simulator and take the props off your drone while testing.


2015-9-1 21:27
cboxdk
Hi.. Thank you.

Yes, IOS is a new field to me, but i'm a fast learner. ;)

I did solve it though by trial & error.

class AppDelegate: UIResponder, UIApplicationDelegate, DJIAppManagerDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        var code: String = "code";
        DJIAppManager.registerApp(code, withDelegate: self)
        
        return true
    }
   
    func appManagerDidRegisterWithError(ErrorCode: CInt)
    {
        var Message: String = "Application is registered.";
        
        if (ErrorCode != RegisterSuccess) {
            Message = "Register App Failed!";
        }
        
        dispatch_async(dispatch_get_main_queue(), {
            var alert = UIAlertController(title: "Alert", message: Message, preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
            //self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
            self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)

        })
        
    }
2015-9-1 21:36
cboxdk
Great tip about the PC simulator. Thank you.
I didn't try it out before now as the documentation is in chinese.
2015-9-1 21:57
cboxdk
Can you give me an advice of how to connect the ipad to both xcode and the transmitter?
Is it possible to do a remote connection or buy a USB hub for ipad?
2015-9-1 22:55
ZEN UAV
I'm having to switch cables by hand, so can not debug and connect to drone at same time. I need to look into remote debug connection too. I don't think a USB Hub will work will it??
2015-9-1 23:24
cboxdk
Last edited by cboxdk In 2015-9-1 18:59 Editor

I will get one and try it out.
It would be nice to debug that way.

Do you have a translation into swift for this method?
As far as i understood it should be needed to allocate memory in Swift as in obj-c.
However, i can't send the videoBuffer directly to the dataQueue method.

-(void) camera:(DJICamera*)camera didReceivedVideoData:(uint8_t*)videoBuffer length:(int)length
{
    uint8_t* pBuffer = (uint8_t*)malloc(length);
    memcpy(pBuffer, videoBuffer, length);
    [[VideoPreviewer instance].dataQueue push:pBuffer length:length];
}

2015-9-2 00:58
suqsid.bobmail
ZEN UAV Posted at 2015-9-1 11:24
I'm having to switch cables by hand, so can not debug and connect to drone at same time. I need to l ...

Hub will not work.  

The PC acts as the host when connected to the device.

The remote acts as the host when connected to the device.

Adding the hub would have two hosts, the PC and the remote.
2015-9-2 01:02
ZEN UAV
I didn't think a hub would  work, thanks for confirming though.
2015-9-2 03:49
ZEN UAV
I don't know very much objc so I can't help.

I'm not using the method in the iOS example, I'm trying to get Harware Decoding to work, not with much luck at moment!
2015-9-2 03:52
cboxdk
Wauw.. That simple video preview is really causing me a lot of troubles. Has anyone got it to work in swift?
Obj-c is straight forward with the examples, but i don't want to create my app in Obj-c..

DJI, please create a basic Swift example as well.
2015-9-3 18:12
ZEN UAV
New to Swift, but would the following not just work?

func camera(camera: DJICamera!, didReceivedVideoData videoBuffer: UnsafeMutablePointer<UInt8>, length: Int32) { // camera data comes through here

         VideoPreviewer.instance().dataQueue.push(videoBuffer, length: Int(length))
     }
2015-9-4 01:54
cboxdk
The method i made is:

    func camera(camera: DJICamera!, didReceivedVideoData videoBuffer: UnsafeMutablePointer<UInt8>, length: Int32)
    {
        VideoPreviewer.instance().dataQueue.push(videoBuffer, length: length)
    }

However it crashes as soon as the video is started.
It compiles and everything looks fine, but as it's not possible to debug with a connected drone it's getting real hard.
Thank you for your suggestion, it's really appreciated.
2015-9-4 02:38
jmiller
I can confirm the code I posted works, it's what I use currently.
2015-9-4 02:43
cboxdk
Thank you very much @jmiller.
And sorry i didn't see your response. I will test it out right away.
2015-9-4 02:48
cboxdk
And i confirm it does work.
That was the last step needed for me to create the mock up app i needed.
Everything is working now.

Once again, thank you!
2015-9-4 03:18
ckizer
Someone please share a working SWIFT xcode project with the bridging headers on github!
2016-1-4 16:23
pirate7
2017-3-10 16:03
unmannedairline
Has anyone gotten the VideoPreviewer with Swift in SDK 4.0.1? We are in the process of rewriting DronePan in Swift and there is little documentation out there on this. Even the DJI Swift Demo doesn't include a VideoPreviewer example. It's all Obj-C. I've been hacking around on this for a couple of days and have ended up with the following, which doesn't work:

extension CameraViewController : DJIVideoFeedListener {
   
    func videoFeed(_ videoFeed: DJIVideoFeed, didUpdateVideoData videoData: Data) {
        
        var result = videoData.withUnsafeBytes { (pointer: UnsafePointer<UInt8>) -> UInt8 in
            return pointer.pointee
        }        
        VideoPreviewer.instance().push(&result, length: Int32(videoData.count))
        
    }
   
}

Any thoughts/ideas would be appreciated.
2017-5-2 23:39
Samuel5
Last edited by Samuel5 In 2017-5-10 15:19 Editor


Hi,

I know it has been awhile since the original post but I was wondering if you could explain how you imported the DJISDK and created your bridging header.

I have been trying for a while but I don't seem to be going anywhere with my bridging header and my "import DJISDK" gives me the following error:"Could not build Objective-C module 'DJISDK' ". Could it maybe have something to do with the pods' installation?

Thank you.

Issue fixed, my Objective-C importation of DJISDK wasn't correctly done which caused my Swift importation of DJISDK to fail.






2017-5-8 22:54
You need to log in before you can reply Login | Register now

Quick Reply Back to top Back to list
Quick Reply Back to top Back to list