【问题标题】:Getting error response while Attempt to hit a API [duplicate]尝试访问 API 时获得错误响应 [重复]
【发布时间】:2016-07-07 15:06:39
【问题描述】:

我尝试使用 almofire 将设备令牌和设备详细信息发布到 Web 服务。但我收到如下回复

UserInfo={NSUnderlyingError=0x7fba3a590e50 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}, NSErrorFailingURLStringKey=http://----/register, NSErrorFailingURLKey=http://---/register, NSLocalizedDescription=资源无法加载,因为应用传输安全政策要求使用安全连接。

在Objective c中,我尝试将所有值设置为httpHeader,在swift中我使用了以下代码..

let params = ["notification_token":deviceToken,"device_identifier":deviceIdentifier,"advertising_identifier":"","model":deviceModel,"os":deviceOS,"os_version":deviceOSVersion,"app_version":deviceAppVersion]

let jsonData = try! NSJSONSerialization.dataWithJSONObject(params, options: [])

request(.POST, urlString, parameters: params,encoding:.JSON).responseJSON
                {
                    response in

                    if let JSON = response.result.value
                    {
                        // print("A JSON Result :\(JSON)")
                        delegate.API_CALLBACK_RegisterApp!(JSON as! NSDictionary)
                    }
                    else
                    {
                        delegate.API_CALLBACK_Error(0,errorMessage: response.result.error!.description)
                    }
            }

请帮助我..

【问题讨论】:

  • 在您的应用中禁用应用传输安全性,或将特定网址列入白名单
  • 谢谢兄弟,工作正常:)
  • 现在它工作正常且不安全。

标签: ios swift


【解决方案1】:
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
</plist>

将此密钥添加到您的 info.plist

【讨论】:

    【解决方案2】:

    您应该禁用 App Transport Security。

    您必须在 .plist 文件中的 NSAppTransportSecurity 字典下将 NSAllowsArbitraryLoads 键设置为 YES。希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-30
      • 1970-01-01
      • 2016-06-27
      • 1970-01-01
      相关资源
      最近更新 更多