-2


I want to get the device location in the application that I want to develop.
I have created a SWIFT project and I want to add the code here
https://github.com/intuit/LocationManager
into my existing swift project.

So here it says

Manually from GitHub

Download all the files in the INTULocationManager subdirectory.
Add the source files to your Xcode project (drag and drop is easiest).
Import the INTULocationManager.h to your bridging header.
Swift: Add #import "INTULocationManager.h" to your bridging header.

So basically can I just drag the source code to my project?

Also how do I create the bridging header?
then let say how do I use it in my ViewController?

Thanks

borna
  • 906
  • 3
  • 12
  • 32

1 Answers1

1

Maybe you should consider using CocoaPods as it may be easier for you to integrate, and in the long-run, is easier to update.

To answer your specific question about a manual installation:

Yes, you can just drag the source code into your project. Instructions for creating the bridging header can be found on the internet (for example, here).

Next, as the instructions say, add #import "INTULocationManager.h" to your bridging header, e.g:

#ifndef TestBridgingHeader_Bridging_Header_h
#define TestBridgingHeader_Bridging_Header_h

#import "INTULocationManager.h"

#endif /* TestBridgingHeader_Bridging_Header_h */

To use the library, you have to translate the example given on the Github page from Objective-C to Swift.

let locMgr = INTULocationManager.sharedInstance()
locMgr.requestLocationWithDesiredAccuracy(INTULocationAccuracy.City, timeout: 10, block: { currentLocation, achievedAccuracy, status in
    if status == .Success {

    } else if status == .TimedOut {

    } else {

    }
})

This is made easier by remembering a few rules:

  • Method calls in Obj-C are called via enclosing square brackets (e.g. [INTULocationManager sharedInstance]) while in Swift they use dot-syntax (e.g. INTULocationManager.sharedInstance())
  • Enums such as INTULocationAccuracyCity generally get translated to only their last part (.City in this case). Swift infers that .City is equivalent to INTULocationAccuracy.City.
  • Obj-C blocks such as INTULocationRequestBlock are equivalent to Swift closures of the same type.
Community
  • 1
  • 1
paulvs
  • 11,963
  • 3
  • 41
  • 66
  • Thanks for the info you provided. So I created a swift project called Location2 Added Location2-Bridging-Header.h with #import "INTULocationManager.h" in it. Also dragged and drop everything under INTUHeadingManger into my project. Added the code in swift that you mentioned above into viewDidLoad of ViewController. No syntax error or anything, but I get error when I run the program – borna Mar 19 '16 at 04:56
  • Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_INTULocationManager", referenced from: type metadata accessor for __ObjC.INTULocationManager in ViewController.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) – borna Mar 19 '16 at 04:56