打开"data:xxx" Safari中的URL [英] Opening "data:xxx" URLs in Safari

查看:176
本文介绍了打开"data:xxx" Safari中的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在iOS/Safari中打开数据URL:.

I'm failing to open data URL:s in iOS/Safari.

NSString* stringUrl = @"data:text/html;base64,PGh0bWw+DQo8Ym9keT4NCjxzY3JpcHQgbGFuZ3VhZ2U9IkphdmFTY3JpcHQiPg0KZG9jdW1lbnQubG9jYXRpb249J2h0dHA6Ly93d3cuZ29vZ2xlLmNvbSc7DQo8L3NjcmlwdD4NCjwvYm9keT4NCjwvaHRtbD4=";
NSURL* url = [NSURL URLWithString:stringUrl];
NSLog(@"Got url: %@", url);
const BOOL STATUS = [UIApplication.sharedApplication openURL:url];
NSLog(@"STATUS: %d", STATUS);

应该在iOS设备上打开Safari.结果页面将是:

This is supposed to open Safari on an iOS device. The resulting page would be:

<html>
<body>
<script language="JavaScript">
document.location='http://www.google.com';
</script>
</body>
</html>

但是openURL失败,STATUS返回NO/0.我们该如何解决?

However openURL fails, STATUS returns NO/0. How do we fix this?

推荐答案

您可以使用UIApplication的特殊方法来确定它是否可以打开您的URL

You can use special method of UIApplication to determine if it can open your URL or not

布尔状态= [UIApplication.sharedApplication canOpenURL :url];

BOOL STATUS = [UIApplication.sharedApplication canOpenURL:url];

但是,在文档中没有提到UIApplication是否能够使用数据"方案打开URL:

However, there is no mention in documentation if UIApplication is capable to open URL with "data" scheme:

"UIKit支持许多方案,包括http,https,tel,facetime和mailto方案." -来自UIApplication的文档.

"UIKit supports many schemes, including http, https, tel, facetime, and mailto schemes." - from documentation for UIApplication.

您可以尝试在内部UIWebView浏览器中打开此URL,并查看此UIWebView方法的用法:

You can attempt to open this URL in internal UIWebView browser and see how it goes with this method of UIWebView:

- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)encodingName baseURL:(NSURL *)baseURL;

这篇关于打开"data:xxx" Safari中的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆