我正在从服务器下载一个 XML 文件,为此我使用了以下代码:
NSURL *url = [NSURL URLWithString:link];
NSData *data = [NSData dataWithContentsOfURL:url]; // Load XML data from web
[data writeToFile:storePath atomically:TRUE];
现在如何检查链接是否是正确的 XML 链接?例如,如果他们填写 www.google.com,则来自该网页的数据将存储在 NSData *data
中。
我正在寻找一种方法来检查用户是否提供了正确的链接。
最佳答案
比如如果您的 XML 链接在运行时生成并且您想检查它是否针对 XML
if ([string rangeOfString:@".xml"].location == NSNotFound) {
NSLog(@"No Xml Link Found");
} else {
NSLog(@"Xml Link Found");
}
Case 1: string = @"http://www.google.com";
will Show this Log
NSLog(@"No Xml Link Found");
Case 2: string = @"http://www.google.com/live.xml";
will Show this Log
NSLog(@"Xml Link Found");
关于ios - 如何检查是否给出了正确的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16436850/