- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从我的应用程序集成 Siri。实际上,我的要求是与我的服务器进行一些 UDP 调用,但由于它没有发生,我尝试使用 URLSession 和 URLRequest 进行简单的网络调用。为了进行测试,我尝试访问 google.com 并打印响应,但从完成处理程序返回的 data
无法转换为 String
并且返回 nil。我需要在 Info.plist
文件或其他位置添加额外的内容才能使网络调用在 Intent Extension
中成功吗?
官方文档中也没有提及这一点。我使用的代码实际上很简单,但我仍然将其粘贴在下面:
let session = URLSession.shared
let task = session.dataTask(with: URLRequest(url: URL(string: "https://www.google.com/")!), completionHandler: { (tcpData, response, error) in
if error != nil {
print("tcp error \(error!.localizedDescription)")
}
if let res = response as? HTTPURLResponse {
print(res.statusCode)
}
if tcpData != nil {
print(String(data: tcpData!, encoding: String.Encoding.utf8) ?? "N/A")
}
})
task.resume()
completion(TurnOnNodeAppIntentResponse.init(code: TurnOnNodeAppIntentResponseCode.success, userActivity: nil))
这里的响应始终是nil
,尽管statusCode
是200
。
最佳答案
你的代码没有任何问题。并非所有数据都可以转换为字符串。您可以在this answer中阅读更多相关信息。 .仅点击 https://www.google.com/
不会返回字符串。但是,如果您确实想打印来自 https://www.google.com/
的数据字符串,只需使用 print(tcpData!.base64EncodedString())
你会得到类似的内容:PCFkb2N0eXBlIGh0bWw+PGh0bWwgaXRlbXNjb3BlPSIiIGl0ZW10eXBlPSJodHRwOi8vc....
将该 URL 切换到开放 API,例如 SpaceX API你会得到一个有效的字符串。
{
"flight_number": 83,
"mission_name": "Amos-17",
"mission_id": [
],
"launch_year": "2019",
"launch_date_unix": 1565131920,
"launch_date_utc": "2019-08-06T22:52:00.000Z",
"launch_date_local": "2019-08-06T18:52:00-04:00",
"is_tentative": false,
"tentative_max_precision": "hour",
"tbd": false,
"launch_window": 5280,
"rocket": {
"rocket_id": "falcon9",
"rocket_name": "Falcon 9",
"rocket_type": "FT",
"first_stage": {
"cores": [
{
"core_serial": "B1047",
"flight": 3,
"block": 5,
"gridfins": false,
"legs": false,
"reused": true,
"land_success": null,
"landing_intent": false,
"landing_type": null,
"landing_vehicle": null
}
]
},
"second_stage": {
"block": 5,
"payloads": [
{
"payload_id": "Amos-17",
"norad_id": [
],
"reused": false,
"customers": [
"Spacecom"
],
"nationality": "Israel",
"manufacturer": "Boeing Satellite Systems",
"payload_type": "Satellite",
"payload_mass_kg": 6500,
"payload_mass_lbs": 14330.05,
"orbit": "GTO",
"orbit_params": {
"reference_system": "geocentric",
"regime": "geostationary",
"longitude": 17,
"semi_major_axis_km": null,
"eccentricity": null,
"periapsis_km": null,
"apoapsis_km": null,
"inclination_deg": null,
"period_min": null,
"lifespan_years": 15,
"epoch": null,
"mean_motion": null,
"raan": null,
"arg_of_pericenter": null,
"mean_anomaly": null
},
"uid": "UerI6qmZTU2Fx2efDFm3QQ=="
}
]
},
"fairings": {
"reused": false,
"recovery_attempt": true,
"recovered": true,
"ship": "GOMSTREE"
}
},
"ships": [
"GOMSTREE",
"GONAVIGATOR"
],
"telemetry": {
"flight_club": null
},
"launch_site": {
"site_id": "ccafs_slc_40",
"site_name": "CCAFS SLC 40",
"site_name_long": "Cape Canaveral Air Force Station Space Launch Complex 40"
},
"launch_success": true,
"links": {
"mission_patch": "https://images2.imgbox.com/a0/ab/XUoByiuR_o.png",
"mission_patch_small": "https://images2.imgbox.com/f1/4a/WAkSmKfY_o.png",
"reddit_campaign": "https://www.reddit.com/r/spacex/comments/cjaawx/amos17_launch_campaign_thread",
"reddit_launch": "https://www.reddit.com/r/spacex/comments/cmedgn/rspacex_amos17_official_launch_discussion_updates",
"reddit_recovery": null,
"reddit_media": "https://www.reddit.com/r/spacex/comments/cmppne/rspacex_amos17_media_thread_videos_images_gifs",
"presskit": "https://www.spacex.com/sites/spacex/files/amos-17_mission_press_kit_8_6_2019.pdf",
"article_link": "https://spaceflightnow.com/2019/08/07/spacex-launches-israeli-owned-telecom-satellite/",
"wikipedia": "https://en.wikipedia.org/wiki/Spacecom",
"video_link": "https://youtu.be/fZh82-WcCuo",
"youtube_id": "fZh82-WcCuo",
"flickr_images": [
"https://live.staticflickr.com/65535/48478269312_58dd3dc446_o.jpg",
"https://live.staticflickr.com/65535/48478269747_353dcb2e62_o.jpg",
"https://live.staticflickr.com/65535/48478119901_2de0441026_o.jpg",
"https://live.staticflickr.com/65535/48478120646_ab72c2c6c3_o.jpg",
"https://live.staticflickr.com/65535/48478120031_5aae1f6131_o.jpg",
"https://live.staticflickr.com/65535/48478269442_08479bed36_o.jpg"
]
},
"details": "SpaceX will launch Boeing built Amos-17, a geostationary communications satellite for Israeli company Spacecom. The satellite will be delivered to GTO from KSC LC-39A or possibly CCAFS SLC-40, and will replace the defunct Amos-5 at 17° E. Amos-17 carries multi-band high throughput and regional beams servicing Africa, Europe and the Middle East. The cost of this launch is covered for Spacecom by SpaceX credit following the Amos-6 incident. A recovery of the booster for this mission is not expected.",
"upcoming": false,
"static_fire_date_utc": "2019-08-01T00:00:00.000Z",
"static_fire_date_unix": 1564617600,
"timeline": null,
"crew": null
}
此外,您可能想停止强制展开,而是使用 if let
或 guard
let session = URLSession.shared
let task = session.dataTask(with: URLRequest(url: URL(string: "https://api.spacexdata.com/v3/launches/latest")!), completionHandler: { (tcpData, response, error) in
if let err = error {
print("tcp error \(err.localizedDescription)")
}
if let res = response as? HTTPURLResponse {
print(res.statusCode)
}
if let data = tcpData {
print(String(data: data, encoding: String.Encoding.utf8) ?? "N/A")
}
})
task.resume()
completion(TurnOnNodeAppIntentResponse.init(code: TurnOnNodeAppIntentResponseCode.success, userActivity: nil))
关于ios - 如何从 Siri Intent Extension 进行网络调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58394076/
我是一名优秀的程序员,十分优秀!