How to get the user's current city name withou

2019-06-26 01:15发布

So the original requirements was that on the launch of the app, the mobile app will get the current location of the user precise to City. Based on the city, the server/app client will give-city related content.

I know it would not be too difficult if I could use the core location service (since you know suspicious (self-concerned) users usually will disable the location service for a not-so-trusting app).

I have searched the web for few hours and get an summary of following solutions where I need your help to decide which one could be my best option or if you have any other better ones.

Using the [NSlocale autoupdatingCurrentLocale]

cons:

  • a.The explanation of the return value of the call from apple's document: "The locale is formed from the settings for the current user’s chosen system locale overlaid with any custom settings the user has specified in System Preferences." From which I don't understand what is "custom settings the user has specified in System Preferences" reference link

  • b. This value could be misleading and not related with user's actual current city/country at all.

comments: I would be appreciated if anybody could explain me how the locale is changed according to user's setting.

Getting the current iPhone TimeZone

NSTimeZone *localTime = [NSTimeZone systemTimeZone];
NSLog(@"Current local timezone  is  %@",[localTime name]); 

cons:

  • a. same as item b above, because user can always specify rather than let the system auto update the time zone.
  • b. precision is comprimised, only capital city will be listed.

comments:Not very promising but easy to implement

Using IP based location detection webservice Based on my reading, the process will involve the app sending a request to a server where the server will record the IP of the request.(Don't think from App itself could get its own IP correctly since it may inside a local network) Based on the IP recorded through some third party service (with a IP db or some other available APIs), it will return the city information back to the client.

cons:

  • a. I've never implemented such thing, don't know if it is feasible for a mobile app.
  • b. Even if it is possible, don't know if it is faster than the core location service.

comments: If you know it is possible for a iOS app to do so please let me know.

Please kindly give me any thoughts you have or suggest any better solution if you know.

Thank you very much.

2条回答
放我归山
2楼-- · 2019-06-26 01:47

NSlocale can be highly inaccurate. My colleague bought the iPhone from New Zealand (he was studying there). When I am testing his phone to get the locale for language, it still show as New Zealand English.

Using IP is possible. I made a quick search and I found:-

  1. Find IP address in iphone

  2. how to get ip address of iphone programmatically

You might want to take a good read from above links.

I still think using Core Location is the best way to get the location of the user.

查看更多
爷、活的狠高调
3楼-- · 2019-06-26 02:00

A preventive measure to avoid all your trouble would be to create an app that wouldn't appear 'not-so-trusting' and use core location to get the location. If a user still does not allow your app to know the location and knowing his city is a must, let him select a city from the list of cites for which content is available. Of course this opens up a huge hole allowing use to get the content for ANY city. Only you can decide if this is an acceptable tradeoff for simpler implementation.

If you must know the CITY automatically without core location, the first two options you mentioned, 'locale' and 'time zone' are useless as each of them encompasses large number of cities. Also, as you mentioned they're dependent on user's settings.

So reverse geocoding the IP address of a web service request using GeoIP or some such service is your best bet. You can find a lot of discussion and references for this on the web.

查看更多
登录 后发表回答