【问题标题】:Call to UIWebView crashing my app in iOS调用 UIWebView 在 iOS 中使我的应用程序崩溃
【发布时间】:2014-05-21 00:30:28
【问题描述】:

我的故事板中有一个 WebView。我在我的视图控制器文件中为该 webview 创建了一个插座属性。然后在我的代码中我这样称呼它:

- (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) 

【问题讨论】:

    标签: objective-c ios7 uiwebview


    【解决方案1】:

    HTML 文件不在您的捆绑包中或不在指定的路径中。检查文件和文件夹名称的大小写。如果您在模拟器中构建/运行,您可以查看 ~/Library/Application Support/iPhone Simulator/7.1/Applications 中的包。

    【讨论】:

    • 我的 HTML 文件在包中。我在 xcode 中创建了一个名为 HTML 的文件夹组,并在那里添加了文件。如何在模拟器中查看捆绑包?
    • 好吧,我明白你在说什么了。我导航到我机器上的那个包路径,我在那里看不到文件。但是我怎么把它放在那里呢?我是手动做的,然后把它放进去还是什么?我将文件放在 xcode 项目中,我认为它会在 Build/Run 时添加,所以我不确定下一步该做什么。
    • 将 HTML 文件夹从 The Finder 拖到 Xcode 中,将其添加到 Xcode 项目中。询问时,选择“创建文件夹引用”。这将确保 HTML 文件夹本身将被复制到您的包中,而不仅仅是其中的文件。
    • 谢谢 Eric,当我改为拖动并“创建文件夹引用”时,这终于奏效了。再次感谢!!
    【解决方案2】:

    如果您想在本地加载 html,请不要使用 loadRequest。用这个

    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"html"];
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
    [webView loadHTMLString:htmlString baseURL:nil];
    

    【讨论】:

    • 我尝试了您的建议,但仍然遇到相同的崩溃错误。就 EricS 而言,也许它没有找到文件。但是我把它放在那里没有意义,除非我遗漏了什么,否则我可以在 xcode 中实际看到它。
    猜你喜欢
    • 1970-01-01
    • 2014-11-11
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多