gpt4 book ai didi

ios - 是否可以在未经 iOS 许可的情况下获取用户大致位置?

转载 作者:行者123 更新时间:2023-12-01 18:03:41 26 4
gpt4 key购买 nike

您会如何建议在未经用户许可的情况下获取用户所在的国家/地区?是否可以使用蜂窝塔三角测量或 WiFi 定位来获得大致位置?你有什么其他的方法来猜测用户所在的国家/地区?

最佳答案

来自苹果的文档

Important: In addition to hardware not being available, the user has the option of denying an application’s access to location service data. During its initial uses by an application, the Core Location framework prompts the user to confirm that using the location service is acceptable. If the user denies the request, the CLLocationManager object reports an appropriate error to its delegate during future requests. You can also check the application’s explicit authorization status using the authorizationStatus method.

因此,即使存在一些棘手的方法来避免许可,您也会在审核阶段被拒绝。


What other way do you suggest to guess what country the user is located?

您可以通过使用 ip 地址获取此信息。


Swift 4 - Get device's IP Address:

添加 #include<ifaddrs.h>在您的桥接头中。

这是获取 IP 地址所需的框架。

class func getIPAddress() -> String? {
var address: String?
var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil
if getifaddrs(&ifaddr) == 0 {
var ptr = ifaddr
while ptr != nil {
defer { ptr = ptr?.pointee.ifa_next }

let interface = ptr?.pointee
let addrFamily = interface?.ifa_addr.pointee.sa_family
if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {

if let name: String = String(cString: (interface?.ifa_name)!), name == "en0" {
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
getnameinfo(interface?.ifa_addr, socklen_t((interface?.ifa_addr.pointee.sa_len)!), &hostname, socklen_t(hostname.count), nil, socklen_t(0), NI_NUMERICHOST)
address = String(cString: hostname)
}
}
}
freeifaddrs(ifaddr)
}
return address
}

Get country using ip address

您可以先尝试http://ip-api.com/json ,它返回一个 JSON 解释 on their API page .

然后您可以将此 JSON 字符串转换为字典并访问数据。

func getIpLocation(completion: @escaping(NSDictionary?, Error?) -> Void)
{
let url = URL(string: "http://ip-api.com/json")!
var request = URLRequest(url: url)
request.httpMethod = "GET"

URLSession.shared.dataTask(with: request as URLRequest, completionHandler:
{ (data, response, error) in
DispatchQueue.main.async
{
if let content = data
{
do
{
if let object = try JSONSerialization.jsonObject(with: content, options: .allowFragments) as? NSDictionary
{
completion(object, error)
}
else
{
// TODO: Create custom error.
completion(nil, nil)
}
}
catch
{
// TODO: Create custom error.
completion(nil, nil)
}
}
else
{
completion(nil, error)
}
}
}).resume()
}

此函数返回字典或错误(在您解决 TODO 之后)。 completion在主线程上调用,假设您将使用结果来更新 UI。如果没有,您可以删除 DispatchQueue.main.async { } .

关于ios - 是否可以在未经 iOS 许可的情况下获取用户大致位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61105527/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com