ios - Google Maps AutocompleteResultsViewController 在 iOS 10 中崩溃

标签 ios swift xcode google-maps google-maps-sdk-ios

我正在开发一个应用程序并将 GoogleMaps 和 GooglePlaces SDK 集成到其中。当我在基于 iOS 10 的设备上打开它时,单击搜索栏会使应用程序崩溃。我还添加了 GMSAutocompleteResultsViewController。我附上代码

    var mapView: GMSMapView?

    var resultsViewController: GMSAutocompleteResultsViewController?
    var searchController: UISearchController?        

    override func viewDidLoad() {
        super.viewDidLoad()

        mapView = GMSMapView()
        mapView.frame = CGRectMake(0, 0, width, height)
        mapView.mapType = kGMSTypeNormal
        self.view.addSubview(mapView)

        mapView.delegate = self

        resultsViewController = GMSAutocompleteResultsViewController()
        resultsViewController?.delegate = self

        searchController = UISearchController(searchResultsController: resultsViewController)
        searchController?.searchResultsUpdater = resultsViewController

        searchController!.view.frame = CGRectMake(0, 0, width, height)

        let subView = UIView(frame: CGRectMake(0, 0, width, 45.0))

        subView.addSubview((searchController?.searchBar)!)
        self.view.addSubview(subView)

        searchController?.hidesNavigationBarDuringPresentation = false
        searchController?.modalPresentationStyle = UIModalPresentationStyle.Popover

        self.definesPresentationContext = true

        searchController?.searchBar.delegate = self
    }

    extension ViewController: GMSAutocompleteResultsViewControllerDelegate {
        func resultsController(resultsController: GMSAutocompleteResultsViewController, didAutocompleteWithPlace place: GMSPlace) {
            searchController?.active = false

            mapView.animateToLocation(place.coordinate)
        }

        func resultsController(resultsController: GMSAutocompleteResultsViewController, didFailAutocompleteWithError error: NSError){
            print("Error: ", error.description)
        }

        func didRequestAutocompletePredictions(viewController: GMSAutocompleteViewController) {
            UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        }

        func didUpdateAutocompletePredictions(viewController: GMSAutocompleteViewController) {
            UIApplication.sharedApplication().networkActivityIndicatorVisible = false
        }
    }

    extension ViewController: UISearchBarDelegate {
        func searchBarSearchButtonClicked(searchBar: UISearchBar) {
            searchBar.resignFirstResponder()
        }

        func searchBarCancelButtonClicked(searchBar: UISearchBar) {
            searchBar.resignFirstResponder()
            searchController?.active = false
        }

        func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
            let doneBtn = self.navigationItem.rightBarButtonItem
            doneBtn?.enabled = false
        }

        func searchBarTextDidEndEditing(searchBar: UISearchBar) {
            let doneBtn = self.navigationItem.rightBarButtonItem
            doneBtn?.enabled = true
        }
}

每当我点击搜索栏时,应用程序都会在输入任何字符时崩溃。我没有看到任何结果,甚至无法输入一个单词。我得到的错误是

* thread #1: tid = 0x43622b, 0x1ca0f050 libobjc.A.dylib`objc_exception_throw, queue = 'com.apple.main-thread', stop reason = breakpoint 1.2
* thread #1: tid = 0x4382b9, 0x1ca0f050 libobjc.A.dylib`objc_exception_throw, queue = 'com.apple.main-thread', stop reason = breakpoint 1.2
* frame #0: 0x1ca0f050 libobjc.A.dylib`objc_exception_throw
frame #1: 0x1d7a9d4c CoreFoundation`+[NSException raise:format:] + 108
frame #2: 0x00933772 AppName`+[GMSPlacesClient sharedClient] + 122
frame #3: 0x0092f702 AppName`-[GMSAutocompleteFetcher requestAutocompleteResultsWithText:] + 202
frame #4: 0x0092f55a AppName`-[GMSAutocompleteFetcher sendOrDelayRequest:] + 82
frame #5: 0x0092f4fa AppName`-[GMSAutocompleteFetcher sourceTextHasChanged:] + 82
frame #6: 0x0093ad6a AppName`-[GMSAutocompleteTableDataSource sourceTextHasChanged:] + 234
frame #7: 0x00943d7e AppName`-[GMSAutocompleteResultsViewController updateSearchResultsForSearchController:] + 202
frame #8: 0x2321d736 UIKit`-[UISearchController _searchBar:textDidChange:] + 84
frame #9: 0x229e202a UIKit`-[UISearchBar(UISearchBarStatic) _searchFieldEditingChanged] + 268
frame #10: 0x22823b5c UIKit`-[UIApplication sendAction:to:from:forEvent:] + 76
frame #11: 0x22823aea UIKit`-[UIControl sendAction:to:forEvent:] + 62
frame #12: 0x2280dc86 UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 470
frame #13: 0x229e1da8 UIKit`-[UITextField fieldEditorDidChange:] + 218
frame #14: 0x2282dd02 UIKit`-[UITextInputController _sendDelegateChangeNotificationsForText:selection:] + 122
frame #15: 0x229b2c7a UIKit`-[UITextInputController _insertText:fromKeyboard:] + 822
frame #16: 0x229b2008 UIKit`-[UITextInputController insertText:] + 406
frame #17: 0x22b3d142 UIKit`-[UIFieldEditor insertFilteredText:] + 540
frame #18: 0x231da3f0 UIKit`-[UITextField insertFilteredText:] + 96
frame #19: 0x229b1d98 UIKit`-[UIKeyboardImpl insertText:] + 136
frame #20: 0x22bf6e94 UIKit`-[UIKeyboardImpl performKeyboardOutput:] + 524
frame #21: 0x22bf6ab6 UIKit`__55-[UIKeyboardImpl handleKeyboardInput:executionContext:]_block_invoke_2 + 334
frame #22: 0x2281831a UIKit`-[UIKeyboardTaskQueue continueExecutionOnMainThread] + 400
frame #23: 0x1e0cb3da Foundation`__NSThreadPerformPerform + 386
frame #24: 0x1d765c8a CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 12
frame #25: 0x1d765794 CoreFoundation`__CFRunLoopDoSources0 + 438
frame #26: 0x1d763a6a CoreFoundation`__CFRunLoopRun + 762
frame #27: 0x1d6b3072 CoreFoundation`CFRunLoopRunSpecific + 486
frame #28: 0x1d6b2e80 CoreFoundation`CFRunLoopRunInMode + 104
frame #29: 0x1ee5bbfc GraphicsServices`GSEventRunModal + 156
frame #30: 0x22859cce UIKit`-[UIApplication _run] + 574
frame #31: 0x22854400 UIKit`UIApplicationMain + 150
frame #32: 0x00232110 AppName`main + 172 at AppDelegate.swift:28
frame #33: 0x1ce7e50a libdyld.dylib`start + 2

该项目在带有 Swift 2.3 的 Xcode 8 上。

我不知道为什么会收到此错误并且无法解决。请调查一下。任何帮助将不胜感激。

最佳答案

替换以下内容:

旧的:

#import <GoogleMaps/GooglePlaces.h>
[GMSServices provideAPIKey:GoogleAPIKey]

更新的工作代码:

@import GooglePlaces;
[GMSPlacesClient provideAPIKey:GoogleAPIKey];

最新的 SDK 指向 GooglePlaces/GooglePlaces.h,而不是 GoogleMaps/GooglePlaces.h 检查此链接: https://developers.google.com/places/migrate-to-v2

关于ios - Google Maps AutocompleteResultsViewController 在 iOS 10 中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40058289/

相关文章:

iphone - 当我分析我的应用程序时,这些问题意味着什么?

ios - 使用swift对日期进行排序

iphone - 图像在 iOS 中用手指在屏幕上移动

ios - 在 Swift 中对 UITabBarController 进行 segue 后如何摆脱 UINavigationController?

ios - DateFormatter 不返回 "HH:mm:ss"的日期

ios - 如何在按下回车键时 swift 隐藏键盘?

ios - 在一次请求中上传多张图片

ios - 使用约束将按钮移向目标,没有反应?

ios - Ipad 上的 React Native - 错误 - 无法连接到开发服务器

ios - 当滚动到屏幕外时,UITableView 部分无法正确绘制/EXC_Bad_Access