Deipayan Dash
Deipayan Dash

Reputation: 1

HTTP loading failed in iOS app

My app just wont print anything at all. I have added print statements but none of them print. Its a problem that occurred earlier too, but I ignored. Maybe I'm doing some HTTP request wrong, although I don't find any. Here is my code;

import UIKit

class ViewController: UIViewController {

    @IBOutlet var cityName: UITextField!

    @IBOutlet var results: UILabel!


    @IBAction func searchButton(_ sender: Any) {
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let url = URL(string: "http://www.weather-forecast.com/locations/KolKata/forecasts/latest")!
        let request = NSMutableURLRequest(url: url)
        let task = URLSession.shared.dataTask(with: request as URLRequest){
            data, response, error in

            if error != nil {
                print(error!)
            }
            else {
                if let unwrapped = data {
                    let datastring  = NSString(data: unwrapped, encoding: String.Encoding.utf8.rawValue)
                    print(datastring!)
                }
            }
        }
    }

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

If it maybe the security purposes I add the info.plist file for reference too:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
    <string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationPortraitUpsideDown</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>weather-forecast.com</key>
        <dict>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludeSubdomains</key>
            <true/> 
        </dict>
    </dict>
</dict>

Upvotes: 0

Views: 188

Answers (1)

Malleswari
Malleswari

Reputation: 427

The problem is not with info.plist you need to write task.resume(). Stop using force unwrap also

 guard let url = URL(string: "http://www.weather-forecast.com/locations/KolKata/forecasts/latest") else {return}
    let request = NSMutableURLRequest(url: url)
    let task = URLSession.shared.dataTask(with: request as URLRequest){
        data, response, error in

        if error != nil {
            print(error!)
        }
        else {
            if let unwrapped = data {
                if let datastring  = NSString(data: unwrapped, encoding: String.Encoding.utf8.rawValue){
                    print(datastring)
                }
            }
        }
    }
    task.resume()
}

Upvotes: 3

Related Questions