2011-05-17 89 views
62

我想獲取當前位置,但我得到一個錯誤。didFailWithError:錯誤域= kCLErrorDomain代碼= 0「操作無法完成。(kCLErrorDomain錯誤0.)」

這是我的視圖控制器的一個片段。

- (void)viewDidLoad { 
    self.locationManager = [[CLLocationManager alloc] init]; 
    [locationManager setDelegate:self]; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; 
    [locationManager startUpdatingLocation]; 
} 

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateLocations:(NSArray<CLLocation *> *)locations { 
    // I would get the latest location here 
    // but this method never gets called 
} 
- (void)locationManager:(CLLocationManager *)manager 
     didFailWithError:(NSError *)error { 
    NSLog(@"didFailWithError: %@", error); 
} 

我期待的委託方法locationManager:didUpdateLocations:到被調用,而是,只有locationManager:didFailWithError:被調用,並打印這樣的:

didFailWithError: Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)" 
+0

您可以使用谷歌API作爲替代。請參閱我的答案在這裏http://stackoverflow.com/a/40670608/2033377 – 2016-11-18 06:25:54

回答

94

1) check that you actually have a valid WiFi and 3G connection

如果你這樣做,然後

2) go to settings and reset your location services
3) reset your network settings

這應該照顧這個問題的是相關設備/網絡應用不相關。它的煩人,尤其是如果你的應用程序是依賴於位置服務和設備是WiFi只有和人們對AppStore給予負面評價....對我來說我有點感動

+27

MeM不會變得情緒化,這是好開發商 – 2013-02-11 12:45:26

+0

OMG的標誌。謝謝。 – 2013-08-21 19:01:00

+2

據我所知GPS不需要互聯網或無線連接。它只是一個接收器,如果它在2或3個GPS衛星的可訪問位置,我們可以獲得地理位置座標。那麼,爲什麼錯誤會出現沒有互聯網或無線連接? – 2013-12-06 07:21:03

7

我有同樣的問題。我相信可能的解釋/修復涵蓋了in this SO post

120

如果您使用模擬器:

  1. command + shift + ,在Xcode打開方案編輯器
  2. 選擇運行方案
  3. 轉到選項標籤
  4. 檢查✅Allow Location Simulation
  5. 在下拉列表中選擇默認位置

選擇None作爲您的默認位置可能導致了問題。

+4

哇!謝謝,我一直在追逐這個數週。我的應用程序不斷強制我的iPad或iPhone到倫敦,英國的位置,我無法弄清楚爲什麼。我把這個選項關掉了,問題沒有了。可能是打嗝抓住了那個位置,然後離開,把位置改成了計劃中的位置。 – 2013-11-06 20:59:03

+1

太棒了,它爲我工作;)謝謝你 – Rifinio 2014-04-21 22:25:02

+1

也爲我工作。謝謝。 – 2014-10-30 09:42:14

17

嘗試重新啓動模擬器(假設這就是你正在使用的)。

嘗試了其他一切後,這爲我工作。

+0

完美的解決方案。 – zproject89 2014-04-12 03:35:33

+0

爲我工作。謝謝 – 2014-12-24 07:06:27

+0

也在這裏工作,謝謝 – VinceOPS 2016-02-11 00:27:18

2

假設您正在使用模擬器,您可以到Debug -> Location並設置一個位置。

如果你沒有選擇你會有這個錯誤。

+0

這對我工作! – holierthanthou84 2014-09-03 06:05:47

14

重新啓動模擬器不適用於我。

I had to clear everything via "iOS Simulator" >> "Reset Content and Settings... .

+3

爲我工作thnkkssssss – NullData 2014-07-26 20:00:19

20

只需忽略此錯誤並等待更多更新或其他錯誤。

位置管理器的文檔說這個錯誤是暫時的。它讓你知道,它沒有立即檢索一個位置,但它仍然在嘗試。 「在這種情況下,你可以簡單地忽略錯誤並等待新事件發生。」使用名爲locationManager:didFailWithError:的方法,這是一個非常頭疼的方式 - 謝謝Apple!

Apple Documentation

4

在模擬器上改變 「位置」 爲我工作。

Debug > Location > (Mine was checked None instead of City Bicycle Ride e.g.)

1

像這樣的問題可以通過設置位置爲「Apple」來解決。至少它適用於測試目的。

9

我在模擬器上出現這個錯誤。點擊調試面板中的位置按鈕,並設置一個位置爲我解決了這個問題。 (確保按鈕是藍色的)

enter image description here

2

如果您使用的是custom location請確保您有長期和緯度的正確方法,我曾經有過逆轉,浪費3小時我意識到之前...

+0

艾姆在這裏我也浪費了2個小時,設置了經度和緯度在正確的位置。 – 2015-07-02 09:53:14

5
  1. In the simulator go to Settings > General > Reset > Reset Location & Privacy

  2. Quit simulator and run app again

1
  1. I have reseted contents and settings.
  2. Removed data from derived data.
  3. Restarted the Xcode, and Simulator and worked for me.

從@Mem的答案和許多其他的感謝

2

我以前看到的問題,有解決這個問題的一種方式,但它只能工作一次。如果您想再次運行該項目,並且每次都需要重複該解決方案。

1.In Xcode,Product -> Scheme -> Edit Scheme,then cancel the "Allow Location Simulator".

2.To the iOS Simulator and reset Content and Setting.

3.Again to the Xcode,repeat the first step.

4.To the iOS Simulator and reset. Then it will work.

1

對於開發者返回:)剛剛從模擬器菜單中選擇 「Debug->Location->Freeway Drive」。如果仍然存在問題,請在模擬器菜單中執行「模擬器 - >重置內容和設置」後嘗試。這幫助我解決了同樣的問題。某些時間模擬器位置設置爲「Custom location」,因爲它未檢測到任何內容。

0

嘗試使用設備。有時模擬器無法獲取您的位置。

0

轉到家裏模擬器

首頁 - >設置 - >高級 - >位置 - >選擇應用程序,然後選擇總是

相關問題