iOS应用中的HTTP加载失败

时间:2018-06-12 04:22:19

标签: ios iphone swift xcode

我的应用程序根本不会打印任何东西。我添加了print语句,但没有打印。这是一个早期发生的问题,但我忽略了。也许我做错了一些HTTP请求,虽然我找不到。这是我的代码;

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.
    }
}

如果出于安全考虑,我可以添加info.plist文件以供参考:

<?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>

1 个答案:

答案 0 :(得分:3)

问题不在于您需要编写task.resume()的info.plist。停止使用force unwrap

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