पर ट्रिगर नहीं करता है, मैं महत्वपूर्ण स्थान परिवर्तन निगरानी का उपयोग कर रहा हूं। जब मैं सिम्युलेटर में कोड चलाता हूं और फ्रीवे विकल्प की जांच करता हूं तो मुझे आवधिक स्थान अपडेट मिलते हैं। मैं इन अद्यतनों को पकड़ता हूं और उन्हें NSUserDefaults में सहेजता हूं और प्रत्येक 10s में टेबलव्यू अपडेट करने की तुलना में, इसलिए मैं देख सकता हूं कि मुझे कोई अपडेट मिला है या नहीं। यदि मैं एक असली डिवाइस पर ऐप चलाता हूं, तो मुझे शून्य अपडेट मिलते हैं। मैंने फोन को अपनी जेब में रखा और 80 किमी से अधिक की यात्रा की, 2 शहरों में। शून्य अपडेट यकीन नहीं है कि अगर मैंने कुछ गड़बड़ कर ली है। मैं कोड संलग्न कर रहा हूँ। कोड कॉपी पेस्ट है, परीक्षण करने के लिए स्वतंत्र महसूस करें। बस स्टोरीबोर्ड में TableViewController का उपयोग करना सुनिश्चित करें और आईडी आईडी को आईडी पर सेट करें। मैं क्या खो रहा हूँ? मैं iphone पर परीक्षण कर रहा हूँ 5.महत्वपूर्ण स्थान परिवर्तन डिवाइस
Info.plist:
संपादित करें: सेब डॉक्स में मिला था। क्या मुझे locationManager
अलग-अलग बनाया जाना चाहिए?
किसी एप्लिकेशन को स्थान अपडेट, लांच विकल्प आपके आवेदन करने के लिए पारित कर दिया शब्दकोश की वजह से फिर से लॉन्च किया गया है: willFinishLaunchingWithOptions: या आवेदन: didFinishLaunchingWithOptions: विधि UIApplicationLaunchOptionsLocationKey कुंजी है। उस कुंजी संकेतों की उपस्थिति सिग्नल करती है कि नया स्थान डेटा आपके ऐप पर पहुंचने का इंतजार कर रहा है। उस डेटा को प्राप्त करने के लिए, आपको एक नया CLLocationManager ऑब्जेक्ट बनाना होगा और आपके ऐप की समाप्ति से पहले चल रहे स्थान सेवाओं को पुनरारंभ करना होगा। जब आप उन सेवाओं को पुनरारंभ करते हैं, तो स्थान प्रबंधक अपने प्रतिनिधि को सभी लंबित स्थान अपडेट प्रदान करता है।
EDIT2:
इस के आधार पर, स्थान कम से कम हर 15 मिनट अद्यतन किया जाना चाहिए। मेरे कोड में बग पुष्टि की।
तो जीपीएस-स्तरीय सटीकता अपने अनुप्रयोग के लिए गंभीर नहीं है और आप निरंतर ट्रैकिंग की जरूरत नहीं है, आप महत्वपूर्ण परिवर्तन स्थान सेवा का उपयोग कर सकते हैं। यह महत्वपूर्ण है कि आप महत्वपूर्ण परिवर्तन स्थान सेवा का सही ढंग से उपयोग करें, क्योंकि यह प्रत्येक 15 मिनट में सिस्टम और आपके ऐप को कम से कम बनाता है, भले ही कोई स्थान परिवर्तन न हो, और इसे तब तक जारी रखता है जब तक आप इसे रोक नहीं देते।
संपादित 3: यह कोड ऐपडिलेगेट didFinishLaunchingWithOptions:
पर यह कोड देखने के लिए जोड़ा गया कि ऐप जागृत हो गया है या नहीं। यह जागृत नहीं होता है- मुझे तालिका दृश्य में 200 200 प्रविष्टि दिखाई नहीं देती है। कुछ ख़राब चल रहा है।
if let options = launchOptions {
print("options")
if (launchOptions![UIApplicationLaunchOptionsLocationKey] != nil){
locationManager.startUpdatingLocation()
self.lat.append(Double(200))
self.lon.append(Double(200))
self.times.append(NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .NoStyle, timeStyle: .ShortStyle))
NSUserDefaults.standardUserDefaults().setObject(lat, forKey: "lat")
NSUserDefaults.standardUserDefaults().setObject(lon, forKey: "lon")
NSUserDefaults.standardUserDefaults().setObject(times, forKey: "time")
}
कोड: // AppDelegate:
import UIKit
import CoreLocation
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var lat:[CLLocationDegrees]!
var lon:[CLLocationDegrees]!
var times:[String]!
var distances: [String]!
var window: UIWindow?
var locationManager: CLLocationManager!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
locationManager=CLLocationManager()
locationManager.delegate=self
locationManager.requestAlwaysAuthorization()
let isFirstLaunch = NSUserDefaults.standardUserDefaults().objectForKey("lat")
if isFirstLaunch == nil{
lat = [CLLocationDegrees]()
lon = [CLLocationDegrees]()
times = [String]()
NSUserDefaults.standardUserDefaults().setObject(lat, forKey: "lat")
NSUserDefaults.standardUserDefaults().setObject(lon, forKey: "lon")
NSUserDefaults.standardUserDefaults().setObject(times, forKey: "time")
}else{
lat = NSUserDefaults.standardUserDefaults().arrayForKey("lat") as! [CLLocationDegrees]
lon = NSUserDefaults.standardUserDefaults().arrayForKey("lon") as! [CLLocationDegrees]
times = NSUserDefaults.standardUserDefaults().objectForKey("time") as! [String]
// distances = NSUserDefaults.standardUserDefaults().objectForKey("distance") as! [String]
}
return true
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("location updated")
self.lat.append(locations[0].coordinate.latitude)
self.lon.append(locations[0].coordinate.longitude)
self.times.append(NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .NoStyle, timeStyle: .ShortStyle))
NSUserDefaults.standardUserDefaults().setObject(lat, forKey: "lat")
NSUserDefaults.standardUserDefaults().setObject(lon, forKey: "lon")
NSUserDefaults.standardUserDefaults().setObject(times, forKey: "time")
print("Location: \(locations[0].coordinate.latitude) \(locations[0].coordinate.longitude)")
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
print("did change AS")
switch status {
case .AuthorizedWhenInUse:
locationManager.startMonitoringSignificantLocationChanges()
case .AuthorizedAlways:
print("to always")
locationManager.startMonitoringSignificantLocationChanges()
if lat.count==0{
self.lat.append((locationManager.location?.coordinate.latitude)!)
self.lon.append((locationManager.location?.coordinate.longitude)!)
self.times.append(NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .NoStyle, timeStyle: .ShortStyle))
NSUserDefaults.standardUserDefaults().setObject(lat, forKey: "lat")
NSUserDefaults.standardUserDefaults().setObject(lon, forKey: "lon")
NSUserDefaults.standardUserDefaults().setObject(times, forKey: "time")
}
// locationManager.startUpdatingLocation()
break
default:
locationManager.stopMonitoringSignificantLocationChanges()
break
}
}
}
// देखें नियंत्रक
import UIKit
class TableViewController: UITableViewController {
let appDel = UIApplication.sharedApplication().delegate as! AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: "updateTableView", userInfo: nil, repeats: true)
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return appDel.lon.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ID", forIndexPath: indexPath)
cell.textLabel?.text = "\(appDel.lat[indexPath.row]) \(appDel.lon[indexPath.row]) \(appDel.times[indexPath.row])"
cell.textLabel?.font = UIFont.systemFontOfSize(9)
return cell
}
func updateTableView(){
self.tableView.reloadData()
}
}
क्या आप वास्तव में प्राधिकरण के लिए अनुरोध देखते हैं? यदि नहीं, तो क्या आपको अपने प्लिस्ट में NSLocationAlwaysUsageDescription मिला है? –
हाँ अनुरोध पॉप आउट हो गया है।और हां, NSLocationAlwaysUsageDescription स्ट्रिंग info.plist में सेट है। – brumbrum
क्या आप ऐप पर डबल बटन टैप करके ऐप पर स्वाइप करके ऐप को मार रहे हैं? यदि आप इस तरह से ऐप को मारते हैं, तो यह पृष्ठभूमि महत्वपूर्ण परिवर्तन सेवाओं को नहीं चलाएगा। – Rob