首页 文章

在尝试访问API时获得错误响应[重复]

提问于
浏览
0

这个问题在这里已有答案:

我尝试使用almofire将设备令牌和设备详细信息发布到Web服务 . 但我得到的回应如下

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

在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)
                    }
            }

请帮助我..

2 回答

  • -1

    您应该禁用App Transport Security .

    You have to set the NSAllowsArbitraryLoads key to YES under NSAppTransportSecurity dictionary in your .plist file. Hope this helps!

    enter image description here

  • 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
    enter image description here

相关问题