objective-c – UIApplication openURL:不适用于相对URL

这是代码:

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.这是您的结果无效的原因.

相关文章

一.C语言中的static关键字 在C语言中,static可以用来修饰局...
浅谈C/C++中的指针和数组(二) 前面已经讨论了指针...
浅谈C/C++中的指针和数组(一)指针是C/C++...
从两个例子分析C语言的声明 在读《C专家编程》一书的第三章时...
C语言文件操作解析(一)在讨论C语言文件操作之前,先了解一下...
C语言文件操作解析(三) 在前面已经讨论了文件打开操作,下面...