ios - NSURLSession/NSURLConnection HTTP 加载失败,-9802

标签 ios xcode swift

程序成功运行后,错误将显示在调试区域

import UIKit
import CoreLocation

protocol ItemDetailViewControllerDelegate: class {
    func itemDetailViewControllerDidCancel(controller: ItemDetailViewController)
    func itemDetailViewController(controller: ItemDetailViewController, didFinishAddingItem item: NoToDoItem)
    func itemDetailViewController(controller: ItemDetailViewController, didFinishEditingItem item: NoToDoItem)

}

class ItemDetailViewController: UITableViewController, CLLocationManagerDelegate {

    @IBAction func myLocation(sender: AnyObject) {

        self.LocationManager.delegate = self
        self.LocationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.LocationManager.requestWhenInUseAuthorization()
        self.LocationManager.startUpdatingLocation()

    }

    let LocationManager = CLLocationManager()

    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var descriptionTextView: UITextView!
    @IBOutlet weak var doneBarButton: UIBarButtonItem!
    @IBOutlet weak var dueDateLabel: UILabel!

    weak var delegate: ItemDetailViewControllerDelegate?

    var itemToEdit: NoToDoItem?
    var dueDate = NSDate()
    var datePickerVisible = false

    override func viewDidLoad() {
        super.viewDidLoad()


        if let item = itemToEdit {
            title = "Edit Item"
            textField.text = item.text
            descriptionTextView.text = item.text
            dueDate = item.dueDate
            doneBarButton.enabled = true
        }

        updateDueDateLabel()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { (placemarks, error) -> Void in

            if (error != nil) {
                print("Error")
                return
            }

            if let pm = placemarks?.first
            {
                self.displayLocationInfo(pm)
            }
            else {
                print("errorData")
            }

        })

    }

    func displayLocationInfo(placemark: CLPlacemark){
        self.LocationManager.stopUpdatingLocation()

        print(placemark.locality)
        print(placemark.postalCode)
        print(placemark.administrativeArea)
        print(placemark.country)
    }

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("Error:" + error.localizedDescription)

    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        textField.becomeFirstResponder()
    }

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

        let oldText: NSString = textField.text!
        let newText: NSString = oldText.stringByReplacingCharactersInRange(range, withString: string)

        doneBarButton.enabled = (newText.length > 0)
        return true
    }

    @IBAction func done(sender: AnyObject) {
        if let item = itemToEdit {
            item.text = textField.text!

            textField.becomeFirstResponder()
            item.text = descriptionTextView.text!
            descriptionTextView.becomeFirstResponder()

            item.dueDate = dueDate

            delegate?.itemDetailViewController(self, didFinishEditingItem: item)
        } else {
            let item = NoToDoItem()
            item.text = textField.text!
            item.dueDate = dueDate

            delegate?.itemDetailViewController(self, didFinishAddingItem: item)
        }

    }

    @IBAction func cancel(sender: AnyObject) {
        delegate?.itemDetailViewControllerDidCancel(self)
    }

    override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
        if indexPath.section == 2 && indexPath.row == 1
        {
            return indexPath
        } else {
            return nil
        } 
    }

    func updateDueDateLabel() {
        let formatter = NSDateFormatter()
        formatter.dateStyle = .MediumStyle
        formatter.timeStyle = .ShortStyle
        dueDateLabel.text = formatter.stringFromDate(dueDate)
    }

    func showDatePicker() {

        datePickerVisible = true

        let indexPathDateRow = NSIndexPath(forRow: 1, inSection: 2)
        let indexPathDatePicker = NSIndexPath(forRow: 2, inSection: 2)

        if let dateCell = tableView.cellForRowAtIndexPath(indexPathDateRow)
        {
            dateCell.detailTextLabel!.textColor = dateCell.detailTextLabel!.tintColor
        }
        tableView.beginUpdates()
        tableView.insertRowsAtIndexPaths([indexPathDatePicker],withRowAnimation: .Fade)

        tableView.reloadRowsAtIndexPaths([indexPathDateRow], withRowAnimation: .None)

        tableView.endUpdates()

        if let pickerCell = tableView.cellForRowAtIndexPath( indexPathDatePicker) {
            let datePicker = pickerCell.viewWithTag(100) as! UIDatePicker
            datePicker.setDate(dueDate, animated: false)}
    }

    func hideDatePicker()
    {
        if datePickerVisible {
            datePickerVisible = false
            let indexPathDateRow = NSIndexPath(forRow: 1, inSection: 2)
            let indexPathDatePicker = NSIndexPath(forRow: 2, inSection: 2)
            if let cell = tableView.cellForRowAtIndexPath(indexPathDateRow) {
                cell.detailTextLabel!.textColor = UIColor(white: 0, alpha: 0.5)
            }
            tableView.beginUpdates()
            tableView.reloadRowsAtIndexPaths([indexPathDateRow], withRowAnimation: .None)
            tableView.deleteRowsAtIndexPaths([indexPathDatePicker], withRowAnimation: .Fade)
            tableView.endUpdates()
        }
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 2 && datePickerVisible { return 3
        } else {
            return super.tableView(tableView, numberOfRowsInSection: section)
        } 
     }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if indexPath.section == 2 && indexPath.row == 2 {
            return 217
        } else {
            return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
        } 
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // 1
        if indexPath.section == 2 && indexPath.row == 2 { // 2
            var cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("DatePickerCell")

            if cell == nil {
                cell = UITableViewCell(style: .Default,
                    reuseIdentifier: "DatePickerCell")
                cell.selectionStyle = .None
                // 3
                let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0,
                    width: 320, height: 216))
                datePicker.tag = 100
                cell.contentView.addSubview(datePicker)
                // 4
                datePicker.addTarget(self, action: Selector("dateChanged:"), forControlEvents: .ValueChanged)
            }
            return cell
            // 5
        } else {
            return super.tableView(tableView, cellForRowAtIndexPath: indexPath)
        }
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool{

        textField.resignFirstResponder()
        return true
    }

    func textFieldDidBeginEditing(textField: UITextField) {
        hideDatePicker()
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        textField.resignFirstResponder()


        if indexPath.section == 2 && indexPath.row == 1 { if !datePickerVisible {
            showDatePicker()
        }
        else
        {
            hideDatePicker()
            }
        }
    }

    override func tableView(tableView: UITableView, var indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int {
        if indexPath.section == 2 && indexPath.row == 2 {
            indexPath = NSIndexPath(forRow: 0, inSection: indexPath.section)
        }
        return super.tableView(tableView, indentationLevelForRowAtIndexPath: indexPath)
    }

    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool{

        if text == "\n"
        {
            descriptionTextView.resignFirstResponder()
            return false
        }
        return true
    }

    func dateChanged(datePicker: UIDatePicker) {
        dueDate = datePicker.date
        updateDueDateLabel()
    }
}

最佳答案

您必须在 info.plist 文件的 NSAppTransportSecurity 字典中将 NSAllowsArbitraryLoads 键添加为 YES。

例如,

 <key>NSAppTransportSecurity</key>
 <dict>
      <key>NSAllowsArbitraryLoads</key>
     <true/>
 </dict>

enter image description here

关于ios - NSURLSession/NSURLConnection HTTP 加载失败,-9802,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32755012/

相关文章:

iOS Today 扩展创建为 .app 而不是 .appex

swift - 如何从 Any 类型中解包一个可选值?

ios - 使用属性观察器更改 Collection View 单元格是否不好?

swift - 如何在 Firebase 中保存复杂的对象?

ios - 在 uitableview 添加索引在 Swift 2.0 中不起作用

ios - Xamarin Form,iOS,仅支持IPv6网络

ios - MKAnnotationView 和点击检测

ios - 标量属性在源存储库中除外

xcode - removePersistentDomainForName 不适用于 NSUserDefaults

iOS 6 vs 7 外观build设置