Try to print location on console

Discussion in 'iPhone/iPad Programming' started by saqibjaan, Mar 14, 2012.

  1. saqibjaan, Mar 14, 2012
    Last edited by a moderator: Mar 24, 2012

    macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #1
    I am developing for jailbreak iPhones. I am reading Beginning iOS 5 book. Currently, I have used a location code from the book and changed it slightly. The change which I have made in the code is that I am trying to print location on console.

    Note: I don't need GUI based solution. And I am building this code in Xcode 4.2 and the iPhone on which I am trying to run this code has iOS 5.0.1 (9A405).

    Here is the code:
    Code:
     
    
     int main(int argc, char *argv[])
        {
           NSLog(@"ENTER in main");
                
           NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
                
           locationAppDelegate *loc = [[locationAppDelegate alloc] init];
                
                
           [pool drain];
                
                
           NSLog(@"EXIT from main");
        }
            
            
            
        -(id) init
        {
           NSLog(@"ENTER in init()");
            	
           [super init];
            	
                
            locationManager = [[CLLocationManager alloc] init];
            locationManager.delegate = self;
                
                
            locationManager.desiredAccuracy = kCLLocationAccuracyBest;
                
            NSTimer *currentTimer = [NSTimer scheduledTimerWithTimeInterval:20.0 target:self
                                                                     selector:@selector(Action) 
                                                                     userInfo:nil
                                                                        repeats:YES];
                	
            NSLog(@"EXIT from init()");	
            	
            return self;
        }
            
            
            
        -(void) Action
        {
           NSLog(@"ENTER in Action");
                
           [locationManager startUpdatingLocation];
                
           NSLog(@"EXIT from Action");
        }
            
            
            
        -(void)locationManager:(CLLocationManager *)manager
               didUpdateToLocation:(CLLocation *)newLocation
                      fromLocation:(CLLocation *)oldLocation
        {
           NSLog(@"ENTER in didUpdateToLocation");
                
                
           NSLog(@"%f",newLocation.coordinate.latitude);
                
           NSLog(@"%f",newLocation.coordinate.longitude);
                
           [locationManager stopUpdatingLocation];
            
                
           NSLog(@"EXIT from didUpdateToLocation");
        }
    
    Now, the only output which is visible on the screen is
    2012-03-22 18:26:23.451 location[437:207] ENTER in main
    2012-03-22 18:26:23.533 location[437:207] ENTER in init()
    2012-03-22 18:26:23.609 location[437:207] EXIT from init()
    2012-03-22 18:26:23.647 location[437:207] EXIT from main
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
    Post the exact title of the book. Post the author's name (or names). What edition is the book?

    Exactly which code from the book are you using? Post the chapter or section.

    Is there a website for the book? What is its URL?

    Are there downloads at the website of the code in the chapters? If not, post the unmodified original code that works.


    First error: If the fopen() statement fails, you then proceed to fprintf to the file anyway. At that point, file is NULL, and the fprintf() won't work. In fact, it will probably crash. Even if it doesn't crash there, your attempt to print an error message will not produce an error message.

    Second error: When if (!file) is true (as it will be when fopen() fails), you not only write to the NULL file, you then continue executing the rest of the function as if the fopen() succeeded. You should return when the file can't be opened, not continue executing as if it worked.


    You should spend a little more time thinking about how to test & debug the code you write. I see no NSLog's or other output that gives progress or debugging messages. I see no indication that you know how to use the debugger to set breakpoints or step through line by line. Either of those would have given you more information about what was happening.

    One of the fundamental aspects of programming is knowing how to find out what's really happening in your program. Learn to do that by applying it to programs that work, i.e. the original sample programs in the book. When you know how to use NSLog and the debugger on working programs, then you can start modifying those programs. If you don't know the first thing about debugging, you will be unable to find the bugs in your modified programs.
     
  3. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    The main bundle is read only. You need to be writing to the Documents directory.

    I would also recommend using the NSFileManager class, rather than fopen(), fprintf(), etc.
     
  4. thread starter macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #4
    Try to write location in a text file

    Book name is "Beginning iOS 5 Development Exploring the iOS SDK"

    By "David Mark, Jack Nutting, Jeff LaMarche"

    Source code of the book can be downloaded at the following link:
    http://www.apress.com/9781430236054

    I am using Chapter 18 example WhereAmI.
     
  5. macrumors 603

    Joined:
    Aug 9, 2009
    #5
    Thanks for posting the additional information.


    This is the original code:
    Code:
    - (void)locationManager:(CLLocationManager *)manager
        didUpdateToLocation:(CLLocation *)newLocation
               fromLocation:(CLLocation *)oldLocation {
        
        if (startingPoint == nil)
            self.startingPoint = newLocation;
        
        NSString *latitudeString = [NSString stringWithFormat:@"%g\u00B0",
                                    newLocation.coordinate.latitude];
        latitudeLabel.text = latitudeString;
        
        NSString *longitudeString = [NSString stringWithFormat:@"%g\u00B0",
                                     newLocation.coordinate.longitude];
        longitudeLabel.text = longitudeString;
        
        NSString *horizontalAccuracyString = [NSString stringWithFormat:@"%gm",
                                              newLocation.horizontalAccuracy];
        horizontalAccuracyLabel.text = horizontalAccuracyString;
        
        NSString *altitudeString = [NSString stringWithFormat:@"%gm",
                                    newLocation.altitude];
        altitudeLabel.text = altitudeString;
        
        NSString *verticalAccuracyString = [NSString stringWithFormat:@"%gm",
                                            newLocation.verticalAccuracy];
        verticalAccuracyLabel.text = verticalAccuracyString;
        
        CLLocationDistance distance = [newLocation
                                       distanceFromLocation:startingPoint];
        NSString *distanceString = [NSString stringWithFormat:@"%gm", distance];
        distanceTraveledLabel.text = distanceString;
    }
    
    It's essentially just the middle part of your posted code, without any stdio file functions or time functions.


    Did you fix the two errors I previously pointed out?

    Do you understand why the file you asked to open for appending might not exist, or might not be writable, and why that would cause fopen() to fail? If not, then you need to study Unix file permissions and take a closer look at what files and directories are accessible to jail-break programs. If you don't have a good reference for jail-break info, you will need to find one.
     
  6. thread starter macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #6
    Try to print location on console

    I have not been able to write location in a text file. But now, I am trying to print location on console. But I have not been able to do so as well. The output which I am getting on console is mentioned in the 1st post.
     
  7. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Looks like your (poorly-named) Action method is never getting called. I would look into why.
     
  8. thread starter macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #8
    Try to print location on console

    Action method gets called but the delegate method 'didUpdateToLocation' method never gets called.

    I am stuck.
     
  9. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    Your whole app runs and quits in 2 tenths of a second. A normal iOS app has a runloop and continues to run allowing the system callbacks to occur. Without a runloop I don't think you'll get anywhere.
     
  10. saqibjaan, Mar 27, 2012
    Last edited by a moderator: Mar 27, 2012

    thread starter macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #10
    Try to print iPhone location in Console in Xcode 4.2

    I have updated the init() method and add NSRunLoop.


    Code:
    -(id) init
    {
    	NSLog(@"ENTER in init()");
    	
    	[super init];
    	
            locationManager = [[CLLocationManager alloc] init];
    	locationManager.delegate = self;
        
        
            NSTimer *currentTimer = [NSTimer    
                                                    scheduledTimerWithTimeInterval:20.0
                                                             target:self
                                                             selector:@selector(Action) 
                                                             userInfo:nil
                                                             repeats:YES];
        
            NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    	[runLoop addTimer:currentTimer forMode:NSDefaultRunLoopMode];
    	[runLoop run];
        
        	
    	NSLog(@"EXIT from init()");	
    	
    	return self;
    }
    Now the output is:

    2012-03-27 12:06:28.135 location[778:707] ENTER in main
    2012-03-27 12:06:28.143 location[778:707] ENTER in init()
    2012-03-27 12:06:48.159 location[778:707] ENTER in Action
    2012-03-27 12:06:48.166 location[778:707] EXIT from Action
    2012-03-27 12:07:08.163 location[778:707] ENTER in Action
    2012-03-27 12:07:08.169 location[778:707] EXIT from Action

    And so on ...
     
  11. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    And? You have described what it does, but not what the problem is now.
     
  12. saqibjaan, Mar 27, 2012
    Last edited by a moderator: Mar 27, 2012

    thread starter macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #12
    Try to print location in console

    Problem is that method

    Code:
     -(void)locationManager:(CLLocationManager *)manager
               didUpdateToLocation:(CLLocation *)newLocation
                      fromLocation:(CLLocation *)oldLocation
    never gets called.

    Have you understand now dejo?
     
  13. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    Kinda. Just don't understand why you are putting all this code in your main.m file (which I normally never touch) and not in your application-delegate (which is where I might put it, depending on the situation) or your view-controller (which is where the "Beginning iOS 5 Development" book puts it, as might I).
     
  14. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #14
    Smart thing to do would be to start with the code in the book, which works. Then you can modify it to do what you want.

    Starting with nothing and copying bits of code from the working app and then coming on here asking why it doesn't work isn't a good way to do this.
     
  15. thread starter macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #15
    Try to print location in console

    The code in the book is a GUI based program and it works perfectly on device.

    I changed that code into non GUI based program that's why there is no viewController in my program.

    In the GUI based program, CLLocationManager class initializes in viewDidLoad.
    In my code, CLLocationManager class initializes in init.

    I think it should work.
     
  16. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #16
    What kind of iOS app doesn't have a GUI?
     
  17. thread starter macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #17
    Try to print location on console

    Dejo, I have mentioned that I am developing for jailbreak iPhones.
    There is an Empty Application project type in Xcode 4.2 which you can use to built non GUI based applications.

    I think you have got my point.
     
  18. macrumors 603

    Joined:
    Aug 9, 2009
    #18
    Please explain your goal. Exactly what are you trying to accomplish?

    Your first posted code was trying to append text to a file. It failed because you weren't opening the file correctly, and weren't handling the error correctly. It didn't fail because the fprintf's were wrong (though they could be, I didn't look closely). It failed because you didn't take file-access permissions into account, and because your error-handling for unopened files was defective.

    Your second posted code is trying to NSLog the coordinates of a location. The only similarity to your first posted code is the %f format and the use of location services. Instead of fprintf you're now using NSLog. Instead of a file opened with fopen you're now using the log stream. And instead of modifying a complete working program, you're starting from scratch. There are almost no similarities between these two cases, other than the use of the %f format and location services.

    So exactly what are you trying to accomplish? If you're trying to learn something, exactly what are you trying to learn? If it's about how to append text to a file, why do you think NSLog will help you? If it's about how to write a program from scratch that uses location services, do you really have the necessary foundation skills to do that at this time?

    We can't help you if we don't know your goal. We need to know what you're trying to accomplish. Since there's very little overlap between your current program and the original fopen and fprintf one, it's difficult for any of us to know how to help you. No one can read your mind, and I certainly can't figure out your intended goal from the two very different programs you posted.
     
  19. saqibjaan, Mar 28, 2012
    Last edited: Mar 28, 2012

    thread starter macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #19
    First of all thanks chown33 for your comments and suggestions.

    Forget about appending in text file.

    I just want to print iPhone location (Latitude & Longitude) in console.

    Development environment is Xcode 4.2 and iPhone device has iOS 5.0.1.

    For all of you, here is my code again through which I want to accomplish this.



    Code:
    int main(int argc, char *argv[])
    {
        NSLog(@"ENTER in main");
        
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        
        locationAppDelegate *loc = [[locationAppDelegate alloc] init];
        
        [pool drain];
        
        NSLog(@"EXIT from main");
    }
    
    
    
    
    -(id) init
    {
    	NSLog(@"ENTER in init()");
    	
    	[super init];
    	
            locationManager = [[CLLocationManager alloc] init];
    	locationManager.delegate = self;
        
            NSTimer *currentTimer = [NSTimer 
                                                     scheduledTimerWithTimeInterval:20.0
                                                     target:self
                                                     selector:@selector(Action)
                                                     userInfo:nil
                                                     repeats:YES];
        
            NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    	[runLoop addTimer:currentTimer forMode:NSDefaultRunLoopMode];
    	[runLoop run];
        
            NSLog(@"EXIT from init()");	
    	
    	return self;
    }
    
    
    
    -(void) Action
    {
        NSLog(@"ENTER in Action");
        
        [locationManager startUpdatingLocation];
        
        NSLog(@"EXIT from Action");
    }
    
    
    
    
    
    -(void)locationManager:(CLLocationManager *)manager
       didUpdateToLocation:(CLLocation *)newLocation
              fromLocation:(CLLocation *)oldLocation
    {
        NSLog(@"ENTER in didUpdateToLocation");
        
        
        NSLog(@"%f",newLocation.coordinate.latitude);
        
        NSLog(@"%f",newLocation.coordinate.longitude);
        
        [locationManager stopUpdatingLocation];
    
            
        NSLog(@"EXIT from didUpdateToLocation");
    }

    Here is the output which I am getting:

    ENTER in main
    ENTER in init
    ENTER in Action
    EXIT from Action
    ENTER in Action
    EXIT from Action
    ENTER in Action
    EXIT from Action

    And so on ...
     
  20. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #20
    Ah, I must've skipped over that detail. I'm afraid I can't help you anymore then. Good luck, though.

    Oh, and please be courteous to your fellow members and see this Sticky:
    Posting code, please use the
    Code:
     tags[/URL]
     

Share This Page