运维开发网

objective-C语言 UIApplication openURL:不适用于相对URL

运维开发网 https://www.qedev.com 2020-03-19 10:33 出处:网络 作者:运维开发网整理
这是代码: NSURL *newsUrl = [NSURL URLWithString:@"/Document/News/1593" relativeToURL:[NSURL URLWithString:@"http://exist.ru"]]; // Outputs "http://exist.ru/Document/News/1593" NSLog(@"%@", [newsUrl absol
这是代码:

NSURL *newsUrl = [NSURL URLWithString:@"/Document/News/1593" relativeToURL:[NSURL URLWithString:@"http://exist.ru"]];

// Outputs "http://exist.ru/Document/News/1593"
NSLog(@"%@", [newsUrl absoluteString]);

// works
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[newsUrl absoluteString]]];

// doesn't work
//[[UIApplication sharedApplication] openURL:newsUrl];

这是Apple的错误吗?

在我的NSLog的Xcode输出中(@“NEW%@”,newsUrl)我将newUrl声明为

NSURL *newsUrl = [NSURL URLWithString:@"/Document/News/1593" relativeToURL:[NSURL URLWithString:@"http://exist.ru"]]:

NSLog输出是

/Document/News/1593 -- http://exist.ru

但对于[newsUrl absoluteString]

NSLog输出是

http://exist.ru/Document/News/1593

所以我猜[URLWithString:relativeToURL:]正在以不同的格式提供URL.这是您的结果无效的原因.

0

精彩评论

暂无评论...
验证码 换一张
取 消