2010-11-30 22 views
1

多くのビューを持つアプリを開発しています。私のアプリでは、ユーザーはボタンをクリックして自分のポジションを尋ねることができるビューに到着することがあります。私はAppleのガイドラインに従って、ユーザーがそれを行うことができる場合にのみユーザーの位置を求めるようにしています。私は何をすべきか、次の最初のコードをアプリケーションデリゲートに使用し、ユーザーが呼び出すビューにロケーションマネージャー属性を宣言し、ロケーションマネージャー属性を新しいビューと古いビューから渡して、いつでも2番目の次のコードを要求しますユーザーがボタンをクリックして自分の位置を特定していることを示します。または、位置情報サービスが有効になっているかどうかを確認するために、ボタンを使用してユーザーの場所を取得できるビューにのみ位置マネージャー属性を宣言して、2番目のコードを使用しますか?iOSロケーションマネージャーを使って作業する

最初のスニペット。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    // Override point for customization after application launch. 

    // Add the navigation controller's view to the window and display. 
    [window addSubview:navigationController.view]; 
    [window makeKeyAndVisible]; 

    // Create a location manager instance to determine if location services are enabled. This manager instance will be 
    // immediately released afterwards. 
    CLLocationManager *manager = [[CLLocationManager alloc] init]; 
    if (manager.locationServicesEnabled == NO) { 
     UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [servicesDisabledAlert show]; 
     [servicesDisabledAlert release]; 
    } 
    [manager release]; 

    return YES; 
} 

第2スニペット。

- (IBAction)locateUser:(id)sender { 

    if([CLLocationManager locationServicesEnabled]) { 
     self.locationManager = [[[CLLocationManager alloc] init] autorelease]; 
     self.locationManager.delegate = self; 
    } else { 
     [[[[UIAlertView alloc] initWithTitle:@"Location services." 
            message:@"Location services are disabled." 
            delegate:nil 
          cancelButtonTitle:@"OK" 
          otherButtonTitles:nil] autorelease] show];  
    } 
} 

お読みいただきありがとうございます。

+0

誰も私を助けることはできませんか? –

答えて

1

CoreLocationはすべてのアラートを処理します。ロケーションサービスが無効になっていて、場所を尋ねると、CoreLocationは、Settings.appに直接行くためのボタンでユーザーに知らせる警告を表示します。

alt text

あなたはここでエラーは、ユーザーがアプリの使用をさせていない場合kCLErrorDeniedされるコードが含まれているデリゲート呼び出し

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

を確認することができます追加かを知りたい場合は位置情報サービス。

また、ユーザーが必要なときにCoreLocationを使用する必要があります。起動時に位置情報サービスを確認する必要はなく、複数のCLLocationManagerのオーバーヘッドはほとんど存在しません。

+0

次に、アプリケーションデリゲートにコードを含める必要はなく、場所ボタンを押したときの対応方法をチェックする必要はありません。私はちょうどCLLocationManagerを使用する任意のクラスに宣言しなければならない位置マネージャとビューにそれを開始クラスのメソッドをロードしましたか? Appleは最初のスニペットをLocateMeの例に使用します。 –

+0

私の答えを編集しました! – gcamp

+0

マップ上にユーザーの場所を表示するにはどうすればよいですか?ユーザーが自分のポジションを取得できない場合は、ユーザーの場所の表示をNOに設定する必要がありますか? –