请帮帮我,我正在使用后台位置更新编码iOS并将位置数据发送到我的服务器。 下面是我的代码:
#import "AppDelegate.h"
@implementation AppDelegate
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@"Went to Background");
[locationManager startMonitoringSignificantLocationChanges];
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
UIApplication *app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier locationUpdateTaskID = [app beginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
if (locationUpdateTaskID != UIBackgroundTaskInvalid) {
[app endBackgroundTask:locationUpdateTaskID];
locationUpdateTaskID = UIBackgroundTaskInvalid;
}
});
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if ([[locations lastObject] horizontalAccuracy] < 100.0f) {
NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
mytoken=[userDefaults stringForKey:@"mytoken"];
if (mytoken==nil) {
mytoken=@"";
}
mybgtype=[userDefaults stringForKey:@"mybgtype"];
if (mybgtype==nil) {
mybgtype=@"all";
[userDefaults setObject:mybgtype forKey:@"mybgtype"];
[userDefaults synchronize];
}
CLLocation *newLocation = [locations lastObject];
CLLocation *oldLocation;
if (locations.count > 1) {
oldLocation = [locations objectAtIndex:locations.count-2];
} else {
oldLocation = nil;
}
NSString *NowLng=[[NSNumber numberWithFloat:newLocation.coordinate.longitude] stringValue];
NSString *NowLat=[[NSNumber numberWithFloat:newLocation.coordinate.latitude] stringValue];
UILocalNotification *scheduleAlert;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
scheduleAlert=[[UILocalNotification alloc] init];
scheduleAlert.applicationIconBadgeNumber=1;
scheduleAlert.fireDate=[NSDate dateWithTimeIntervalSinceNow:1];
scheduleAlert.timeZone=[NSTimeZone defaultTimeZone];
scheduleAlert.repeatInterval=NSDayCalendarUnit;
scheduleAlert.soundName=UILocalNotificationDefaultSoundName;
scheduleAlert.alertBody=NowLat;
[[UIApplication sharedApplication] scheduleLocalNotification:scheduleAlert];
if ([mybgtype isEqualToString:@"n"]) {
} else {
[self LoadTaskMyBg:newLocation];
}
}
dispatch_async(dispatch_get_main_queue(), ^{
if (locationUpdateTaskID != UIBackgroundTaskInvalid) {
[app endBackgroundTask:locationUpdateTaskID];
locationUpdateTaskID = UIBackgroundTaskInvalid;
}
});
});
}
-(void) LoadTaskMyBg:(CLLocation *)newLocation
{
NSString *mylat = [NSString stringWithFormat:@"%.8f", newLocation.coordinate.latitude];
NSString *mylng = [NSString stringWithFormat:@"%.8f", newLocation.coordinate.longitude];
NSLog(@"task 1");
receivedDataMyBg=[[NSMutableData alloc] initWithLength:0];
NSString * urlString;
urlString = @"https://www.xx.xx/xx.cshtml";
NSURL * url = [NSURL URLWithString:urlString];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];
[request setHTTPMethod:@"POST"];
NSString *parameterString=[NSString stringWithFormat:@"%@%@%@%@%@%@%@%@", @"pp=a&tt=",@"aa",@"&mylat=",mylat,@"&mylng=",mylng,@"&myalt=",@"0"];
NSData * postData = [parameterString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:postData];
connMyBg = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"task 1a");
NSLog(@"%@",parameterString);
if (connMyBg==nil) {
NSLog(@"task 1e");
return;
}
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace{
return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
//NSLog(@"received authen challenge");
NSLog(@"task challenge");
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
if (connection==connMyBg) {
[receivedDataMyBg setLength:0];
NSLog(@"task connect receive");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
//NSLog(@"got data %@", [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
if (connection==connMyBg) {
[receivedDataMyBg appendData:data];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"error");
if (connection==connMyBg) {
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (connection==connMyBg) {
NSLog(@"task 2");
NSString *response;
NSError *error;
response=[[NSString alloc] initWithData:receivedDataMyBg encoding:NSUTF8StringEncoding];
NSData* data = [response dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *JSONDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
if([[JSONDic objectForKey:@"bnum"] isEqualToString:@""])
{
} else {
UILocalNotification *scheduleAlert;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
scheduleAlert=[[UILocalNotification alloc] init];
scheduleAlert.applicationIconBadgeNumber=1;
scheduleAlert.fireDate=[NSDate dateWithTimeIntervalSinceNow:1];
scheduleAlert.timeZone=[NSTimeZone defaultTimeZone];
scheduleAlert.repeatInterval=NSDayCalendarUnit;
scheduleAlert.soundName=UILocalNotificationDefaultSoundName;
scheduleAlert.alertBody=@"TEST:OK";
}
NSLog(@"%@",[JSONDic objectForKey:@"bnum"]);
}
}
@end
我可以得到日志:“任务1a”!但为什么我不能接收“任务连接接收”???? 我该怎么办 ? 感谢。
答案 0 :(得分:0)
要使用NSURLConnection或CLLocationManager的委托,您需要将它们创建为具有强引用的属性。否则,将不会调用委托实现。由于您未提供声明可能存在问题的部分。
答案 1 :(得分:0)
NSString * pp = [NSString stringWithFormat:@&#34; xxxx.xx / xx.cshtml,newLocation.coordinate.latitude,newLocation.coordinate.longitude,0.0];
NSURL * response = [NSURL URLWithString:pp];
NSString * response_str = [NSString stringWithContentsOfURL:响应编码:NSUTF8StringEncoding错误:nil];
NSLog(@&#34;%@,报告的响应=%@&#34;,pp,response_str);
如果我如上所述重写didUpdateLocations中的代码, 我可以得到response_str,这是正确的! 但问题是:如何通过ssl https而不受挑战? 即使它已经做好了工作!