我正在开发一个应用程序,并将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
这个项目是在Xcode8和Swift 2.3上进行的。
我不知道为什么我会犯这个错误而无法解决。请调查一下。任何帮助都将不胜感激。
最佳答案
替换以下内容:
旧的:
#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