PDA

View Full Version : Try to print location on console




saqibjaan
Mar 14, 2012, 07:27 AM
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:


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



chown33
Mar 14, 2012, 10:55 AM
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 writing the location in a text file. But the program crashes. Although the same program without any modification is working perfectly.


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.


- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
FILE *file = fopen("/usr/locations.txt", "a");

if (!file)
{
fprintf(file, "Error opening file");
}

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

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.

dejo
Mar 14, 2012, 12:29 PM
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.

saqibjaan
Mar 19, 2012, 06:35 AM
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.

chown33
Mar 19, 2012, 10:15 AM
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.
Thanks for posting the additional information.


This is the original 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.

saqibjaan
Mar 23, 2012, 10:55 PM
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.

dejo
Mar 24, 2012, 08:01 AM
Looks like your (poorly-named) Action method is never getting called. I would look into why.

saqibjaan
Mar 26, 2012, 12:49 PM
Action method gets called but the delegate method 'didUpdateToLocation' method never gets called.

I am stuck.

PhoneyDeveloper
Mar 26, 2012, 06:09 PM
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.

saqibjaan
Mar 27, 2012, 02:10 AM
I have updated the init() method and add NSRunLoop.


-(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 ...

dejo
Mar 27, 2012, 08:53 AM
And so on ...

And? You have described what it does, but not what the problem is now.

saqibjaan
Mar 27, 2012, 02:38 PM
Problem is that method

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

never gets called.

Have you understand now dejo?

dejo
Mar 27, 2012, 03:03 PM
Have you understand now dejo?

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).

PhoneyDeveloper
Mar 27, 2012, 08:09 PM
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.

saqibjaan
Mar 27, 2012, 09:52 PM
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.

dejo
Mar 27, 2012, 11:23 PM
What kind of iOS app doesn't have a GUI?

saqibjaan
Mar 28, 2012, 12:47 AM
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.

chown33
Mar 28, 2012, 01:00 AM
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.

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.

saqibjaan
Mar 28, 2012, 05:52 AM
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.



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 ...

dejo
Mar 28, 2012, 09:06 AM
Dejo, I have mentioned that I am developing for jailbreak iPhones.

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 (http://forums.macrumors.com/showthread.php?t=747660)