在NSURL中传递NSString会导致应用程序崩溃

时间:2014-04-07 06:51:58

标签: iphone nsstring nsurl

我正在将NSString传递给NSURL并且应用程序崩溃了。我认为这是由于NSString

中使用的

NSString的NSLog是

http://www.test.com/?AssessmentID=040714114412 &QuestionID=113&ResponseText=yes&AssessmentName=Housekeeping&AssessmentDate=11:44:21&AssessmentQuestion=Use the guest’s or employee’s name.&ResponseComment=No comment&DepartmentID=9&SectionName=Service Standards

这是例外

[NSURL initWithString:relativeToURL:]: nil string parameter'

我认为由于客人的

而崩溃了

2 个答案:

答案 0 :(得分:1)

问题是您的网址编码不正确。拿这个&相应地修改你的代码。

NSString *relativeURL = @"http://www.something with Space and parameters";
NSURL *url = [NSURL URLWithString:[relativeURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];

这是正确的方法,而不是用%20

替换空格

希望有所帮助。

答案 1 :(得分:0)

我尝试了以下代码,它对我有用......你可以按照以下代码执行此操作!

NSString *relativeToURL = @"http://www.test.com/?AssessmentID=040714114412 &QuestionID=113&ResponseText=yes&AssessmentName=Housekeeping&AssessmentDate=11:44:21&AssessmentQuestion=Use the guest’s or employee’s name.&ResponseComment=No comment&DepartmentID=9&SectionName=Service Standards";
NSURL *url =     [[NSURL alloc] initWithString:relativeToURL];

[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
相关问题