Timur Aykut YILDIRIM Timur Aykut YILDIRIM - 3 years ago 212
Objective-C Question

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download