如何在AppDelegate获取位置,并将lat和long传递给另一个VC并加载webview

问题描述 投票:0回答:2

Edited

我已经设法通过使用this获得当前位置。之后,我试图将lat和long作为字符串传递给另一个加载webview的方法。问题是每次加载此VC时,都不会调用以下方法

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 

并立即跳转到以下方法。

-(void)viewWillAppear:(BOOL)animated

如何获取位置并将其存储为变量,将它们作为字符串传递给另一个方法,附加一个字符串并将其传递给NSURL并加载webview

是拉特和长期保留?如何在整个项目中保留它?

单击其他选项卡控制器并单击此VC时会发生什么。 lat和long是否会再次刷新?

- (void)viewDidLoad
{
    [super viewDidLoad];

    geocoder = [[CLGeocoder alloc] init];
    if (locationManager == nil)
    {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
        locationManager.delegate = self;
        [locationManager requestAlwaysAuthorization];
    }

    [locationManager startUpdatingLocation];

    dtDate = [[NSMutableArray alloc] init];
    self.currentPageIndex = 0;
    self.hasAppearedFlag = NO;

}

//=== It doesn't call a following method first. 
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

    CLLocation *newLocation = [locations lastObject];

    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {

        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            latitude = [NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];
            longitude = [NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
            state = placemark.administrativeArea;
            country = placemark.country;

            NSLog(@"This is the latitude%@", latitude);
            NSLog(@"This is the longitude%@", longitude);

        } else {
            NSLog(@"This is the error debug%@", error.debugDescription);
        }
    }];

    // Turn off the location manager to save power.
    [manager stopUpdatingLocation];

        //*** It will start loading this part below
        [self setupSegmentButtons];
        NSDate *now = [NSDate date];
        NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd/MM/YYYY"];
        NSString *dateString = [dateFormatter stringFromDate:now];

        [self LoadClasses:dateString];
        self.hasAppearedFlag = YES;
}

- (void)locationManager:(CLLocationManager *)manager
   didFailWithError:(NSError *)error
{
    NSLog(@"Cannot find the location.");
}

//=== Load webview

- (void)LoadClasses : (NSString *)sDate{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    sMemCode = [defaults objectForKey:@"txtMemCode"];

    NSLog(@"Load Class This is the memCode:%@", sMemCode);
    NSLog(@"Load Class This is the latitude:%@", latitude);
    NSLog(@"Load Class This is the longitude:%@", longitude);

    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    sURL = appDelegate.gURL;
    sURL = [sURL stringByAppendingString:@"/apps/class.asp?"];
    sURL = [sURL stringByAppendingString:@"memCode="];
    sURL = [sURL stringByAppendingString:sMemCode];
    sURL = [sURL stringByAppendingString:@"&dtpClass="];
    sURL = [sURL stringByAppendingString:sDate];
    sURL = [sURL stringByAppendingString:@"&lat="];
    sURL = [sURL stringByAppendingString:latitude];
    sURL = [sURL stringByAppendingString:@"&long="];
    sURL = [sURL stringByAppendingString:longitude];

    NSLog(@" The sURL to load for the current page : %@ ", sURL);

    NSURL *url = [NSURL URLWithString:sURL];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [webView loadRequest:urlRequest];
    [webView setDelegate:(id<UIWebViewDelegate>)self];

}

Solution

 I have put the `Location Delegates` to `AppDelegates` 

AppDelegates.h

 #import <UIKit/UIKit.h>
 #import<CoreLocation/CoreLocation.h>

 @interface AppDelegate : UIResponder <UIApplicationDelegate>{
CLLocationManager *locationManager;

}

@property (nonatomic, strong) NSString *slatitude;
@property (nonatomic, strong) NSString *slongitude;

@end

AppDelegates.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//--- Get current location ---
if (locationManager == nil)
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    locationManager.delegate = self;

    //-- Pop up authrorization to use current location ---
    [locationManager requestAlwaysAuthorization];
}

[locationManager startUpdatingLocation];
}

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

float latitude = newLocation.coordinate.latitude;
slatitude = [NSString stringWithFormat:@"%f",latitude];
float longitude = newLocation.coordinate.longitude;
slongitude = [NSString stringWithFormat:@"%f", longitude];

NSLog(@"App:This is the latitude%@", slatitude);
NSLog(@"App:This is the longitude%@", slongitude);

}

- (void)locationManager:(CLLocationManager *)manager
   didFailWithError:(NSError *)error
{
    NSLog(@"Cannot find the location.");
}

In any VC,


 appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

latitude = appDelegate.slatitude;
longitude = appDelegate.slongitude;
ios objective-c webview cllocationmanager viewwillappear
2个回答
0
投票

我建议你将你的位置代表代码移动到你的Appdelegate文件,以便它注册并不断更新位置委托方法,

您可以从appdelegate文件中创建两个属性变量,并在调用委托时继续更新它们,或者使用这样的getter或setter方法:

@interface AppDelegate : UIResponder<UILocationDelegate>{
 CLLocationManager * locationManager;
}
@property (nonatomic, strong) NSString *lattitude;
@property (nonatomic, strong) NSString *longitude;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
 if (locationManager == nil)
    {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
        locationManager.delegate = self;
        [locationManager requestAlwaysAuthorization];
    }

    [locationManager startUpdatingLocation];
}

然后在这里实现位置委托方法

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
      if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            self.latitude = [NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];
            self.longitude = [NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
            state = placemark.administrativeArea;
            country = placemark.country;

            NSLog(@"This is the latitude%@", latitude);
            NSLog(@"This is the longitude%@", longitude);

        }

}

- (void)locationManager:(CLLocationManager *)manager
   didFailWithError:(NSError *)error
{
    NSLog(@"Cannot find the location.");
}

迟到,你可以从ViewController访问它

- (void)viewDidAppear:(BOOL)animated {
AppDelegate* appdelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

NSString *lat = appdelegate.lattitude;
NSString *long = appdelegate.longitude;

}

以这种方式即使您切换标签栏或更改控制器,您仍将拥有最新的位置。


0
投票

实际上你在ViewWillAppear中调用你的方法并且你在didUpdateLocations中获取数据..之后ViewWillappear没有保证,更好的是你在收到数据后从didUpdateLocations调用你的方法

© www.soinside.com 2019 - 2024. All rights reserved.