Objective c 调用UIWebView在iOS中崩溃我的应用程序
我的故事板中有一个网络视图。我在视图控制器文件中为该webview创建了一个outlet属性。然后在我的代码中,我这样称呼它:Objective c 调用UIWebView在iOS中崩溃我的应用程序,objective-c,ios7,uiwebview,Objective C,Ios7,Uiwebview,我的故事板中有一个网络视图。我在视图控制器文件中为该webview创建了一个outlet属性。然后在我的代码中,我这样称呼它: - (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"news1" ofType:@"html" inDirectory:@"/HTML"]]; [self.webView loadRe
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"news1" ofType:@"html" inDirectory:@"/HTML"]];
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}
问题是当我运行这个程序时,我的应用程序崩溃了,我真的不知道为什么。我得到这个错误,但我需要帮助破译问题是什么
2014-05-20 20:18:10.214 MyApp[1789:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
*** First throw call stack:
(
0 CoreFoundation 0x01f7b1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x019fe8e5 objc_exception_throw + 44
2 CoreFoundation 0x01f7afbb +[NSException raise:format:] + 139
3 Foundation 0x0161a92b -[NSURL(NSURL) initFileURLWithPath:] + 123
4 Foundation 0x0161a7fd +[NSURL(NSURL) fileURLWithPath:] + 67
5 MyApp 0x00011c3d -[LVGTutorialVC viewDidLoad] + 205
6 UIKit 0x007dd33d -[UIViewController loadViewIfRequired] + 696
7 UIKit 0x00803345 -[UINavigationController _layoutViewController:] + 39
8 UIKit 0x0080385b -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 235
9 UIKit 0x00803953 -[UINavigationController _startTransition:fromViewController:toViewController:] + 78
10 UIKit 0x008048cc -[UINavigationController _startDeferredTransitionIfNeeded:] + 645
11 UIKit 0x008054e9 -[UINavigationController __viewWillLayoutSubviews] + 57
12 UIKit 0x009460d1 -[UILayoutContainerView layoutSubviews] + 213
13 UIKit 0x0072d964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
14 libobjc.A.dylib 0x01a1082b -[NSObject performSelector:withObject:] + 70
15 QuartzCore 0x01de245a -[CALayer layoutSublayers] + 148
16 QuartzCore 0x01dd6244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
17 QuartzCore 0x01dd60b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
18 QuartzCore 0x01d3c7fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
19 QuartzCore 0x01d3db85 _ZN2CA11Transaction6commitEv + 393
20 QuartzCore 0x01d3e258 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
21 CoreFoundation 0x01f4336e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
22 CoreFoundation 0x01f432bf __CFRunLoopDoObservers + 399
23 CoreFoundation 0x01f21254 __CFRunLoopRun + 1076
24 CoreFoundation 0x01f209d3 CFRunLoopRunSpecific + 467
25 CoreFoundation 0x01f207eb CFRunLoopRunInMode + 123
26 GraphicsServices 0x03eab5ee GSEventRunModal + 192
27 GraphicsServices 0x03eab42b GSEventRun + 104
28 UIKit 0x006bef9b UIApplicationMain + 1225
29 MyApp 0x0002158d main + 141
30 libdyld.dylib 0x0262c701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
如果要在本地加载html,请不要使用
loadRequest
。用这个
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];
HTML文件不在捆绑包中或不在指定的路径上。检查文件名和文件夹名的大小写。如果您在模拟器中构建/运行,您可以查看~/Library/Application Support/iPhone simulator/7.1/Applications中的包。我的HTML文件在包中。我在xcode中创建了一个名为HTML的文件夹组,并在其中添加了文件。我如何在模拟器中查看包?好的,现在我知道你在说什么了。我在我的机器上导航到该捆绑路径,但没有看到其中的文件。但是我怎么把它放进去呢?我是手动操作,然后直接输入还是怎么做?我将该文件放在xcode项目中,我认为它将在构建/运行时添加,因此我不确定下一步要做什么。通过将HTML文件夹从Finder拖到xcode中,将其添加到xcode项目中。当被询问时,选择“创建文件夹引用”。这将确保HTML文件夹本身将被复制到您的包中,而不仅仅是其中的文件。谢谢Eric,当我拖动并“创建文件夹引用”时,这终于奏效了。再次感谢!!我尝试了你的建议,但仍然得到相同的崩溃错误。对EricS来说,可能是找不到文件。但我把它放在那里是没有意义的,我可以在xcode中看到它,除非我遗漏了什么。