Working in Simulator but not on device

Discussion in 'iOS Programming' started by saqibjaan, Feb 1, 2012.

  1. saqibjaan, Feb 1, 2012
    Last edited by a moderator: Feb 2, 2012

    macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #1
    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.

    Code:
    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
     
  2. macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #2
    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.
     
  3. thread starter macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #3
    Working in Simulator but not on device

    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.
     
  4. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    Do other apps, like maps, manage to get your GPS coordinates?
     
  5. macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #5
    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.
     
  6. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #6
    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.
     
  7. thread starter macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #7
    Working in Simulator but not on device

    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
     
  8. macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #8
    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!
     
  9. thread starter macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #9
    Working in Simulator but not on Device

    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.
     
  10. saqibjaan, Feb 2, 2012
    Last edited by a moderator: Feb 6, 2012

    thread starter macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #10
    Still no success

    I have updated the code. But still no success.

    Here is my modified code:

    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
     
  11. macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #11
    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.
     
  12. thread starter macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #12
    for dantastic

    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.
     
  13. macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #13
    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.
     
  14. thread starter macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #14
    Still no success

    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.
     
  15. macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #15
    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...
     
  16. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #16
    Well, this forum doesn't really deal with jailbroken development, so you might want to consider seeking help from forums that do.
     
  17. thread starter macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #17
    Still no success

    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
     

Share This Page