gpt4 book ai didi

ios - 有什么方法可以经常使用 3G/4G/LTE 从后台上传位置?

转载 作者:行者123 更新时间:2023-12-01 16:36:51 25 4
gpt4 key购买 nike

我想制作一个 iOS7 设计,以使用 3G/4G/LTE(非 WiFi)从后台将其位置上传到服务器,频率低于每 5 到 10 分钟一次,或者当它的位置发生变化时。

我已经尝试过的是这样的:

1.后台获取
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler叫得这么不规律,不可控。

2.远程通知(非静音推送)
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler仅在 WiFi 连接中调用。

3.CLLocationManager

我把上传者放在 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation但仅在 WiFi 上时调用。

我想实现一个像“Find Your Friends”这样的应用程序,它是 iOS8 中的 Apple 原生应用程序。有什么想法吗?

感谢您的帮助。

更新

我试过NSURLSession uploadCLLocationManager didUpdateToLocation 触发如下所示,但它仍然只在 WiFi 上上传。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{

[[NSUserDefaults standardUserDefaults] setFloat:newLocation.coordinate.latitude forKey:@"lat"];
[[NSUserDefaults standardUserDefaults] setFloat:newLocation.coordinate.longitude forKey:@"lon"];
[[NSUserDefaults standardUserDefaults] synchronize];

[self update];

}

- (void)update
{

NSURL *url = [NSURL URLWithString:@"http://.../updater.php"];
NSString *identifier = [NSDate date].description;
NSURLSessionConfiguration *configration = [NSURLSessionConfiguration backgroundSessionConfiguration:identifier];
configuration.networkServiceType = NSURLNetworkServiceTypeBackground;
configuration.discretionary = NO;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"PUT";
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
delegate:self
delegateQueue:[NSOperationQueue mainQueue]];

float lat = [[NSUserDefaults standardUserDefaults] floatForKey:@"lat"];
float lon = [[NSUserDefaults standardUserDefaults] floatForKey:@"lon"];

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:lat], @"lat",
[NSNumber numberWithFloat:lon], @"lon", nil];

NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];

NSString *path = [[self applicationDocumentsDirectory].path stringByAppendingPathComponent:@"json.data"];
[data writeToFile:path atomically:YES];

NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromFile:[NSURL fileURLWithPath:path]];

[task resume];

}

- (NSURL *)applicationDocumentsDirectory
{

// The directory the application uses to store the Core Data store file. This code uses a directory named "jp.skyElements.NNZ" in the application's documents directory.
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

}

最佳答案

您有后台位置更新模式,可让您的应用收到有关位置更改的通知。在 Background Modes 中阅读更多信息iOS 的 Xcode 文档中的部分。

现在我从未尝试在后台位置更新期间执行任何网络请求,但我会尝试以下内容:
NSURLSession有后台配置。 POST 到服务器并将所有负担委托(delegate)给 iOS 本身。 QoS、网络节流、电源效率和其他好处由此而来。

关于ios - 有什么方法可以经常使用 3G/4G/LTE 从后台上传位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27459528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com