如何使用 iPhone/iOS 编程(Objective-c)使用 wifi/3g IP 地址检索当前城市名称?

发布于 2024-10-30 15:03:26 字数 496 浏览 3 评论 0原文

我想使用设备的 IP 地址(wifi/3g)检索 iPhone 当前的“城市名称”。有什么办法可以做到吗?请帮帮我。

以下是我想要当前位置的地方。

-(BOOL)fetchAndParseRss{

NSString * CurrentLocation = <I want the retrieved Location here!!>;
NSString * URLrss = [NSString stringWithFormat:@"http://news.google.com/news?q=location:%@&output=rss", CurrentLocation];

NSURL *url = [NSURL URLWithString:URLrss]; 
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
}

实际上我正在为自己开发一个应用程序,显示当前城市的新闻。

I want to retrieve the current "city name" of the iPhone using ip address of device (wifi/3g). Is there any way to do it? please help me out.

Following is the place where I want the current location.

-(BOOL)fetchAndParseRss{

NSString * CurrentLocation = <I want the retrieved Location here!!>;
NSString * URLrss = [NSString stringWithFormat:@"http://news.google.com/news?q=location:%@&output=rss", CurrentLocation];

NSURL *url = [NSURL URLWithString:URLrss]; 
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
}

Actually i am developing an app for myself that displays the news of current city.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小鸟爱天空丶 2024-11-06 15:03:26

您可以使用CLLocationManagerMKReverseGeocoder来获取用户所在城市。

首先添加CLLocationManager并开始更新用户位置

- (void)startLocationManager 
{    
    if (locManager==nil)
        locManager = [[CLLocationManager alloc] init];
    if (locManager.locationServicesEnabled)
    {
        NSLog(@"User has opted out of service on it error");
    } else {
        locManager.delegate = self;
        locManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
        [locManager startUpdatingLocation];
    }
}

然后添加位置管理器委托方法以使用MKReverseGeocoder

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"Manager error: %@", [error description]);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    self.geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
    [geocoder release];
    geocoder.delegate = self;
    [geocoder start];
    [manager stopUpdatingLocation];
}

当然,您必须实现 MKReverseGeocoder 委托方法

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSLog(@"reverseGeocoder:%@ didFailWithError:%@", geocoder, error);
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{   
    // with the placemark you can now retrieve the city name
    NSString *city = [placemark.addressDictionary objectForKey:@"City"];
    NSLog(@"City is %@", city);
}

如果用户向您的应用程序授予位置管理器的权限,则可以使用此方法。

You can use CLLocationManager and MKReverseGeocoder to get users city.

First add CLLocationManager and start updating user location

- (void)startLocationManager 
{    
    if (locManager==nil)
        locManager = [[CLLocationManager alloc] init];
    if (locManager.locationServicesEnabled)
    {
        NSLog(@"User has opted out of service on it error");
    } else {
        locManager.delegate = self;
        locManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
        [locManager startUpdatingLocation];
    }
}

Then add location manager delegate methods to use MKReverseGeocoder.

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"Manager error: %@", [error description]);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    self.geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
    [geocoder release];
    geocoder.delegate = self;
    [geocoder start];
    [manager stopUpdatingLocation];
}

and of course you have to implement MKReverseGeocoder Delegate methods

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSLog(@"reverseGeocoder:%@ didFailWithError:%@", geocoder, error);
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{   
    // with the placemark you can now retrieve the city name
    NSString *city = [placemark.addressDictionary objectForKey:@"City"];
    NSLog(@"City is %@", city);
}

This method can be use if user gives permission to your application for location manager.

帅的被狗咬 2024-11-06 15:03:26

一种选择是向 geoiptool.com 等网站发出 HTTP 请求,并从响应中抓取城市信息。不过,我不确定使用移动电话网络时您会取得多大成功——当我在手机上尝试时,wifi 可以工作,但 3G 却不行。

One option would be to make an HTTP request to a website like geoiptool.com and scrape the city information from the response. I'm unsure how much success you'll have when using mobile phone networks, though—wifi worked when I tried it on my phone, but 3G didn't.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文