2016-05-19 1 views
0

私はiOSアプリケーション開発で非常に新しいです。私はAFNetworking 3.0を使用するつもりです。私はバックグラウンドで場所を更新し、それをサーバーに送信したいと思います。サーバーに送信している間、私は条件を付けたい: 1)場所が変わらない場合は、私はサービスsendlocation1 を呼び出したいと思う2)もし私がサービスsendlocation2を呼び出したい場合。 場所の変更(約50メートル付近)AFN 3を使用して場所が変更された場合、または使用されない場合、バックグラウンドの場所をサーバーに定期的に送信する方法はありますか。

(void)viewDidLoad { 
[super viewDidLoad]; 

locationManager = [[CLLocationManager alloc] init]; 
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
[locationManager startUpdatingLocation]; 

[locationManager requestAlwaysAuthorization]; 



float Lat = locationManager.location.coordinate.latitude; 
float Long = locationManager.location.coordinate.longitude; 
NSLog(@"Lat : %f Long : %f",Lat,Long);} 

私は、サーバーからの応答を取得した場合のログインの成功は、その後にのみ、サーバーに場所を送信され、チェックするつもりです: は私がのviewDidLoadで ----次のコードを試してみました.... を助けてください

if ([_str isEqualToString:@"Login Success"]) { 

     UIAlertController *Loginalert= [UIAlertController 

              alertControllerWithTitle:@"Login Success" 

              message:@"This app is going to close now" 

              preferredStyle:UIAlertControllerStyleAlert]; 


     UIAlertAction* yesButton = [UIAlertAction 

            actionWithTitle:@"Ok" 

            style:UIAlertActionStyleDefault 

            handler:^(UIAlertAction * action) 

            { 
             if() { 
              [self sendlocation1]; 
             } else { 
              [self sendlocation2]; 
             } 

             [Loginalert dismissViewControllerAnimated:YES completion:nil]; 



            }]; 

     [Loginalert addAction: yesButton]; 




    } else { 

     NSLog(@"Something Wrong"); 

し、最終的に

(void)sendlocation1{ 
} 

(void)sendlocation2{ 
} 

私を助けてください...どのようにかどうかを確認するために:、私は、次のif-else statement.likeを使用する理由のthats場所の変更かどうか...条件を書いて、それをサーバーに送信する必要があります。

+0

コードを適切にフォーマットする必要があります。 – cst1992

+0

上記のコードで –

+0

を書くと、sendlocation1というサービスを呼び出す条件が書けるはずです。場所の変更、私はsendlocation2をしない場合sendlocation1を呼び出したい...仲間を助けてください –

答えて

1

ステップ-1

(void)viewDidLoad { 
[super viewDidLoad]; 

locationManager = [[CLLocationManager alloc] init]; 
//set the amount of metres travelled before location update is made 
[locationManager setDistanceFilter:50]; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
[locationManager startUpdatingLocation]; 

[locationManager requestAlwaysAuthorization]; 

// call the timer with 5 minutes cap using 5 * 60 = 300 
[NSTimer scheduledTimerWithTimeInterval:300.0f target:self selector:@selector(sendlocation1) userInfo:nil repeats:YES]; 

ステップ-2

すべて50メートル変更装置このメソッドが呼び出されます。

-(void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"%f",newLocation.coordinate.latitude); 

    NSLog(@"%f",newLocation.coordinate.longitude); 
    CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation]; 
     if(meters >=50) 
     { 
     // call webservice for location is updated 
     [self sendlocation1]; 
     }else 
     { 
     // call normal method 
     [self webservice_UpdateLocation]; 
     } 
    } 
} 

-(void)webservice_UpdateLocation 
{ 
    if ([_str isEqualToString:@"Login Success"]) { 

     UIAlertController *Loginalert= [UIAlertController 

              alertControllerWithTitle:@"Login Success" 

              message:@"This app is going to close now" 

              preferredStyle:UIAlertControllerStyleAlert]; 


     UIAlertAction* yesButton = [UIAlertAction 

            actionWithTitle:@"Ok" 

            style:UIAlertActionStyleDefault 

            handler:^(UIAlertAction * action) 

            { 



              [self sendlocation2]; 


             [Loginalert dismissViewControllerAnimated:YES completion:nil]; 



            }]; 

     [Loginalert addAction: yesButton]; 




    } else { 

     NSLog(@"Something Wrong"); 
} 
+0

シングメソッドは十分ですbro、その50メートルの距離ごとに呼び出されます –

+0

場所が変更されたり、(私はその2のサービスを書く理由は、5分または10分) (sendlocation1 n sendlocation2)......どのように私はその弟をすることができますか... ...私の問題を解決してください –

+0

@SurajSukale - 更新された回答兄をチェックし、私はNSTimerの概念を使用して、単一のwebserviceは十分です –

0

CLLocationを使用して2(coord_A、coord_B)座標の変位を確認できます。

CLLocationDistance distance = [coord_A distanceFromLocation:coord_B]; 

受信距離はメートルです。取得した距離でIf-Elseループで条件を実行できます。

+0

@ 7vikram7 ...どうか私に言うことができますか、私はライト(coord_Aとcoord_B)..お願いします。 –

+0

これらはCLLocationオブジェクトです。それらの1つはユーザーの古い場所であり、他の1つは新しい場所です。これらのオブジェクトは、「locationManager:didUpdateToLocation:fromLocation:」メソッドで取得します。 – 7vikram7

関連する問題