2016-05-05 13 views
10

में है मैं एक ऐप्स दो उद्देश्यों के लिए CoreLocation का उपयोग करता है के निर्माण कर रहा हूँ:,CLLocationManager स्थान अद्यतन शुरू एप्लिकेशन पृष्ठभूमि

  1. पहले बीकन क्षेत्रों की निगरानी। अधिकतर हम एक विशिष्ट हार्डवेयर डिवाइस के साथ काम करते हैं जो बीकन के रूप में कार्य करता है और हम कोरलोकेशन के साथ उस बीकन कनेक्शन की स्थिति को ट्रैक करते हैं।

  2. दूसरा, जब ऐप बीकन डिवाइस से डिस्कनेक्शन का पता लगाता है, तो हम डिस्कनेक्शन कहां से पता लगाने के लिए एक जीपीएस प्रक्रिया शुरू करना चाहते हैं।

ज्यादातर मामलों में, बीकन डिवाइस का डिस्कनेक्शन कॉलबैक तब होता है जब ऐप पृष्ठभूमि में होता है। इसलिए ऐप को पृष्ठभूमि में होने पर -startUpdatingLocation पर कॉल करके जीपीएस ट्रैकिंग शुरू करनी होगी।

और यह समस्या है। आईओएस पृष्ठभूमि पृष्ठभूमि अपडेट को सक्षम नहीं करता है अगर ऐप पृष्ठभूमि में था, जबकि -startUpdatingLocation कॉल हुआ था।

जाहिर है, मैंने सही ढंग से पृष्ठभूमि मोड कॉन्फ़िगर किया है, NSLocationAlwaysUsageDescription, संपत्ति _locationManager.allowsBackgroundLocationUpdates से YES पर सेट करें।

आश्चर्यजनक बात: अगर मैं अग्रभूमि में रहते हुए -startUpdatingLocation पर कॉल करता हूं और फिर मैं पृष्ठभूमि में स्विच करता हूं, तो पृष्ठभूमि अपडेट होने पर स्थान अपडेट सही तरीके से भेजे जाते हैं। हालांकि, पृष्ठभूमि में होने पर -startUpdatingLocation पर कॉल करते समय, एप्लिकेशन को 2 या 3 स्थान अपडेट प्राप्त होते हैं और फिर हमेशा के लिए रुक जाता है .. जब तक कि मैं मैन्युअल रूप से अग्रभूमि पर एप्लिकेशन वापस नहीं लाता, जब स्थान अपडेट फिर से शुरू होता है और तब भी काम करता रहता है, भले ही मैं ऐप स्विच करता हूं पृष्ठभूमि फिर से करने के लिए।

क्या कोई जानता है या समझता है कि इसे ठीक करने के लिए मुझे क्या करना चाहिए? या यह आईओएस की एक बड़ी बाधा है? मेरे ऐप की आवश्यकताओं के कारण मुझे ऐप पृष्ठभूमि में होने पर अद्यतनों को ढूंढना शुरू करना होगा (एक बीकन के कॉलबैक के अंदर बाहर निकलने वाले क्षेत्र में)।

बीटीडब्ल्यू, यदि आप इस मुद्दे को पुन: पेश करना चाहते हैं तो यह बहुत आसान है। बस एक्सकोड में एक खाली परियोजना बनाएं और इस कोड को अपने डिफ़ॉल्ट दृश्य नियंत्रक में जोड़ें। पृष्ठभूमि मोड को सक्षम करने के लिए याद रखें अपनी जानकारी में एक विवरण जोड़ें। प्लिस्ट कुंजी "NSLocationAlwaysUsageDescription"।

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    _locationManager = [[CLLocationManager alloc] init]; 

    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_8_3) 
     _locationManager.allowsBackgroundLocationUpdates = YES; 

    _locationManager.delegate = self; 
    _locationManager.activityType = CLActivityTypeOther; 
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    _locationManager.distanceFilter = kCLDistanceFilterNone; 

    [_locationManager requestAlwaysAuthorization]; 

    // Creating a background task to delay the start of the startUpdatingLocation 
    __block UIBackgroundTaskIdentifier taskIdentifier = UIBackgroundTaskInvalid; 
    taskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ 
     [[UIApplication sharedApplication] endBackgroundTask:taskIdentifier]; 
    }]; 

    // Delaying the call to startUpdatingLocation 10 seconds, enough time to dismiss the application 
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
     [_locationManager startUpdatingLocation]; 
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
      [[UIApplication sharedApplication] endBackgroundTask:taskIdentifier]; 
     }); 
    }); 
} 

फिर, कुछ एनएसएलओजी करके CLLocationManager प्रतिनिधि को ट्रैक करें और देखें कि क्या होता है।

धन्यवाद,

+0

मुझे एक ही समस्या मिलती है। मेरे मामले में, स्थान काफी लंबे समय तक चल सकते हैं, या बस कुछ ही सेकंड बंद हो जाते हैं, या फिर शुरू नहीं होते हैं। पृष्ठभूमि में स्थान शुरू करते समय भी पृष्ठभूमि कार्य को चालू रखने की कोशिश करता हूं लेकिन कोई भाग्य नहीं है ... – nahung89

उत्तर

2

यह प्रश्न पोस्ट करने के 3 महीने बाद हो गया है। क्या आपने इसे अपने आप पहले से ही समझ लिया है?

मैंने आपका कोड आजमाया। मैंने कुछ NSLogsAppDelegate प्रतिनिधि कार्यों में और viewDidLoad में डाल दिया। इसके अलावा, AppDelegate कक्षा में, मैंने पृष्ठभूमि समय छोड़ा।

तो startUpdatingLocation पृष्ठभूमि में जाने से पहले कहा जाता है: startUpdatingLocation पृष्ठभूमि में जाने के बाद कहा जाता है

2016-08-18 01:22:52.355 test background GPS from StackOF[2267:745042] viewDidLoad 
2016-08-18 01:22:52.376 test background GPS from StackOF[2267:745042] _locationManager.allowsBackgroundLocationUpdates = YES; 
2016-08-18 01:22:52.382 test background GPS from StackOF[2267:745042] _locationManager requestAlwaysAuthorization 
2016-08-18 01:22:52.410 test background GPS from StackOF[2267:745042] applicationDidBecomeActive 
2016-08-18 01:22:52.469 test background GPS from StackOF[2267:745042] viewDidAppear 
2016-08-18 01:23:03.361 test background GPS from StackOF[2267:745042] [_locationManager startUpdatingLocation]; 
2016-08-18 01:23:03.362 test background GPS from StackOF[2267:745042] start updating location, _locationManager.allowsBackgroundLocationUpdates == YES 
2016-08-18 01:23:03.382 test background GPS from StackOF[2267:745042] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 65.000000 
2016-08-18 01:23:03.630 test background GPS from StackOF[2267:745042] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 1743.000000 
2016-08-18 01:23:03.709 test background GPS from StackOF[2267:745042] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 1743.000000 
2016-08-18 01:23:03.716 test background GPS from StackOF[2267:745042] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 1743.000000 
2016-08-18 01:23:03.720 test background GPS from StackOF[2267:745042] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 1743.000000 
2016-08-18 01:23:03.814 test background GPS from StackOF[2267:745042] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 65.000000 
2016-08-18 01:23:05.004 test background GPS from StackOF[2267:745042] inner block [[UIApplication sharedApplication] endBackgroundTask:taskIdentifier]; 
2016-08-18 01:23:05.005 test background GPS from StackOF[2267:745042] inner block start updating location, _locationManager.allowsBackgroundLocationUpdates == YES 
2016-08-18 01:23:08.287 test background GPS from StackOF[2267:745042] applicationDidEnterBackground. Background time left: 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 
2016-08-18 01:23:10.319 test background GPS from StackOF[2267:745042] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 65.000000 
2016-08-18 01:23:19.321 test background GPS from StackOF[2267:745042] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 65.000000 
/**** 
Keep on polling... 
****/ 

हैं: पृष्ठभूमि समय छोड़ दिया की

2016-08-18 01:34:29.023 test background GPS from StackOF[2285:747132] viewDidLoad 
2016-08-18 01:34:29.030 test background GPS from StackOF[2285:747132] _locationManager.allowsBackgroundLocationUpdates = YES; 
2016-08-18 01:34:29.033 test background GPS from StackOF[2285:747132] _locationManager requestAlwaysAuthorization 
2016-08-18 01:34:29.047 test background GPS from StackOF[2285:747132] applicationDidBecomeActive 
2016-08-18 01:34:29.104 test background GPS from StackOF[2285:747132] viewDidAppear 
2016-08-18 01:34:31.612 test background GPS from StackOF[2285:747132] applicationDidEnterBackground. Background time left: 179.964144 
2016-08-18 01:34:40.004 test background GPS from StackOF[2285:747132] [_locationManager startUpdatingLocation]; 
2016-08-18 01:34:40.006 test background GPS from StackOF[2285:747132] start updating location, _locationManager.allowsBackgroundLocationUpdates == YES 
2016-08-18 01:34:40.290 test background GPS from StackOF[2285:747132] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 10.000000 
2016-08-18 01:34:40.332 test background GPS from StackOF[2285:747132] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 355.117789 
2016-08-18 01:34:40.336 test background GPS from StackOF[2285:747132] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 355.118536 
2016-08-18 01:34:40.493 test background GPS from StackOF[2285:747132] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 355.141209 
2016-08-18 01:34:40.496 test background GPS from StackOF[2285:747132] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 355.147748 
2016-08-18 01:34:40.505 test background GPS from StackOF[2285:747132] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 355.148158 
2016-08-18 01:34:40.507 test background GPS from StackOF[2285:747132] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 65.000000 
2016-08-18 01:34:41.640 test background GPS from StackOF[2285:747132] inner block [[UIApplication sharedApplication] endBackgroundTask:taskIdentifier]; 
2016-08-18 01:34:41.642 test background GPS from StackOF[2285:747132] inner block start updating location, _locationManager.allowsBackgroundLocationUpdates == YES 
/**** 
stops polling... 
****/ 

लें टिप्पणी। यदि CLLocationManager स्थान अपडेट करना प्रारंभ करने से पहले ऐप पृष्ठभूमि में जाता है, तो पृष्ठभूमि का समय शेष 3 मिनट होता है। मैं खुद को भी यकीन नहीं कर रहा हूं क्योंकि मेरे पास भी अपनी जीपीएस पृष्ठभूमि चल रही समस्या है, लेकिन इस संक्षिप्त परीक्षण से ऐसा लगता है कि ऐप पृष्ठभूमि से ऐप्पल जाने से पहले CLLocationManager को कॉल किया जाना चाहिए था।

यहाँ जहां मैं अपने NSLogs डाल:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    NSLog(@"viewDidLoad"); 

    _locationManager = [[CLLocationManager alloc] init]; 

    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_8_3) 
    { 
     _locationManager.allowsBackgroundLocationUpdates = YES; 
     NSLog(@"_locationManager.allowsBackgroundLocationUpdates = YES;"); 
    } 



    _locationManager.delegate = self; 
    _locationManager.activityType = CLActivityTypeOther; 
    _locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; 
    _locationManager.distanceFilter = kCLDistanceFilterNone; 

    [_locationManager requestAlwaysAuthorization]; 
    NSLog(@"_locationManager requestAlwaysAuthorization"); 

    // Creating a background task to delay the start of the startUpdatingLocation 
    __block UIBackgroundTaskIdentifier taskIdentifier = UIBackgroundTaskInvalid; 
    taskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ 
     [[UIApplication sharedApplication] endBackgroundTask:taskIdentifier]; 
     NSLog(@"[[UIApplication sharedApplication] endBackgroundTask:taskIdentifier];"); 
    }]; 

    // Delaying the call to startUpdatingLocation 10 seconds, enough time to dismiss the application 
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
     [_locationManager startUpdatingLocation]; 
     NSLog(@"[_locationManager startUpdatingLocation];"); 
     if(_locationManager.allowsBackgroundLocationUpdates == YES) 
     { 
      NSLog(@"start updating location, _locationManager.allowsBackgroundLocationUpdates == YES"); 
     } 
     else 
     { 
      NSLog(@"start updating location, _locationManager.allowsBackgroundLocationUpdates == NO"); 
     } 
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
      [[UIApplication sharedApplication] endBackgroundTask:taskIdentifier]; 
      NSLog(@"inner block [[UIApplication sharedApplication] endBackgroundTask:taskIdentifier];"); 
      if(_locationManager.allowsBackgroundLocationUpdates == YES) 
      { 
       NSLog(@"inner block start updating location, _locationManager.allowsBackgroundLocationUpdates == YES"); 
      } 
      else 
      { 
       NSLog(@"inner block start updating location, _locationManager.allowsBackgroundLocationUpdates == NO"); 
      } 

     }); 
    }); 

} 

CLLocationManager प्रतिनिधि समारोह:

-(void)locationManager:(CLLocationManager *) manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations 
{ 
    CLLocation *location = [locations firstObject]; 
    NSLog(@"Lat/Long: %f %f, horizontal accuracy: %f", 
      location.coordinate.latitude, 
      location.coordinate.longitude, 
      location.horizontalAccuracy); 
} 

AppDelegate:

- (void)applicationDidEnterBackground:(UIApplication *)application { 

    NSLog(@"applicationDidEnterBackground. Background time left: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]); 

} 

टी एल; डॉ: startUpdatingLocation बुला की कोशिशमें ठीक हैया ऐपडिलेगेट में application:didFinishLaunchingWithOptions:

संबंधित मुद्दे