等到有移动互联网在Objective-C上发布数据

时间:2014-08-22 09:46:56

标签: ios objective-c parse-platform cllocationmanager

我是Objective-C开发的新手,所以请耐心等待!

我正在尝试从用户那里获取数据&将该信息发送到外部数据库。我已经设法解决了如何轻松推送数据,但问题是;由于我的应用程序的性质,用户很可能在他们使用它时没有移动连接。如何持续检查用户是否具有移动连接,然后在连接时发送数据?我的行动代码如下:

(只是为了澄清,该动作在5秒钟内读取10个信号读数,将它们附加到阵列,计算平均值并更新位置。反过来,locationManager将数据发送到云服务,包括平均信号读数。

    - (IBAction)buttonPressed:(id)sender {

            // Make Manager Delegate & Set Accuracy
            manager.delegate = self;
            manager.desiredAccuracy = kCLLocationAccuracyBest;

            // Call Timer
            myTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
                                                       target:self
                                                     selector:@selector(arrayBuild)
                                                     userInfo:nil
                                                      repeats:YES];

            // Initialise Array
            resultsArray = [[NSMutableArray alloc]init];

    }




    #pragma mark CLLocationManagerDelegate Methods

    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {

            NSLog(@"Error: %@", error);
            NSLog(@"Failed to get location!");

    }


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

            NSLog(@"Location: %@",newLocation);

            if (curentLocation != nil) {

                // Code below uses a third party utility to submit data
                PFObject *Object = [PFObject objectWithClassName:@"Issue"];
                Object[@"Latitude"] = [NSString stringWithFormat:@"%.8f",curentLocation.coordinate.latitude];
                Object[@"Longitude"] = [NSString stringWithFormat:@"%.8f",curentLocation.coordinate.longitude];
                Object[@"Signal"] = [NSString stringWithFormat:@"%@",_avgNumber];
                [Object saveInBackground];

                // Update Text Fields
                self.latitude.text = [NSString stringWithFormat:@"%.8f",curentLocation.coordinate.latitude];
                self.longitude.text = [NSString stringWithFormat:@"%.8f",curentLocation.coordinate.longitude];
                self.signal.text = [NSString stringWithFormat:@"%@",_avgNumber];

            }

        // Stop the Location Update
        [manager stopUpdatingLocation];

    }

- (void)arrayBuild {

        loopCount++;

        if (loopCount >= 11) {

            // Find Average
            NSNumber *avg = [resultsArray valueForKeyPath:@"@avg.self"];
            _avgNumber = avg;

            // Change Text of Label to Average & Log
            self.signal.text = [NSString stringWithFormat:@"%@",_avgNumber];
            NSLog(@"%@",_avgNumber);

            // Update Location
            [manager startUpdatingLocation];

            // Invalidate Timer
            [myTimer invalidate];
            myTimer = nil;

        }else{

            // Declare Signal Strength
            float signalstrength = CTGetSignalStrength();

            // Individual Result & Convert to Integer
            NSString *result = [NSString stringWithFormat:@"%f", signalstrength];
            NSInteger resultInt = [result integerValue];

            // Add Object
            [resultsArray addObject:[NSNumber numberWithFloat:resultInt]];

            // Change Text of Label Each Second
            self.signal.text = [NSString stringWithFormat:@"%d",loopCount];
            NSLog(@"%f",signalstrength);

        }

    }

2 个答案:

答案 0 :(得分:3)

您需要的是“网络可达性监控”。订阅后,当网络连接状态发生变化(即设备变为在线或离线),甚至是当前连接类型(WLAN或WWAN)时,您将收到通知。

Apple提供了一个样本project,第三方网络库(例如AFNetworking)经常提供便利类,以获得更好的体验。

编辑:更简单的解决方案是使用Parse SDK及其saveEventually方法而不是saveInBackground。根据文档,这将处理无法访问网络的情况。

答案 1 :(得分:0)

论坛上有很多问题要解决这个问题。 Apple为此提供了Reachability课程。您可以查看Thisthis问题以获得进一步说明。