2016-04-29 13 views
0

私はCLLocationManagerを使用しています。問題はアプリが起動してから2秒後(スプラッシュスクリーンが表示されているとき)にホームボタンを押してからアプリをバックグラウンドに移動し、秒、自動隠れ、ユーザーがカントをクリックすることができるため、ポップアップが自動的に隠れてしまい、アプリがフォアグラウンド位置のポップアップをバックグラウンドに移動してしまい、アプリを終了すると再び開いているアプリケーションのポップアップが表示されているか、一般的には場所のためのオプションが存在しない場合、私のコードに問題がある、私は何をすべきかではなく、許可するか、私のこと、これは問題である。ここIOSフレームワーク である私のコードはSwift CLLocationManagerポップアップがホームボタンを押したままにならない

Plist 
<key>NSLocationAlwaysUsageDescription</key> 
    <string>Get Your Location </string> 
    <key>NSLocationWhenInUseUsageDescription</key> 
    <string>Get Your Location For </string> 

AppDelegate.swift 
import CoreLocation 


class AppDelegate: UIResponder, UIApplicationDelegate ,CLLocationManagerDelegate { 
    let locationManager = CLLocationManager() 
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
     // Override point for customization after application launch. 
     //NSThread.sleepForTimeInterval(3); 

     self.initLocationManager(); 

     return true 
    } 
func initLocationManager() { 


     // locationManager = CLLocationManager() 
     self.locationManager.delegate = self 
     self.locationManager.distanceFilter = 40 // Must move at least 1km 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer 
     self.locationManager.requestWhenInUseAuthorization() 
     self.locationManager.startUpdatingLocation() 
     //self.locationManager.requestAlwaysAuthorization() 



    } 

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     print(locations) 
    } 

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) 
    { 

    } 


    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { 



     print("Error") 

    } 

} 

です私はアップルストアからダウンロードすると思うときに私を助けてください私はアップルストアのアプリとUberアプリは、スプラッシュ画面の時間のポップアップでは、このアプリケーションでは、バックアプリでは、私のコードではできませんができます。私を助けてください 。

ありがとう よろしく、 Nishant Chandwani

答えて

0

は、なぜあなたは、あなたのアプリが起動したときにちょうどhomeButtonを押していますか? 表示されている最初のViewControllerで許可を求めることをお勧めします。また、許可を求める前にCLLocationManagerを初期化する必要があります。それはあなたがYourLocationManager.swiftという名前のクラスを持っている場合は、この使用することができますので、CLLocationManagerためにシングルトンを使用することも非常に便利です:

YourLocationManager.swift 

    class YourLocationManager: NSObject, CLLocationManagerDelegate { 

    static let sharedInstance = YourLocationManager() 

    var locationManager: CLLocationManager? 


    func startLocationManager() { 
     NSLog("Start UpdatedManagerLocation") 
     if locationManager == nil { 
      NSLog("Initialize location Manager") 
      locationManager = CLLocationManager() 
      if let manager = locationManager { 
       manager.desiredAccuracy = kCLLocationAccuracyBest 
       manager.distanceFilter = 5.0 
       manager.delegate = self 
       manager.activityType = CLActivityType.Fitness 
       manager.pausesLocationUpdatesAutomatically = true 
      } 
     } 
     self.locationManager?.startUpdatingLocation() 
    } 

    func stopLocationManager() { 
       NSLog("Stop LocationUpdatedManager") 
       self.locationManager?.stopUpdatingLocation() 
    } 

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
       print(locations) 
      // Maybe you also want to just print locations.last which is the last position received 
    } 

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 
     // I recommend you to send a NSNotificationCenter to detect if the user changes it in the settings so that you can handle it 
} 

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { 
    print("Error") 
} 

をそしてあなたは、ユーザーの許可を求めるしたい場合は、ちょうどあなたのViewControllerに開始しますか、でも、あなたのAppDelegate)お住まいの地域のマネージャーに:

YourLocationManager.sharedInstance.startLocationManager() 

次に、あなたはそれに許可を求める:

YourLocationManager.sharedInstance.locationManager?.requestWhenInUseAuthorization() 

私はAppDelegateでpは、これを使用している場合ではありませんアプリケーションがバックグラウンドになっても、ユーザーのアクセス権を求めるopup。

+0

私はこのコードを試してみましたが、私の問題は解決していません –

関連する問題