gpt4 book ai didi

ios - iOS 应用中的 HTTP 加载失败

转载 作者:行者123 更新时间:2023-11-28 10:40:17 26 4
gpt4 key购买 nike

我的应用程序根本不会打印任何东西。我添加了打印语句,但没有打印出来。之前也出现过这个问题,但是我忽略了。也许我做错了一些 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>

最佳答案

问题不在于 info.plist,您需要编写 task.resume()。也停止使用强制解包

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

关于ios - iOS 应用中的 HTTP 加载失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50809211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com