Trying to pull properties from another class in iOS

Go To


I am completely new to iOS development so I may be doing this wrong but I have a class I am using to get coordinate gps data that I want to have as a generic class I can reuse in lots of apps. My problem is getting the data from the gps to properly display in other apps.

Here is my header file for the GPS class:

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@interface LocationAwareness : NSObject <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
@property(copy) NSString *longitude;
@property(copy) NSString *latitude;


And here is the implementation:

#import "LocationAwareness.h"

@implementation LocationAwareness
@synthesize longitude;
@synthesize latitude;

- (id)init {

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
return self;

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation   *)newLocation fromLocation:(CLLocation *)oldLocation {

    // Stops updating location if data has been updated within 10 minutes
    if ( abs([newLocation.timestamp timeIntervalSinceDate: [NSDate date]]) < 600) {

        [locationManager stopUpdatingLocation];
        float latitudedata = newLocation.coordinate.latitude;
        latitude = [NSString stringWithFormat:@"%f", latitudedata];

        float logitudedata = newLocation.coordinate.longitude;
        longitude = [NSString stringWithFormat:@"%f", logitudedata];



Now I can't seem to find anywhere that tells me how to get the latitude or longitude properties in another project. I have the header imported and have tried to store LocationAwareness.latitude into a variable that I can use but everything I store it in ends up blank. When I start my main class and aloc init a locationawareness object the gps fires up so I think its working but I don't seem to know enough about how this works to get everything in order. I've been searching the internet for hours. Anyone have an idea what I am doing wrong?

2012-04-04 02:22
by Joel Smith
Hard to know what is going on without seeing how you are implementing all this. As a reality check, perhaps try putting a log statement in the locationManager:didUpdateToLocation: function so you are sure that there are values to get. In other word: NSLog(@"longitude: %@", longitude);No Grabbing 2012-04-04 02:27
Maybe I should say I want to know what a simple import and print to screen would look like. I know how to work with IBOulets and all that but I honestly have no idea how to make a property cross from one class to another - Joel Smith 2012-04-04 02:34


Well, this may or may not be causing the problem (it's quite likely), but a major problem is your init method.

The beginning should be:

self = [super init];
if (self) {
    // Do your initializing as you did above.
return self;

I added your code with my update to a project and it works well. In order to use this, you should do something like the following:

LocationAwareness *loc = [[LocationAwareness alloc] init];

// Give it some time to start updating the current location and then
// in a different function:
NSLog(@"%@", loc.latitude);

Wherever you are using this, you will want to declare a property which stores it so that you can create it once and reference it many times. To do that, use the following code:

In the header for the object where you want to use this object, add this with the other properties:

@property (nonatomic, assign) LocationAwareness *location;

Then, towards the top of your implementation file (.m file) you should see other @synthesize lines, add this one:

@synthesize location;

Then, create the actual location instance that you want to use as per the example above:

self.location = [[LocationAwareness alloc] init];

Now give it some time to figure out your location and start providing updates. Then you can print the location like this:

NSLog(@"%@", self.location.latitude);
2012-04-04 02:33
by lnafziger
Yeah I also should have mentioned I didn't know if the init is the proper thing to do there - Joel Smith 2012-04-04 02:42
Yes, but if you don't call super, none of the classes that you inherit from (including NSObject) get initialized. This will cause all kinds of crazy errors - lnafziger 2012-04-04 02:44
like this? - (id)init { self = [super init];

if (self) {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy =      kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
return self;
< - Joel Smith 2012-04-04 02:50
The tutorial I was following was for a viewcontroller but I wanted to change i - Joel Smith 2012-04-04 02:52
Yes, like that.. I updated with an example of use as well - lnafziger 2012-04-04 03:04
hmm tried your second code block in a brand new project in the viewdidload function noting came up in nslog: 2012-04-03 20:08:56.794 ClassTest[58569:c07] (null) any ideas? maybe I have header wrong - Joel Smith 2012-04-04 03:11
Are you using ARC - lnafziger 2012-04-04 03:14
don't know what that is so probably not : - Joel Smith 2012-04-04 03:33
Oh boy, my suggestion (if you are running 5.0+) is to enable it by selecting Use Automatic Reference Counting when you create your project. Otherwise, you really need to read Apple's memory management guide, as there is a lot that you will need to change in order to get this to work - lnafziger 2012-04-04 03:54
hmmmm really? Actually think I did check tha - Joel Smith 2012-04-04 05:33
Oh, I just figured out why. That will be nil until the location has the chance to update. I just added this to a project and it works well, but I saved the reference to a declared property (so that I had global access to it in my view controller) and then used a button to print the log line. Works perfectly - lnafziger 2012-04-04 23:03
Do you know how to do this part - lnafziger 2012-04-06 03:12
Need to see the code to understand. Could you post it as an answer here - Joel Smith 2012-04-06 06:05
I updated my answer - lnafziger 2012-04-06 17:06
what did you use for the nslog statement? I can't seem to pull value

I've tried: NSLog(@"%@", location.latitude); NSLog(@"%@", self.location.latitude) - Joel Smith 2012-04-06 21:07

Oops, sorry, I updated the answer. It is NSLog(@"%@", self.location.latitude); You have to give it time to determine your location though, so you probably want to have it called when you press a button or something like that - lnafziger 2012-04-06 21:21
the button worke - Joel Smith 2012-04-07 00:54