Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly using Obj-C library in Swift project

edit: I think it might be about loading .xib from objective-c library and displaying it on swift's view controller


I've been trying to use https://github.com/promet/PRAugmentedReality library in a swift app.

It works perfectly with obj-c, like: enter image description here

But when i bridge it to swift project it starts to act like this: enter image description here

Below you can see my steps when i integrated this library to my swift project, codes of my swift project and download link for both objective-c and swift projects. Also here are youtube videos for PR Augmented Reality Framework:

Link 1: Getting Started -> https://www.youtube.com/watch?v=fdN0XAoZXOY

Link 2: Walkthrough of Internals-> https://www.youtube.com/watch?v=Xmx-0OnzTPY

Swift project steps:

  1. Creating a single view application with Xcode Version 7.2.1 (7C1002) named demoAR
  2. I add Foundation.framework, CoreGraphics.framework and UIKit.framework from Targets -> demoAR -> Build Phases -> Link Binary with Libraries
  3. Then, File -> New Target -> Cocoa Touch Framework and clicked Next
  4. Created a Target which has these properties: Product Name: PRAR , Language: Objective-C and Organization Identifier: com.timur.PRAR then I clicked Finish enter image description here
  5. I created another group under PRAR and named it as "Classes", Then i copied files from Classes folder of framework and added them to PRAR/Classes folder.
  6. I changed each .h files of framework's Target Membership to "Public"

List item

  1. Then i imported header files to my bridging file (PRAR.h) like this

    #import <PRAR/ARSettings.h>
    #import <PRAR/PRARManager.h>
    #import <PRAR/ARController.h>
    #import <PRAR/ARObject.h>
    #import <PRAR/ARRadar.h>
    #import <PRAR/LocationMath.h>
    
  2. Rest of what is did in order to run this framework within my swift project remains in my ViewController.swift file such as:

    import UIKit
    import PRAR
    var prARManager = PRARManager()
    let x: Int32 = AR_VIEW_TAG
    let y = Int(x)
    class ViewController: UIViewController, PRARManagerDelegate{
    
        func alert(title: String, withDetails details: String) {
            print("alert function")
            let alert: UIAlertView = UIAlertView(title: title, message: details, delegate: nil, cancelButtonTitle: "Ok", otherButtonTitles: "")
            alert.show()
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            print("1")
            // AŞAĞIDAKİ SATIRDA BİR SORUN VAR!!!
            prARManager = PRARManager.init(size: self.view.frame.size, delegate: self, showRadar: true)
            print("2")
        }
    
        override func viewDidAppear(animated: Bool) {
            print("3")
            let locationCoordinates = CLLocationCoordinate2D(latitude: 41.085468, longitude: 29.044033)
            print("4")
            prARManager.startARWithData(self.getDummyData() as [AnyObject], forLocation: locationCoordinates)
            print("5")
        }
    
        func getDummyData() -> NSArray {
            print("6")
            let dummyData: [AnyObject] = [["id": 0, "lat": 40.9780919, "lon": 27.511674, "title": "Tekirdağ"], ["id": 1, "lat": 40.8025157, "lon": 29.4397941, "title": "Gebze"]]
            print("7")
            return dummyData;
        }
    
        func prarDidSetupAR(arView: UIView, withCameraLayer cameraLayer: AVCaptureVideoPreviewLayer, andRadarView radar: UIView) {
            print("8")
            NSLog("Finished displaying ARObjects")
            print("9")
            self.view.layer.addSublayer(cameraLayer)
            print("10")
            self.view!.addSubview(arView)
            print("11")
            self.view.bringSubviewToFront(self.view.viewWithTag(y)!)
            //self.view.bringSubviewToFront(self.view!.viewWithTag(AR_VIEW_TAG));
            //self.view!.bringSubviewToFront(self.view!.viewWithTag( 042313 )!)
            //print("12")
            self.view.addSubview(radar)
            //print("13")
        }
    
        func prarUpdateFrame(arViewFrame: CGRect) {
            print("14")
            self.view.viewWithTag(y)?.frame = arViewFrame
            print("hello")
            //self.view.viewWithTag(AR_VIEW_TAG)!.frame = arViewFrame
            //self.view!.viewWithTag(042313)!.frame = arViewFrame
            print("15")
        }
    
        func prarGotProblem(problemTitle: String!, withDetails problemDetails: String!) {
            print("16")
            self.alert(problemTitle, withDetails: problemDetails)
            print("17")
        }
    }
    

Below you can download the objective-c and swift projects. I just want swift project to be able to run as objective-c project. How can i do that? I'm constantly walking through all framework and my code but i cannot see something wrong. *ps: I'm using Xcode version 7.2.1

Swift project's link: https://drive.google.com/file/d/0B-cDfWHidgvcYnFNcnlMZG1aaDg/view?usp=sharing

Objective-C project's link: https://drive.google.com/file/d/0B-cDfWHidgvcQUFXbktYSE5CeHM/view?usp=sharing

like image 995
t1w Avatar asked Feb 24 '16 02:02

t1w


People also ask

Can I use Objective-C in Swift?

You can use Objective-C and Swift files together in a single project, no matter which language the project used originally. This makes creating mixed-language app and framework targets as straightforward as creating an app or framework target written in a single language.

How do I use Objective-C pod in Swift?

Support Objective-C pods in a Swift project First, create your Podfile and add the pods you need as usual. Install them using the pod install command and open the . xcworkspace file created in your project folder. Pods should now be included in your workspace.

How do I add an Objective-C framework to a Swift project?

The correct approach is as follows: Import your Objective C framework by dragging and dropping the framework into an Xcode 6 Swift project. Create a new Objective C file in your project (File->New->File [Objective C for iOS]). Accept the prompt (agree) to create a bridging header file between Objective C and Swift.

Is OBJ C still used?

While Objective-C is still supported by Apple and will likely not be deprecated anytime soon, there will be no updates to the language.


2 Answers

You forgot to add framework resources to you Swift project, see the Obj-C project:

enter image description here

compared to Swift project:

enter image description here

Make sure all the necessary images and xibs have target membership in "demoAR":

enter image description here

like image 153
Sulthan Avatar answered Oct 28 '22 13:10

Sulthan


If you look at your Obj-C project, you'll see the image files (like RadarMV.png) are assigned to the app target. But in the Swift project, they're assigned to the PRAR target. So the app just isn't finding the images it's trying to load.

You could modify the code to find the images in the PRAR bundle. But a quicker solution (maybe not better) is to just assign the images to the app target:

Image Targets

This was enough to get things working for me.

like image 31
Dave Batton Avatar answered Oct 28 '22 14:10

Dave Batton