1

我正在编写一个应用程序来将 gps 坐标发送到服务器。我需要每 30 秒发送一次坐标。我正在使用一个按钮来启动该发送请求,但我无法按照要求每 30 秒重新发送一次坐标的计时器或循环过程。有任何想法吗?发布一些代码来帮助:

-(IBAction)pushedGo:(id)sender
{

    NSMutableData *data = [[NSMutableData alloc]init];
    self.receivedData = data;
    [data release];




    NSString *contentType = @"text/html";
    NSString *mimeType = @"text/xml";

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://telesto.zapto.org:81/SMART_EdgeNode/EdgeNode/DataFeeds/3/addMeasurement"]];





    NSString *string1 = [ NSString stringWithFormat:@"%f", locationController.dblLatitude];
    NSString *string2 = [ NSString stringWithFormat:@"%f", locationController.dblLongitude];

    NSLog(@"%@",string1);
    NSLog(@"%@",string2);

    NSString *bodystring =[NSString stringWithFormat:@"geoX#%@#geoY#%@", string1, string2];

    NSData *body = [bodystring dataUsingEncoding:NSUTF8StringEncoding];

    NSLog(@"%@",bodystring);





    NSMutableDictionary* headers = [[[NSMutableDictionary alloc] init] autorelease];
    [headers setValue:contentType forKey:@"Content-Type"];
    [headers setValue:mimeType forKey:@"Accept"];
    [headers setValue:@"no-cache" forKey:@"Cache-Control"];
    [headers setValue:@"no-cache" forKey:@"Pragma"];
    [headers setValue:@"close" forKey:@"Connection"];






    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:60.0];
    [request setHTTPMethod:@"PUT"];

    [request setAllHTTPHeaderFields:headers];


    [request setHTTPBody:body];

    self.conn = [NSURLConnection connectionWithRequest:request delegate:self];
 }

这就是我用来通过点击我的应用程序上的按钮来发起请求的代码。是否有可能在点击该按钮后使其不只发送 1 个请求,而是每 30 秒发送许多请求,直到我退出应用程序或我点击另一个按钮停止发送请求?

提前致谢!

4

1 回答 1

2

使用NSTimer

[NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(request:) userInfo:nil repeats:YES];
于 2012-10-25T09:24:20.587 回答