2016-09-17 11 views
7

मेरी कोड के सभी AppDelegate.m में है:महत्वपूर्ण परिवर्तन स्थान प्रतिनिधि तरीकों नहीं बुलाया जा रहा है

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; 

    _locationMgr = [[CLLocationManager alloc] init]; 
    [_locationMgr setDelegate:self]; 
    if([_locationMgr respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) 
     [_locationMgr setAllowsBackgroundLocationUpdates:YES]; 
    CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus]; 

    if([launchOptions valueForKey:UIApplicationLaunchOptionsLocationKey] != nil) { 
     NSLog(@"relaunching because of significant location change - restarting SLC"); 
     [_locationMgr startMonitoringSignificantLocationChanges]; 
    } 
    else 
    { 
     if (authorizationStatus == kCLAuthorizationStatusAuthorizedAlways) { 
      NSLog(@"launching with authorization to always use location - starting SLC"); 
      [_locationMgr startMonitoringSignificantLocationChanges]; 
     } 
     else 
     { 
      NSLog(@"launching with no authorization to always use location - requesting authorization"); 
      if([_locationMgr respondsToSelector:@selector(requestAlwaysAuthorization)]) 
       [_locationMgr requestAlwaysAuthorization]; 
     } 
    } 

    if([userdefaults objectForKey:@"pfuser"] == nil) { 
     NSLog(@"in delegate signup"); 
     SignUpController *signup = [[SignUpController alloc] init]; 
     [self.window setRootViewController:signup]; 
    } 
    else { 
     ViewController *map = [[ViewController alloc] init]; 
     [self.window setRootViewController:map]; 
    } 
    [self.window makeKeyAndVisible]; 

    return YES; 
} 

- (void)startSignificantChangeUpdates 
{ 
    deviceNotFoundAlertController = [UIAlertController alertControllerWithTitle:@"START" message:@"startSignificantChangeUpdates called" preferredStyle:UIAlertControllerStyleAlert]; 

    [deviceNotFoundAlertController addAction:deviceNotFoundAlert]; 
    // Create the location manager if this object does not 
    // already have one. 
    if (nil == _locationMgr) { 
     _locationMgr = [[CLLocationManager alloc] init]; 
     _locationMgr.delegate = self; 
    } 

    [CLLocationManager significantLocationChangeMonitoringAvailable]; 
    [_locationMgr startMonitoringSignificantLocationChanges]; 
} 

-(void)locationManger:(CLLocationManager *)manager didFailWithError:(NSError *)error { 
    NSLog(@"didFailWithError: %@", error); 
    deviceNotFoundAlertController = [UIAlertController alertControllerWithTitle:@"LOCATION FAIL" message:@"didFailWithError" preferredStyle:UIAlertControllerStyleAlert]; 

    [deviceNotFoundAlertController addAction:deviceNotFoundAlert]; 
} 

// Delegate method from the CLLocationManagerDelegate protocol. 
- (void)_locationManager:(CLLocationManager *)manager 
     didUpdateLocations:(NSArray *)locations { 
    deviceNotFoundAlertController = [UIAlertController alertControllerWithTitle:@"LOCATION UPDATE" message:@"didUpdateLocations called" preferredStyle:UIAlertControllerStyleAlert]; 

    [deviceNotFoundAlertController addAction:deviceNotFoundAlert]; 
    // If it's a relatively recent event, turn off updates to save power. 
    CLLocation* location = [locations lastObject]; 
    NSDate* eventDate = location.timestamp; 
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; 
    if (fabs(howRecent) < 15.0) { 
     // If the event is recent, do something with it. 
     NSLog(@"latitude %+.6f, longitude %+.6f\n", 
       location.coordinate.latitude, 
       location.coordinate.longitude); 
    } 
} 

अलर्ट में से कोई भी हो, ऐसा प्रतिनिधि पद्धतियों को बुलाया नहीं किया जा रहा है लगता है।

अद्यतन

अब मेरे पास है:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; 

    deviceNotFoundAlert = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]; 

    ... 

} 

// Delegate method from the CLLocationManagerDelegate protocol. 
- (void)locationManager:(CLLocationManager *)manager 
     didUpdateLocations:(NSArray *)locations { 
    deviceNotFoundAlertController = [UIAlertController alertControllerWithTitle:@"LOCATION UPDATE" message:@"didUpdateLocations called" preferredStyle:UIAlertControllerStyleAlert]; 

    [deviceNotFoundAlertController addAction:deviceNotFoundAlert]; 
    // If it's a relatively recent event, turn off updates to save power. 
    CLLocation* location = [locations lastObject]; 
    NSDate* eventDate = location.timestamp; 
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; 
    if (fabs(howRecent) < 15.0) { 
     // If the event is recent, do something with it. 
     NSLog(@"latitude %+.6f, longitude %+.6f\n", 
       location.coordinate.latitude, 
       location.coordinate.longitude); 
    } 
} 

जब मैं ऐप का परीक्षण, मैं इसे अपने घर में खोलें, और फिर इसे बंद, इतना है कि यह भेजना चाहिए जब मैं अपने घर छोड़ने किसी बिंदु पर एक चेतावनी (या 3), लेकिन मुझे किसी भी प्रतिनिधि तरीकों से अलर्ट नहीं मिल रहे हैं (जहां मैंने अलर्ट लगाए थे)।

मैं सिर्फ एक विचार था, शायद मैं मुख्य UIViewController, नहीं AppDelegate से अलर्ट प्रदर्शित करने के लिए है?

यही कारण है कि मैं अलर्ट नहीं दिखाई दे रहा हो सकता है: How do I add a UIAlertController in app delegate (obj-c)

अद्यतन

यह कैसे मैं अब अलर्ट कर रहा हूँ है:

deviceNotFoundAlertController = [UIAlertController alertControllerWithTitle:@"START" message:@"startSignificantChangeUpdates called" preferredStyle:UIAlertControllerStyleAlert]; 

[deviceNotFoundAlertController addAction:deviceNotFoundAlert]; 

alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
alertWindow.rootViewController = [[UIViewController alloc] init]; 
alertWindow.windowLevel = UIWindowLevelAlert + 1; 
[alertWindow makeKeyAndVisible]; 
[alertWindow.rootViewController presentViewController:deviceNotFoundAlertController animated:YES completion:nil]; 

अद्यतन

अलर्ट समस्या नहीं प्रतीत होती, एले startSignificantChangeUpdates में आरटी कभी प्रकट नहीं होता है। एक बार जब मैं अपने शुरुआती स्थान से 500 मीटर हूं तो क्या यह दिखाना चाहिए?

अद्यतन

किसी को भी मुझे इस को समझने में मदद कर सकते हैं?

आपके प्रतिनिधि वस्तु के तरीकों को उस थ्रेड से बुलाया जाता है जिसमें आपने संबंधित स्थान सेवाएं शुरू की थीं। उस थ्रेड के पास एक सक्रिय रन लूप होना चाहिए, जैसा कि आपके एप्लिकेशन के मुख्य थ्रेड में पाया गया है।

अद्यतन

मुझे लगता है कि मैं पता लगा क्या उपर्युक्त उद्धरण कह रहा है ... और मैं अब इस राशि - मैं कल का परीक्षण होगा।

... 

if([launchOptions valueForKey:UIApplicationLaunchOptionsLocationKey] != nil) { 
     NSLog(@"relaunching because of significant location change - restarting SLC"); 
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
      [_locationMgr startMonitoringSignificantLocationChanges]; 
     }); 
    } 
    else 
    { 
     if (authorizationStatus == kCLAuthorizationStatusAuthorizedAlways) { 
      NSLog(@"launching with authorization to always use location - starting SLC"); 
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
       [_locationMgr startMonitoringSignificantLocationChanges]; 
      }); 
     } 
     else 
     { 
      NSLog(@"launching with no authorization to always use location - requesting authorization"); 
      if([_locationMgr respondsToSelector:@selector(requestAlwaysAuthorization)]) 
       [_locationMgr requestAlwaysAuthorization]; 
     } 
    } 

... 

मुझे लगता है कि कोड अपने स्वयं के धागे पर स्थान सेवाएं शुरू कर रहा है। एक बात मैंने पहले ही देखी है, यह है कि जब मैं ऐप से बाहर निकलता हूं, तो शीर्ष दाएं स्थान का स्थान दूर हो जाता है। मैंने अभी आईओएस 10 में अपडेट किया है। आईओएस 9 में शीर्ष दाएं स्थान में स्थान तीर वहां रहेगा, लेकिन जब ऐप नहीं चल रहा था तो यह केवल एक काला रूपरेखा होगी। यह सिर्फ कुछ ऐसा हो सकता है जो उन्होंने आईओएस 10 के साथ बदल दिया, या अब क्योंकि मैंने 10 तक अपडेट किया है, अब कुछ और काम नहीं कर रहा है। या ऐसा तब होता है जब स्थान सेवाएं अपने स्वयं के धागे पर चलती हैं।यहाँ से: iOS start Background Thread

अद्यतन

शायद मैं धागा सही ढंग से उपयोग नहीं कर रहा है, लेकिन जैसा कि मैंने कहा, अब जब मैं अनुप्रयोग बंद करें, स्थान सेवाओं इस्तीफा। जब मैं धागे के बिना ऐसा कर रहा था तो स्थान सेवा तीर एक रूपरेखा के रूप में, शीर्ष दाएं में रहेगा।

अद्यतन

मैंने पढ़ा है कि सेवा मुख्य थ्रेड पर शुरू किया जाना चाहिए - तो अब मेरे पास है:

CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus]; 

    NSLog(@"launching with no authorization to always use location - requesting authorization"); 
    if([_locationMgr respondsToSelector:@selector(requestAlwaysAuthorization)]) { 
     [_locationMgr requestAlwaysAuthorization]; 
    } 

    if([launchOptions valueForKey:UIApplicationLaunchOptionsLocationKey] != nil) { 
     NSLog(@"relaunching because of significant location change - restarting SLC"); 
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
      [_locationMgr startMonitoringSignificantLocationChanges]; 
     }); 
    } 
    else if (authorizationStatus == kCLAuthorizationStatusAuthorizedAlways) { 
     NSLog(@"launching with authorization to always use location - starting SLC"); 
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
      [_locationMgr startMonitoringSignificantLocationChanges]; 
     }); 
    } 
    else { 
     // 
    } 

सही दिखाई नहीं देता जब अनुप्रयोग है में तीर बंद, क्या यह आईओएस 10 के लिए कुछ नया है जहां वे इसे और नहीं दिखाते हैं?

अद्यतन

मैंने गलती से नष्ट कर दिया: _locationMgr = [[CLLocationManager alloc] init]; मैं में डाल दिया और अब तीर हमेशा वहाँ है, आज का परीक्षण करने जा रहा है।

अद्यतन

मैं इसे कोई अलर्ट परीक्षण किया है, अभी भी।

+0

क्या आपने प्लेस्ट में स्थान सेवा का उपयोग करने के लिए कॉन्फ़िगर किया था ?? यदि नहीं तो आपको इसे करने की ज़रूरत है।दो विकल्प अनुरोध हैं हमेशा प्राधिकरण और अनुरोध WhenInUseAuthorization। मुझे बताएं कि क्या यह आपकी समस्या हल करता है – Janmenjaya

+0

हाय, हाँ मेरे पास है - जब मैं इसे टाइप करता हूं तो 'गोपनीयता - स्थान हमेशा उपयोग विवरण' में बदल जाता है और मैंने दूसरे कॉलम में एक संदेश दिया ... वास्तव में कुछ अलग है ... मुझे लगता है कि मुझे दोबारा परीक्षण करने की ज़रूरत है बीमार आपको – ewizard

+0

पता है कि मेरे पास 'आइटम 0' के साथ 'आवश्यक पृष्ठभूमि मोड' और 'स्थान अपडेट के लिए ऐप रजिस्ट्रार' – ewizard

उत्तर

2

मैं अपनी कार में मेरे साथ मेरे कंप्यूटर ले लिया, और सांत्वना देखा, और मैंने देखा कि महत्वपूर्ण स्थान परिवर्तन अब हो रही हैं, क्योंकि मैं स्थान हर 500 मी अपडेट प्राप्त करें। अलर्ट ही काम नहीं कर रहे एकमात्र चीज हैं, लेकिन वे कार्यक्रम के लिए अप्रासंगिक हैं - वे यह देखने के लिए वहां थे कि यह काम कर रहा था या नहीं। यह इस कोड के साथ काम कर रहा है:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; 

    ... 

    _locationMgr = [[CLLocationManager alloc] init]; 
    [_locationMgr setDelegate:self]; 

    if([_locationMgr respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) 
     [_locationMgr setAllowsBackgroundLocationUpdates:YES]; 

    CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus]; 

    NSLog(@"launching with no authorization to always use location - requesting authorization"); 
    if([_locationMgr respondsToSelector:@selector(requestAlwaysAuthorization)]) { 
     [_locationMgr requestAlwaysAuthorization]; 
    } 

    if([launchOptions valueForKey:UIApplicationLaunchOptionsLocationKey] != nil) { 
     NSLog(@"relaunching because of significant location change - restarting SLC"); 
     [_locationMgr startMonitoringSignificantLocationChanges]; 
    } 
    else if (authorizationStatus == kCLAuthorizationStatusAuthorizedAlways) { 
     NSLog(@"launching with authorization to always use location - starting SLC"); 
     [_locationMgr startMonitoringSignificantLocationChanges]; 
    } 
    else { 
     // 
    } 

    ... 

    [self.window makeKeyAndVisible]; 

    return YES; 
} 

- (void)startSignificantChangeUpdates 
{ 
    // Create the location manager if this object does not 
    // already have one. 
    if (nil == _locationMgr) { 
     _locationMgr = [[CLLocationManager alloc] init]; 
     _locationMgr.delegate = self; 
    } 

    [CLLocationManager significantLocationChangeMonitoringAvailable]; 

    [_locationMgr startMonitoringSignificantLocationChanges]; 

    deviceNotFoundAlertController = [UIAlertController alertControllerWithTitle:@"START" message:@"startSignificantChangeUpdates called" preferredStyle:UIAlertControllerStyleAlert]; 

} 

-(void)locationManger:(CLLocationManager *)manager didFailWithError:(NSError *)error { 
    NSLog(@"didFailWithError: %@", error); 
} 

// Delegate method from the CLLocationManagerDelegate protocol. 
- (void)locationManager:(CLLocationManager *)manager 
     didUpdateLocations:(NSArray *)locations { 

    // If it's a relatively recent event, turn off updates to save power. 
    CLLocation* location = [locations lastObject]; 
    NSDate* eventDate = location.timestamp; 
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; 
    if (fabs(howRecent) < 15.0) { 
     // If the event is recent, do something with it. 
     NSLog(@"latitude %+.6f, longitude %+.6f\n", 
       location.coordinate.latitude, 
       location.coordinate.longitude); 
    } 
} 
2

यह अपने प्रतिनिधि विधि के साथ एक समस्या के साथ

- (void)locationManager:(CLLocationManager *)manager 
     didUpdateLocations:(NSArray *)locations { 
} 

आशा है कि यह आपकी मदद करेगा एक

- (void)_locationManager:(CLLocationManager *)manager 
     didUpdateLocations:(NSArray *)locations { 
} 

नीचे की जगह कृपया है।

+0

भयानक पकड़ है .... सभी खोज + प्रतिस्थापन के कारण। ..i इसे जल्द ही कोशिश करूंगा – ewizard

+0

मैंने परिवर्तन किया - लेकिन मुझे अभी भी कोई अलर्ट नहीं मिल रहा है। साथ ही, मुझे 'startSignificantChangeUpdates'' से अलर्ट नहीं मिला है? – ewizard

+0

मुझे यह काम मिल सकता है, मुझे फिर से परीक्षण करने की आवश्यकता है ... एक बड़ी समस्या यह थी कि मैंने कभी भी डिवाइस नहीं किया 'डिवाइस नॉटफाउंडअर्टर्ट' – ewizard

1
You have written write code, Just add below delegate method in your code. But startMonitoringSignificantLocationChanges for updating location take 10 to 20 min. and also trigger if location channel change. 

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 

} 
1

[_locationMgr startMonitoringSignificantLocationChanges];

महत्वपूर्ण परिवर्तन स्थान सेवा केवल तभी अपडेट प्रदान करती है जब डिवाइस के स्थान में 500 मीटर या उससे अधिक के महत्वपूर्ण परिवर्तन हुए हैं।

तो आपकी डिवाइस विधि प्रत्येक बार कॉल करेगी जब आपका डिवाइस 500 मीटर से अधिक स्थानांतरित हो जाएगा।

सुनिश्चित करें कि आपके ऐप में पृष्ठभूमि स्थान की अनुमति है।

कि आपके ऐप पृष्ठभूमि या अग्रभूमि में चल रही है तो यह प्रतिनिधि विधि फोन करेगा, जिनमें आपने स्थान प्रबंधक वस्तु बनाने और नए स्थान प्राप्त करने के लिए स्थान फिर से शुरू कर दिया है अन्यथा यह एप्लिकेशन AppDelegate फ़ाइल में स्थान के विकल्प के साथ शुभारंभ करेंगे।

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html

+0

के साथ कुछ गलत है, मेरे पास पृष्ठभूमि है अनुमति, यह 500m + के बाद काम नहीं करता - क्या मेरा कोड सही दिखता है? – ewizard

+0

स्थान प्रतिनिधि विधि में स्थानीय अधिसूचना पर कॉल करें, सुनिश्चित करें कि नई प्रतिनिधि विधि। और लगभग 2+ केएम ले जाएँ। यह 500 + मीटर का सटीक 500 मीटर कॉल नहीं करता है, कभी-कभी नेटवर्क सेवा के आधार पर अधिक दूरी लेता है। तो आप पहचान सकते हैं। इसके कामकाज मैंने इसका परीक्षण किया –

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