-->

iOS的HTTP请求而在背景(iOS HTTP request while in backgroun

2019-06-17 16:38发布

这有可能使异步HTTP请求到PHP服务器,而应用程序在后台是? 该应用程序是一个基于位置之一,应收集当前的位置和发送的坐标服务器每5(或其它值)分钟。 我可以让HTTP职位,甚至是应用程序在后台服务器? 我看了很多关于这个想法,但他们中的一些告知可以做,其他人无法做到的。

谢谢,

亚历克斯。

Answer 1:

这是可以做到,而是因为你问的是OS时间送东西,它可以接受或拒绝你的要求是不可靠的。 这是我(从偷上某处SO):

[...] //we get the new location from CLLocationManager somewhere here    
BOOL isInBackground = NO;
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
{
    isInBackground = YES;
}
if (isInBackground)
{
    [self sendBackgroundLocationToServer:newLocation];
}


- (void) sendBackgroundLocationToServer: (CLLocation *) lc
{
    UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
    bgTask = [[UIApplication sharedApplication]
         beginBackgroundTaskWithExpirationHandler:^{
             [[UIApplication sharedApplication] endBackgroundTask:bgTask];
    }];

    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:2];
    [dictionary setObject:[NSNumber numberWithDouble:lc.coordinate.latitude] forKey:@"floLatitude"];
    [dictionary setObject:[NSNumber numberWithDouble:lc.coordinate.longitude] forKey:@"floLongitude"];
    // send to server with a synchronous request


    // AFTER ALL THE UPDATES, close the task
    if (bgTask != UIBackgroundTaskInvalid)
    {
        [[UIApplication sharedApplication] endBackgroundTask:bgTask];
    }
}


Answer 2:

这些链接将帮助你...

iphone -连接到服务器在后台



文章来源: iOS HTTP request while in background