Reputation: 3682
I have implemented -(void)localnotification
in DashboardController.m.Now I want to access localnotification
and implement didUpdateLocations
delegate methods into settingsController.m class. My approach is so far:
DashBoardViewController.h
#import <CoreLocation/CoreLocation.h>
@class AppDelegate;
@interface DashBoardViewController : UIViewController<CLLocationManagerDelegate, UIAlertViewDelegate>{
AppDelegate *appDel;
}
@property(nonatomic,strong) CLLocationManager *locationManager;
@property (nonatomic,retain) CLLocation *current;
-(void)localnotification;
@end
DashBoardViewController.m
#import "DashBoardViewController.h"
#import "AppDelegate.h"
@interface DashBoardViewController ()
@end
@implementation DashBoardViewController
@synthesize current,locationManager;
- (void)viewDidLoad
{
[super viewDidLoad];
appDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
[self localnotification];
}
-(void)localnotification{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startMonitoringSignificantLocationChanges];
}
Now in SettingController.m I am accessing like this:
@implementation SettingsViewController
DashBoardViewController *dash;
- (void)viewDidLoad {
[super viewDidLoad];
appDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
dash=[[DashBoardViewController alloc] init];
[dash localnotification];
NSArray *locations;
[self locationManager:appDel.locationManager didUpdateLocations:locations];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//location 1
CLLocation *locA = [[CLLocation alloc] initWithLatitude:appDel.latVal longitude:appDel.longVal];
//location 2
CLLocation *locB = [[CLLocation alloc] initWithLatitude:appDel.locationManager.location.coordinate.latitude longitude:appDel.locationManager.location.coordinate.longitude ];
appDel.latVal = appDel.locationManager.location.coordinate.latitude; //Latitute of the location
appDel.longVal = appDel.locationManager.location.coordinate.longitude; //Longitude of the location
//Difference between location 1 & location 2
CLLocationDistance dist = [locA distanceFromLocation:locB];
NSLog(@"Difference from Settings: %ld",lroundf(dist));
NSLog(@"Last Location's Object Settings: %@",[locations lastObject]);
}
But [locations lastObject] is returning null in the settingsController class. In view Controller class I don't have to define the locations array but in settings class as I only accessing localnotification method I have declare the locations array otherwise it gives error. What should I pass
[self locationManager:appDel.locationManager didUpdateLocations:?];
Upvotes: 3
Views: 4120
Reputation: 1781
Here is the best way to use Location manager in multiple view controllers:
in AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) CLLocationManager * locationManager;
@property (nonatomic, strong) CLLocation *currentLocation;
in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if([CLLocationManager locationServicesEnabled]){
currentLocation_ = [[CLLocation alloc] init];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
currentLocation_ = newLocation;
}
Now in DashBoardViewController & SettingsViewController
@property (strong, nonatomic) CLLocation *currentLocation;
- (void)viewDidLoad{
[super viewDidLoad];
if([CLLocationManager locationServicesEnabled]){
AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
currentLocation_ = [[CLLocation alloc] initWithLatitude:appDelegate.currentLocation.coordinate.latitude longitude:appDelegate.currentLocation.coordinate.longitude];
}
}
Note: Its for an example, You can improve it as per your need.
Hope it will work for you.
Upvotes: 9