从 iOS 应用发布到 MS Excel 的深层链接
Issue deep linking to MS Excel from iOS app
我正在尝试打开位于服务器上的 Excel 文档。我写了下面的代码,但它总是 returns false for UIApplication.shared.canOpenURL(url as URL)
我想我缺少一些深度链接到 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 in the question above I get redirected to this URL 时,我的猜测是您的代码没有问题,可能只是您尝试打开的 excel 文件实际上是一个 HTML 页面,因为 tinyupload
显然阻止了直接 link 到文件。
也许尝试打开直接 excel 文件下载 link、https://pgcconline.blackboard.com/webapps/dur-browserCheck-bb_bb60/samples/sample.xlsx
(这是“xlsx file sample download
”的第一个 google 结果)
我分析了你的代码,发现了一些错误。首先,您的 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 你必须将你的应用程序想要在Info.plist
中查询的任何URL方案列入[=15]下的白名单=] 键(字符串数组):
例如我们的案例:
我正在尝试打开位于服务器上的 Excel 文档。我写了下面的代码,但它总是 returns false for UIApplication.shared.canOpenURL(url as URL)
我想我缺少一些深度链接到 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 in the question above I get redirected to this URL 时,我的猜测是您的代码没有问题,可能只是您尝试打开的 excel 文件实际上是一个 HTML 页面,因为 tinyupload
显然阻止了直接 link 到文件。
也许尝试打开直接 excel 文件下载 link、https://pgcconline.blackboard.com/webapps/dur-browserCheck-bb_bb60/samples/sample.xlsx
(这是“xlsx file sample download
”的第一个 google 结果)
我分析了你的代码,发现了一些错误。首先,您的 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 你必须将你的应用程序想要在Info.plist
中查询的任何URL方案列入[=15]下的白名单=] 键(字符串数组):
例如我们的案例: