Trying to pull properties from another class in iOS

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;

@end

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];

    }
}

@end

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?

Answers


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;

edit: 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);

EDIT 2 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);

Need Your Help

rpm-maven-plugin exclude directories in final rpm

maven rpm-maven-plugin

Is there a way to avoid the full directory path being created in the final rpm package by executing the rpm-maven-plugin?

AngularJS Odd Directive Scope Behavior

javascript angularjs

After describing my setup, my questions are below in bold.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.