Brennan Casey
Brennan Casey

Reputation: 897

Getting the Users location with parse

I am trying to make my code that on app loading it updates my parse database with the users current time and location. Here is my current code:


- (void)viewDidLoad {
    [super viewDidLoad];
   PFObject *testObject = [PFObject objectWithClassName:@"TestObject"];
   testObject[@"foo"] = @"bar";
//    PFGeoPoint *point = [PFGeoPoint geoPointWithLatitude:40.0 longitude:-30.0];
//    testObject[@"location"] = point;
    [PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
        if (!error) {
            testObject[@"location"] = geoPoint;
        }
    }];

    [testObject saveInBackground];

My database updates correctly when I give it a preset lat and long to use but it doesnt work when I use parses code to fetch the users geoPoint. Is this because I am doing this wrong or is it because it will not work when im using the iPhone simulator on my computer. Thanks.

Upvotes: 1

Views: 771

Answers (1)

Ashish Kakkad
Ashish Kakkad

Reputation: 23882

Example code is given in the parse blog :

[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
    if (!error) {
        NSLog(@"User is currently at %f, %f", geoPoint.latitude, geoPoint.longitude);

        [[PFUser currentUser] setObject:geoPoint forKey:@"currentLocation"];
        [[PFUser currentUser] saveInBackground];
    }
}];

As per my opinion. You have to save the location from the completion block. you have done it from the outside of the block.

Also you can take a look of iOS Guide for Geo Location

Hope it helps.

Upvotes: 1

Related Questions