PDA

View Full Version : Working in Simulator but not on device




saqibjaan
Feb 2, 2012, 01:23 AM
I am trying to run this program on iPhone 3GS with iOS 5. Program works fine on simulator but doesn't work on device. No error but locationManager:didUpdateToLocation:fromLocation delegate method doesn't get called when run on the device.

I just want to print location after every 30 seconds.


int main(int argc, char *argv[])
{
iLocation *loc = [[iLocation alloc] init];


NSDate *now = [[NSDate alloc] init];

NSTimer *timer = [[NSTimer alloc] initWithFireDate:now
interval:30
target:loc
selector:@selector(start)
userInfo:nil
repeats:YES];


NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
[runLoop run];
}


@interface iLocation : NSObject <CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) NSTimer *timer;


-(void) start;

@end


@implementation iLocation

@synthesize locationManager;
@synthesize timer;



-(void) start
{
NSLog(@"Enter in start");

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;

locationManager.desiredAccuracy = kCLLocationAccuracyBest;

[locationManager startUpdatingLocation];

NSLog(@"Exit from start");

}



#pragma mark -
#pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Enter in didUpdateToLocation");

/*if (startingPoint == nil)
self.startingPoint = newLocation;*/

NSString *latitudeString = [NSString stringWithFormat:@"%g\u00B0",
newLocation.coordinate.latitude];
//latitudeLabel.text = latitudeString;
NSLog(@"Latitude = %@", latitudeString);


NSString *longitudeString = [NSString stringWithFormat:@"%g\u00B0",
newLocation.coordinate.longitude];
//longitudeLabel.text = longitudeString;
NSLog(@"Longitude = %@", longitudeString);


NSString *horizontalAccuracyString = [NSString stringWithFormat:@"%gm",
newLocation.horizontalAccuracy];
//horizontalAccuracyLabel.text = horizontalAccuracyString;
NSLog(@"Horizontal Accuracy = %@", horizontalAccuracyString);


NSString *altitudeString = [NSString stringWithFormat:@"%gm",
newLocation.altitude];
//altitudeLabel.text = altitudeString;
NSLog(@"Altitude = %@", altitudeString);


NSString *verticalAccuracyString = [NSString stringWithFormat:@"%gm",
newLocation.verticalAccuracy];
//verticalAccuracyLabel.text = verticalAccuracyString;
NSLog(@"Vertical Accuracy = %@", verticalAccuracyString);



[locationManager stopUpdatingLocation];


NSLog(@"Exit from didUpdateToLocation");
}



- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Enter in didFailWithError");

NSString *errorType = (error.code == kCLErrorDenied) ?
@"Access Denied" : @"Unknown Error";

NSLog(@"%@", errorType);

NSLog(@"Exit from didFailWithError");
}

@end



dantastic
Feb 2, 2012, 02:31 AM
How long are you trying this for? Inside?

Where you are sitting, have you had success previously getting coordinates quickly?

Thing is, the GPS is pretty rubbish, if you are sitting inside by a computer you may not be able to communicate with the satellites. It also takes a bit of time to get coordinates, sometimes more than 30 sec.

So load your app and go outside for a bit. Create a viewcontroller you can post the logging to so you can see what is going on.

saqibjaan
Feb 2, 2012, 03:27 AM
I am trying to run this for the last 2 weeks. Yes, I am sitting inside with Wi-Fi enabled. But this code works perfectly fine in previous iOS versions. So, I don't think it is a problem of GPS.

By the way, thanks for your reply.

ArtOfWarfare
Feb 2, 2012, 05:58 AM
I am trying to run this for the last 2 weeks. Yes, I am sitting inside with Wi-Fi enabled. But this code works perfectly fine in previous iOS versions. So, I don't think it is a problem of GPS.

By the way, thanks for your reply.

Do other apps, like maps, manage to get your GPS coordinates?

dantastic
Feb 2, 2012, 06:08 AM
I'm using CLLocationManager in several apps and none have broken since moving to iOS5 so you'll need to do a bit more convincing before we attribute this to being an iOS5 issue.

1) This very piece of code was working on the same device running iOS4.x?
2) Under Settings > Location Services. Location services are ON, the app is ON?
3) Delete the app from your device & install again - do you get the popup asking if OK to use user location?
4) You are getting the exit from start logging?
5) After this, do the navigation arrow appear in the top bar?

You can't really compare device to simulator here as the simulator doesn't have any GPS hardware xcode is sending coordinates to the simulator.

jnoxx
Feb 2, 2012, 06:50 AM
you assign a new alloc inited object every 30 seconds, wow, that's not really top notch code right there.
And start calling startupdating all the time on a new object isn't the best way too.
Try reusing some of the code.

saqibjaan
Feb 2, 2012, 07:49 AM
dantastic, can you share some piece of your code if possible.

Yes, same code works with previous versions of iOS.


Remember I am developing this program for jailbreak iPhones.


Thanks

dantastic
Feb 2, 2012, 08:37 AM
dantastic, can you share some piece of your code if possible.

Yes, same code works with previous versions of iOS.


Remember I am developing this program for jailbreak iPhones.


Thanks

Code isn't necessary. Obviously as jnoxx pointed out you're allocating a new object every 30 sec but the code should still work.

Can you just humor me and try the previous points?

btw, why are you doing this the way you do? Generally you don't add anything at all to main.m - at most you would do this from the app delegate. Looking at your code you're not creating an application in the traditional sense at all, are you? This is purely something to track user location?

EDIT:
So I was just thinking - if you're not creating a user interface. CLLocation Manager can't ask the user for permission to run. So you don't have any permission to use the user location. therefore - no location for you!

saqibjaan
Feb 2, 2012, 01:21 PM
Yes, it's not an application but a part of an application. My ultimate goal is to run this code as a daemon.

As far as your suggestions are concerned about my code, I am trying different modifications in my code.


Thanks for all the helpful comments.

saqibjaan
Feb 3, 2012, 01:55 AM
I have updated the code. But still no success.

Here is my modified code:

int main(int argc, char *argv[])
{
@autoreleasepool
{
iLocation *loc = [[iLocation alloc] init];

[loc start];

NSLog(@"In main");

}

return 0;
}

////////////////////////////////////////////////////////////////////////

@interface iLocation : NSObject <CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;


-(void) start;
-(void) startAgain;

@end


/////////////////////////////////////////////////////////////////////



#import "BIDAppDelegate.h"

@implementation iLocation

@synthesize locationManager;

-(void) start
{
NSLog(@"Enter in start");

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;

locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;

if([CLLocationManager locationServicesEnabled])
{
NSLog(@"Location Services are enabled");

[locationManager startUpdatingLocation];
}
else
NSLog(@"Location Services are disabled");


NSLog(@"Exit from start");

}


-(void) startAgain
{
[locationManager startUpdatingLocation];

}



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

NSLog(@"Enter in didUpdateToLocation");


NSString *latitudeString = [NSString stringWithFormat:@"%g\u00B0",
newLocation.coordinate.latitude];

NSLog(@"Latitude = %@", latitudeString);


NSString *longitudeString = [NSString stringWithFormat:@"%g\u00B0",
newLocation.coordinate.longitude];

NSLog(@"Longitude = %@", longitudeString);


NSString *horizontalAccuracyString = [NSString stringWithFormat:@"%gm",
newLocation.horizontalAccuracy];

NSLog(@"Horizontal Accuracy = %@", horizontalAccuracyString);


NSString *altitudeString = [NSString stringWithFormat:@"%gm",
newLocation.altitude];

NSLog(@"Altitude = %@", altitudeString);


NSString *verticalAccuracyString = [NSString stringWithFormat:@"%gm",
newLocation.verticalAccuracy];

NSLog(@"Vertical Accuracy = %@", verticalAccuracyString);


NSTimer *timer = [NSTimer timerWithTimeInterval:20.0
target:self
selector:@selector(startAgain)
userInfo:nil
repeats:YES];

[[NSRunLoop currentRunLoop] addTimer:timer
forMode:NSDefaultRunLoopMode];


NSLog(@"Exit from didUpdateToLocation");
}



- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Enter in didFailWithError");

NSString *errorType = (error.code == kCLErrorDenied) ?
@"Access Denied" : @"Unknown Error";

NSLog(@"%@", errorType);

NSLog(@"Exit from didFailWithError");
}

@end

dantastic
Feb 3, 2012, 02:32 AM
You still haven't answered my questions and you still haven't addressed the point where I think it is failing. You dump a load of code here just saying it's not working without any information what so ever as to what is happening or what you get logged to console.

You're clearly new to this, you have been working on this and asking the same questions over and over on several forums for a month now. May I suggest you drop the whole "daemon" thing and just make a regular UI based application the way Apple intended.

saqibjaan
Feb 3, 2012, 03:51 AM
Here are the answers dantastic:

1. This code (with some modifications) is running on iPhone 3GS with iOS 4.
2. Location services are ON.
3. Yes popup will come which asks me to allow Current User Location.

Location code works great in GUI based app. I know.
I want that code to work in non GUI app.

Hope you will get your answers.

dantastic
Feb 3, 2012, 04:38 AM
Here are the answers dantastic:

1. This code (with some modifications) is running on iPhone 3GS with iOS 4.
2. Location services are ON.
3. Yes popup will come which asks me to allow Current User Location.

Location code works great in GUI based app. I know.
I want that code to work in non GUI app.

Hope you will get your answers.

And what is getting logged to console? looking at the latest revision of your code I'd say sweet f-a. The app is returning immediately. What's your actual code? You are importing a BIDAppDelegate.h but it's not used anywhere.

To be honest this all looks like an app that you want to run hidden, without the user's knowledge to collect data on them. Quite frankly I find this unethical and I'm not too keen on supporting it.

saqibjaan
Feb 3, 2012, 07:28 AM
Current flow of the program is that control goes from main to start() function and then returns back to main.

Nothing more than that.

BIDAppDelegate is the name of file. It's not a delegate in the true sense.


As far as you are saying that I want to run this code hidden. Then yes you are right. Because I am jailbreak iPhone developer.

dantastic
Feb 3, 2012, 07:38 AM
Current flow of the program is that control goes from main to start() function and then returns back to main.

Nothing more than that.

BIDAppDelegate is the name of file. It's not a delegate in the true sense.


As far as you are saying that I want to run this code hidden. Then yes you are right. Because I am jailbreak iPhone developer.

So what is happening at the moment is that
1) enter main.
2) create an instance of ilocation
3) call start in instance of ilocation.
4) start allocates & sets up an instance of CLLocationManager. starts updating location.
5) start is complete immediately as the locationManager instance is threaded off to work in the background.
4) exit main - return 0 - app is no longer running.

So your app is returning 0 in about one sec. It never sticks around to wait for any delegate callbacks...

dejo
Feb 3, 2012, 08:58 AM
As far as you are saying that I want to run this code hidden. Then yes you are right. Because I am jailbreak iPhone developer.

Well, this forum doesn't really deal with jailbroken development, so you might want to consider seeking help from forums that do.

saqibjaan
Feb 6, 2012, 04:50 AM
Dantastic thanks for your help and valuable comments.
I have read your comments and think the best is to share my output.
So, here is my output:

Enter in start
2012-02-06 15:47:52.346 WhereAmI[743:207] Location Services are enabled
2012-02-06 15:47:52.364 WhereAmI[743:207] Exit from start
2012-02-06 15:47:52.368 WhereAmI[743:207] In main
2012-02-06 15:47:52.374 WhereAmI[743:207] Still in main
Program ended with exit code: 0