本文介绍了NSURLSession/NSURLConnection HTTP加载失败,-9802的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After Successfully running the program the error will be shows on debug area

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.

You have to add just the NSAllowsArbitraryLoads key to YES in NSAppTransportSecurity dictionary in your info.plist file.

例如,

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

这篇关于NSURLSession/NSURLConnection HTTP加载失败,-9802的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 04:40