i am taking user's location with this code:
@implementation TabBar
CLLocationManager *locationManager;
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
UsersCurrentLongitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
UsersCurrentLatitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
NSLog(@"Latitude:%@,Longitude:%@",UsersCurrentLatitude,UsersCurrentLongitude);
}
and in nslog screen it is showing location successfully. but when i want to use that locations in another class which imported my location class (in this case it is TabBar), location turns null. what can i do to solve this issue? Edited:
After I try two different method for my issue i am still getting null at NsLog:
First Method:
At AppDelegate.h:
@property(nonatomic,retain)NSString *latitude;
@property(nonatomic,retain)NSString
At AppDelegate.mlongitude;
@synthesize latitude;
@synthesize longitude;
At TabBar.m
#import "AppDelegate.h"
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
[(AppDelegate *)[[UIApplication sharedApplication] delegate] setLatitude:[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] setLongitude:[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]];
}
}
At my SecondView.m
#import "AppDelegate.h"
(void)viewDidLoad
{
[super viewDidLoad];
NSString *UsersCurrentLatitude = [(AppDelegate *)[[UIApplication sharedApplication] delegate] latitude];
NSString *UsersCurrentLongitude = [(AppDelegate *)[[UIApplication sharedApplication] delegate] longitude];
NSLog(@"latitude:%@,longitude%@",UsersCurrentLatitude,UsersCurrentLongitude);
}
Second Method (I found a tutorial in youtube and arrange that method for my issue, so it can be meaningless code):
I made a NSObject Class which is named SingletonClass At SingletonClass.h
#import <CoreLocation/CoreLocation.h>
@interface SingletonClass : NSObject <CLLocationManagerDelegate>
@property (nonatomic, strong) NSNumber *UsersCurrentLatitude;
@property (nonatomic, strong) NSNumber *UsersCurrentLongitude;
+(SingletonClass *) Lokasyon;
@end
At SingletonClass.m
#import "SingletonClass.h"
@implementation SingletonClass {
CLLocationManager *locationManager;
}
@synthesize UsersCurrentLongitude;
@synthesize UsersCurrentLatitude;
+(SingletonClass *)Lokasyon {
static SingletonClass *Lokasyon = nil;
if (!Lokasyon) {
Lokasyon = [[super allocWithZone:nil] init];
}
return Lokasyon;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
UsersCurrentLongitude = [NSNumber numberWithFloat:currentLocation.coordinate.longitude];
UsersCurrentLatitude = [NSNumber numberWithFloat:currentLocation.coordinate.latitude];
}
}
@end
At my SeconViewController
#import "SingletonClass.h"
- (void)viewDidLoad
{
[super viewDidLoad];
NSNumber *UsersCurrentLongitude1 = [[SingletonClass Lokasyon]UsersCurrentLongitude];
NSLog(@"%@",UsersCurrentLongitude1);
}
Declare them in the Appdelegate.h
like-
@property(nonatomic,retain)NSString *latitude;
@property(nonatomic,retain)NSString *longitude;
make sure u, synthesize latitude
and longitude
properties in AppDelegate.m
class.
Set them in TabBar.m
where u get the location in CLLocationManager
's didUpdateToLocation:
if (currentLocation != nil) {
[(MyAppDelegate *)[[UIApplication sharedApplication] delegate] setLatitude:[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]];
[(MyAppDelegate *)[[UIApplication sharedApplication] delegate] setLongitude:[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]];
//UsersCurrentLatitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
//NSLog(@"Latitude:%@,Longitude:%@",UsersCurrentLatitude,UsersCurrentLongitude);
}
Access in other class(es) like-
NSString aLatitude = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] latitude];
NSString aLongitude = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] longitude];
PS: there are many other ways to do what u need, like- Having a Singleton
class or Saving in the iOS app File system
i.e. in App Sandbox
..