【问题标题】:Google Maps iOS SDK Crash - GMSBackgroundAppExceptionGoogle 地图 iOS SDK 崩溃 - GMSBackgroundAppException
【发布时间】:2026-02-17 20:50:02
【问题描述】:

我已将 Google Maps for iOS SDK 集成到我的应用程序中,但遇到了奇怪的崩溃。

FatalException: GMSBackgroundAppException
Background execution would crash in this scenario

0  CoreFoundation                 0x0000000183abe59c __exceptionPreprocess + 132
1  libobjc.A.dylib                    0x00000001942080e4 objc_exception_throw + 60
2  CoreFoundation                 0x0000000183abe4dc -[NSException initWithCoder:]
3  myApp                               0x00000001004c4a04 -[GMSAsyncInitServices sharedInitWithSync:]
4  myApp                               0x00000001004c48d4 -[GMSAsyncInitServices initSync]
5  myApp                               0x00000001004c5588 +[GMSServices sharedServicesSync]
6  myApp                               0x00000001004ba4a8 -[GMSMapView sharedInitWithServices:camera:]
7  myApp                               0x00000001004b9e44 -[GMSMapView initWithFrame:camera:]
8  myApp                               0x00000001004b9cdc +[GMSMapView mapWithFrame:camera:]
9  myApp                               0x0000000100103c88 -[GoogleMapViewController viewDidLoad] (GoogleMapViewController.m:63)
10 UIKit                                  0x000000018826d184 -[UIViewController loadViewIfRequired] + 692
11 UIKit                                  0x000000018826ce94 -[UIViewController view] + 32
12 myApp                               0x0000000100136cc0 -[BaseViewController viewDidLoad] (BaseViewController.m:93)
13 UIKit                                  0x000000018826d184 -[UIViewController loadViewIfRequired] + 692
14 UIKit                                  0x000000018826ce94 -[UIViewController view] + 32
15 UIKit                                  0x000000018895e90c -[_UIFullscreenPresentationController _setPresentedViewController:] + 76
16 UIKit                                  0x000000018856787c -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 120
17 UIKit                                  0x000000018858291c -[UIViewController _presentViewController:withAnimationController:completion:] + 1972
18 UIKit                                  0x000000018827ace8 +[UIView(Animation) performWithoutAnimation:] + 88
19 UIKit                                  0x0000000188584dd8 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 284
20 UIKit                                  0x000000018835a3d4 -[UIViewController presentViewController:animated:completion:] + 216
21 myApp                               0x00000001000cf1e4 -[FirstViewController viewDidAppear:] (FirstViewController.m:49)
22 UIKit                                  0x000000018828418c -[UIViewController _setViewAppearState:isAnimating:] + 592
23 UIKit                                  0x00000001882ef5fc -[UIViewController _executeAfterAppearanceBlock] + 64
24 UIKit                                  0x00000001882ef564 _applyBlockToCFArrayCopiedToStack + 356
25 UIKit                                  0x0000000188260ea0 _afterCACommitHandler + 572
26 CoreFoundation                 0x0000000183a76a50 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
27 CoreFoundation                 0x0000000183a739dc __CFRunLoopDoObservers + 360
28 CoreFoundation                 0x0000000183a73dbc __CFRunLoopRun + 836
29 CoreFoundation                 0x00000001839a10a4 CFRunLoopRunSpecific + 396
30 GraphicsServices               0x000000018cb3b5a4 GSEventRunModal + 168
31 UIKit                                  0x00000001882d2aa4 UIApplicationMain + 1488
32 myApp                               0x0000000100266818 main (main.m:16)
33 libdyld.dylib                       0x0000000194876a08 start + 4

在 Google 开发网站上报告了一个类似崩溃的错误,但我不能 100% 确定这是 Google 错误,还是 SDK 的一些误用:

https://code.google.com/p/gmaps-api-issues/issues/detail?id=7716

BaseViewController 是我的应用程序的主视图,并在应用程序启动时加载(或在注册/登录后立即加载)。在它的 viewDidLoad() 方法中,它初始化了我的 GoogleMapViewController,它只是一个保存地图的 UIViewController。 GoogleMapVC 的 viewDidLoad() 在这里:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

//We have to stop Google Maps from accessing location when the app goes into the background.
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(appGoingBackground:)
                                             name:UIApplicationDidEnterBackgroundNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(appBecomeActive:)
                                             name:UIApplicationDidBecomeActiveNotification
                                           object:nil];
self.allMarkers = [NSMutableArray array];


double latitude = 40.71448;
double longitude = -74.00598;

GPSManager *manager = [GPSManager sharedInstance];
if (manager.lastKnownLocation) {
    latitude = manager.lastKnownLocation.coordinate.latitude;
    longitude = manager.lastKnownLocation.coordinate.longitude;
}

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitude
                                                        longitude:longitude
                                                             zoom:5];

self.mapView = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
[self.mapView setMinZoom:1 maxZoom:15];
self.mapView.myLocationEnabled = YES;
[self.view addSubview:self.mapView];
[self performSelector:@selector(setPadding) withObject:nil afterDelay:0.1];
}

导致崩溃的行是这一行:

    self.mapView = [GMSMapView mapWithFrame:self.view.bounds camera:camera];

崩溃报告的标题似乎表明这与后台执行有关,但正如我之前所说,这是在应用启动时在 viewDidLoad() 中执行的代码。

我有什么遗漏吗?任何想法/建议将不胜感激。

【问题讨论】:

  • 不确定是什么原因造成的,但更好的做法是将 addObserver 代码放在 viewDidAppear() 中并删除 viewDidDisappear() 中的观察者,这样可以防止您的观察者被多次添加。

标签: ios iphone google-maps


【解决方案1】:

以防万一其他人遇到这种情况,Google 在他们的开发板上发布了回复。

当应用在后台时实例化 Google 地图时会发生此崩溃。他们建议在这种情况下捕获异常,并在应用返回前台时重新实例化。

以下是相关 Google 开发板帖子的链接:

https://code.google.com/p/gmaps-api-issues/issues/detail?id=7716

【讨论】:

  • 能否分享帖子的链接?
【解决方案2】:

该问题已在 1.10.0 版本中修复,请查看发行说明:

https://developers.google.com/maps/documentation/ios/releases

希望对你有帮助

【讨论】: