gpt4 book ai didi

ios - 从 iOS 应用发出到 MS Excel 的深度链接

转载 作者:行者123 更新时间:2023-11-28 12:06:48 25 4
gpt4 key购买 nike

我正在尝试打开位于服务器上的 Excel 文档。我写了下面的代码,但它总是为 UIApplication.shared.canOpenURL(url as URL)

返回 false

我想我缺少一些深度链接到 Excel 的要求。为什么 iOS 无法理解 ms-excel:ofe|u| 格式?

@objc static func openExcel() {

let originalString = "http://s000.tinyupload.com/download.php?file_id=23290165129849240725&t=2329016512984924072514118"
let encodedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
let encodedURLString = "ms-excel:ofe|u|" + encodedString! + "|n|TestDoc.xlsx|a|App"

if let url = NSURL(string: encodedURLString),
UIApplication.shared.canOpenURL(url as URL) {
UIApplication.shared.openURL(url as URL)
} else if let itunesUrl = NSURL(string: "https://itunes.apple.com/us/app/microsoft-excel/id586683407?mt=8&uo=4"), UIApplication.shared.canOpenURL(itunesUrl as URL) {
UIApplication.shared.openURL(itunesUrl as URL)
}
}

最佳答案

我分析了你的代码,发现了一些错误。首先,您的 URL 被重定向到某个地方,根据 Microsoft 文档它无法处理重定向 URL

The URL has to be encoded and must be a direct link to the file (not a redirect). If the URL is in a format that Office cannot handle, or the download simply fails, Office will not return the user to the invoking application.

这里是 Microsoft Documentation Link

第二个错误是您只对包含站点 URL 的 URL 字符串进行编码,您应该将方案 ms-excel: 之后的部分视为 URL,并且应该对其进行编码。

由于编码不当,let url = URL(string: encodedURLString) 结果为 nil,这就是它无法按预期工作的原因。

这是一个示例工作代码:

 @objc static func openExcel() {
//replace the below url with yours. may be this one dosen't work
let originalString = "ofe|u|https://pgcconline.blackboard.com/webapps/dur-browserCheck-bb_bb60/samples/sample.xlsx"
let encodedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let encodedURLString = "ms-excel:" + encodedString!

if let url = URL(string: encodedURLString),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.openURL(url)
} else if let itunesUrl = NSURL(string: "https://itunes.apple.com/us/app/microsoft-excel/id586683407?mt=8&uo=4"), UIApplication.shared.canOpenURL(itunesUrl as URL) {
UIApplication.shared.openURL(itunesUrl as URL)
}
}

注意:从 iOS 9 开始,您必须在LSApplicationQueriesSchemes 键(一个字符串数组):

例如在我们的案例中:

enter image description here

关于ios - 从 iOS 应用发出到 MS Excel 的深度链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49266569/

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