2017-10-01 3 views
0

firebase에서 데이터를 가져와 거리별로 구성하고 사용자가 앱을 중단하지 않기를 바라는 tableview가 있기 때문에 위치 서비스가 활성화 된 경우 코드 실행에 도움이 필요합니다. . 또한 위치 서비스가 활성화 된 경우에만 코드를 실행하려는 UIRefreshControl도 있습니다. 현재 내 설정은 내가 원하는 것을하지 않습니다. 내 전체 ViewDidLoad뿐만 아니라 내 UIRefreshControl 게시 오전. getTableViewData()는 위치 서비스가 사용되는 경우 기반으로 실행하고자하는 코드입니다. 감사.Swift 위치 서비스가 활성화 된 경우에만 코드 실행

override func viewDidLoad() { 
    super.viewDidLoad() 

    searchController.searchResultsUpdater = self 
    searchController.dimsBackgroundDuringPresentation = false 
    definesPresentationContext = true 
    tableView.tableHeaderView = searchController.searchBar 

    locationManager.delegate = self 
    locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.startUpdatingLocation() 
    locationManager.stopUpdatingLocation() 
    locationManager.requestAlwaysAuthorization() 
    locationManager.requestWhenInUseAuthorization() 


    if CLLocationManager.locationServicesEnabled() { 
     switch(CLLocationManager.authorizationStatus()) { 
     case .notDetermined, .restricted, .denied: 
      print("No access") 
     case .authorizedAlways, .authorizedWhenInUse: 
      print("Access") 
      getTableViewData() 
     } 
    } else { 
     print("Location services are not enabled") 
    } 

    refresher = UIRefreshControl() 
    refresher.attributedTitle = NSAttributedString(string: "Pull to refresh") 
    refresher.addTarget(self, action: #selector(RegisteredLocations.handleRefresh(refreshControl:)), for: UIControlEvents.valueChanged) 
    tableView.addSubview(refresher) 
} 


@objc func handleRefresh(refreshControl: UIRefreshControl) { 
    self.usersArray.removeAll() 

    if CLLocationManager.locationServicesEnabled() { 
     switch(CLLocationManager.authorizationStatus()) { 
     case .notDetermined, .restricted, .denied: 
      print("No access") 
     case .authorizedAlways, .authorizedWhenInUse: 
      print("Access") 
      getTableViewData() 
     } 
    } else { 
     print("Location services are not enabled") 
    } 

    self.tableView.reloadData() 
    refreshControl.endRefreshing() 
} 
+0

어떻게됩니까? 작동하지 않는 것은 무엇입니까? –

+0

액세스를 허용 할 때 "getTableViewData()"를 실행하지 않습니다. –

+0

액세스가 거부 된 경우 "getTableViewData()"를 실행하지 않으려합니다. –

답변

0

당신은 사용자가 현재 장치를 사용할 위치 서비스가 있는지 여부를 결정하기 위해 위치 업데이트를 시작하기 전에이 메서드의 반환 값을 확인해야합니다. 위치 서비스는 사용자가 앱에서 위치 관련 정보를 처음 사용하려고 시도하지만 이후 시도를 묻지 않습니다. 사용자가 위치 서비스 사용을 거부하고 어쨌든 위치 업데이트를 시작하려고하면 위치 관리자가 해당 대리인에게 오류를보고합니다.

해결 방법은 위치 서비스를 사용하도록 사용자가 앱 설정을 열도록 경고하는 것입니다.

let alert = UIAlertController(title: "", message: "Please Allow the location Service to open this screen.", preferredStyle: UIAlertControllerStyle.alert) 
    alert.addAction(UIAlertAction(title: "Go to Settings", style: UIAlertActionStyle.default, handler: { (alert: UIAlertAction!) in 
     UIApplication.shared.openURL(NSURL(string:UIApplicationOpenSettingsURLString)! as URL) 
    })) 

    UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil) 
관련 문제