iPhone 应用程序在设备上崩溃,但在模拟器上则不然。使用 UIWebView 访问本地文件,可能的 [NSBundle mainBundle] 问题?
我希望你们能帮助我,我的代码出了什么问题,导致我的应用程序在我的设备上崩溃,而不是在模拟器上崩溃。这是一个非常简单的应用程序,我只是在 UIWebView 中显示本地文件。这是我在 .m 中使用的内容,
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"localHTML/mobile"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
我只是发布此代码,因为我觉得这里的某些内容导致了问题,但我知道什么,如果您需要查看其他内容,请告诉我。
这是崩溃日志。其中一些可能没有帮助,但我想我会发布整个内容,因为我不知道要寻找什么。
Incident Identifier: 9598C96E-EA38-4C54-B39F-BBD245648E48
CrashReporter Key: cb7605a41daea519012d6fd4f52c4b19fb584743
Hardware Model: iPhone3,1
Process: Cannon Mobile [5129]
Path: /var/mobile/Applications/E5760D01-689A-41A3-A6CA-45E007B7C27A/Cannon Mobile.app/Cannon Mobile
Identifier: Cannon Mobile
Version: ??? (???)
Code Type: ARM (Native)
Parent Process: launchd [1]
Date/Time: 2011-09-30 19:26:57.951 -0400
OS Version: iPhone OS 4.3.3 (8J2)
Report Version: 104
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x00000000, 0x00000000
Crashed Thread: 0
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x3558fa1c __pthread_kill + 8
1 libsystem_c.dylib 0x356663b4 pthread_kill + 52
2 libsystem_c.dylib 0x3565ebf8 abort + 72
3 libstdc++.6.dylib 0x35628a64 __gnu_cxx::__verbose_terminate_handler() + 376
4 libobjc.A.dylib 0x3449d06c _objc_terminate + 104
5 libstdc++.6.dylib 0x35626e36 __cxxabiv1::__terminate(void (*)()) + 46
6 libstdc++.6.dylib 0x35626e8a std::terminate() + 10
7 libstdc++.6.dylib 0x35626f5a __cxa_throw + 78
8 libobjc.A.dylib 0x3449bc84 objc_exception_throw + 64
9 CoreFoundation 0x3098048a +[NSException raise:format:arguments:] + 62
10 CoreFoundation 0x309804c4 +[NSException raise:format:] + 28
11 Foundation 0x341dd188 -[NSURL(NSURL) initFileURLWithPath:] + 64
12 Foundation 0x341dd128 +[NSURL(NSURL) fileURLWithPath:] + 24
13 Cannon Mobile 0x000033da -[Cannon_MobileViewController refresh:] (Cannon_MobileViewController.m:48)
14 Cannon Mobile 0x000034ec -[Cannon_MobileViewController viewDidLoad] (Cannon_MobileViewController.m:62)
15 UIKit 0x30a18f08 -[UIViewController view] + 104
16 UIKit 0x30a172ae -[UIWindow addRootViewControllerViewIfPossible] + 26
17 UIKit 0x30b42538 -[UIWindow setRootViewController:] + 160
18 Cannon Mobile 0x00002fb6 -[Cannon_MobileAppDelegate application:didFinishLaunchingWithOptions:] (Cannon_MobileAppDelegate.m:22)
19 UIKit 0x30a1781a -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 766
20 UIKit 0x30a11b5e -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 266
21 UIKit 0x309e67d0 -[UIApplication handleEvent:withNewEvent:] + 1108
22 UIKit 0x309e620e -[UIApplication sendEvent:] + 38
23 UIKit 0x309e5c4c _UIApplicationHandleEvent + 5084
24 GraphicsServices 0x30269e70 PurpleEventCallback + 660
25 CoreFoundation 0x30957a90 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 20
26 CoreFoundation 0x30959838 __CFRunLoopDoSource1 + 160
27 CoreFoundation 0x3095a606 __CFRunLoopRun + 514
28 CoreFoundation 0x308eaebc CFRunLoopRunSpecific + 224
29 CoreFoundation 0x308eadc4 CFRunLoopRunInMode + 52
30 UIKit 0x30a10d42 -[UIApplication _run] + 366
31 UIKit 0x30a0e800 UIApplicationMain + 664
32 Cannon Mobile 0x00002f20 main (main.m:14)
33 Cannon Mobile 0x00002ec8 start + 32
Thread 1:
0 libsystem_kernel.dylib 0x355903ec __workq_kernreturn + 8
1 libsystem_c.dylib 0x356676d8 _pthread_wqthread + 592
2 libsystem_c.dylib 0x35667bbc start_wqthread + 0
Thread 2 name: Dispatch queue: com.apple.libdispatch-manager
Thread 2:
0 libsystem_kernel.dylib 0x35590fbc kevent + 24
1 libdispatch.dylib 0x35261032 _dispatch_mgr_invoke + 706
2 libdispatch.dylib 0x3526203a _dispatch_queue_invoke + 86
3 libdispatch.dylib 0x352615ea _dispatch_worker_thread2 + 186
4 libsystem_c.dylib 0x3566758a _pthread_wqthread + 258
5 libsystem_c.dylib 0x35667bbc start_wqthread + 0
Thread 3 name: WebThread
Thread 3:
0 libsystem_kernel.dylib 0x3558dc5c semaphore_wait_signal_trap + 8
1 libsystem_kernel.dylib 0x3558df52 semaphore_wait_signal + 2
2 libsystem_c.dylib 0x35664734 pthread_mutex_lock + 256
3 WebCore 0x35f533ee _ZL17_WebTryThreadLockb + 150
4 WebCore 0x35f5332e _ZL14WebRunLoopLockP19__CFRunLoopObservermPv + 14
5 CoreFoundation 0x30957a2e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 10
6 CoreFoundation 0x3095945e __CFRunLoopDoObservers + 406
7 CoreFoundation 0x3095a760 __CFRunLoopRun + 860
8 CoreFoundation 0x308eaebc CFRunLoopRunSpecific + 224
9 CoreFoundation 0x308eadc4 CFRunLoopRunInMode + 52
10 WebCore 0x35f5327e _ZL12RunWebThreadPv + 382
11 libsystem_c.dylib 0x3566630a _pthread_start + 242
12 libsystem_c.dylib 0x35667bb4 thread_start + 0
Thread 0 crashed with ARM Thread State:
r0: 0x00000000 r1: 0x00000000 r2: 0x00000001 r3: 0x00000000
r4: 0x3f86348c r5: 0x00000006 r6: 0x1d59051c r7: 0x2fe10f90
r8: 0x3ed19bf8 r9: 0x00000065 r10: 0x3180e2f0 r11: 0x3ed0c964
ip: 0x00000148 sp: 0x2fe10f84 lr: 0x3619b3bb pc: 0x360c4a1c
cpsr: 0x00000010
非常感谢您的帮助。
I was hoping you guys could help me what is wrong with my code that is causing my app to crash on my device but not the simulator. It is a very simple app, I just have local files displayed in a UIWebView. Here is what I am using in my .m
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"localHTML/mobile"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
I'm only posting this code because I feel like something in here is causing the problem, but what do I know, let me know if you need to see something else.
Here is the crash log. Some of this probably isn't helpful, but I figured I'll post the entire thing since I don't know what to look for.
Incident Identifier: 9598C96E-EA38-4C54-B39F-BBD245648E48
CrashReporter Key: cb7605a41daea519012d6fd4f52c4b19fb584743
Hardware Model: iPhone3,1
Process: Cannon Mobile [5129]
Path: /var/mobile/Applications/E5760D01-689A-41A3-A6CA-45E007B7C27A/Cannon Mobile.app/Cannon Mobile
Identifier: Cannon Mobile
Version: ??? (???)
Code Type: ARM (Native)
Parent Process: launchd [1]
Date/Time: 2011-09-30 19:26:57.951 -0400
OS Version: iPhone OS 4.3.3 (8J2)
Report Version: 104
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x00000000, 0x00000000
Crashed Thread: 0
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x3558fa1c __pthread_kill + 8
1 libsystem_c.dylib 0x356663b4 pthread_kill + 52
2 libsystem_c.dylib 0x3565ebf8 abort + 72
3 libstdc++.6.dylib 0x35628a64 __gnu_cxx::__verbose_terminate_handler() + 376
4 libobjc.A.dylib 0x3449d06c _objc_terminate + 104
5 libstdc++.6.dylib 0x35626e36 __cxxabiv1::__terminate(void (*)()) + 46
6 libstdc++.6.dylib 0x35626e8a std::terminate() + 10
7 libstdc++.6.dylib 0x35626f5a __cxa_throw + 78
8 libobjc.A.dylib 0x3449bc84 objc_exception_throw + 64
9 CoreFoundation 0x3098048a +[NSException raise:format:arguments:] + 62
10 CoreFoundation 0x309804c4 +[NSException raise:format:] + 28
11 Foundation 0x341dd188 -[NSURL(NSURL) initFileURLWithPath:] + 64
12 Foundation 0x341dd128 +[NSURL(NSURL) fileURLWithPath:] + 24
13 Cannon Mobile 0x000033da -[Cannon_MobileViewController refresh:] (Cannon_MobileViewController.m:48)
14 Cannon Mobile 0x000034ec -[Cannon_MobileViewController viewDidLoad] (Cannon_MobileViewController.m:62)
15 UIKit 0x30a18f08 -[UIViewController view] + 104
16 UIKit 0x30a172ae -[UIWindow addRootViewControllerViewIfPossible] + 26
17 UIKit 0x30b42538 -[UIWindow setRootViewController:] + 160
18 Cannon Mobile 0x00002fb6 -[Cannon_MobileAppDelegate application:didFinishLaunchingWithOptions:] (Cannon_MobileAppDelegate.m:22)
19 UIKit 0x30a1781a -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 766
20 UIKit 0x30a11b5e -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 266
21 UIKit 0x309e67d0 -[UIApplication handleEvent:withNewEvent:] + 1108
22 UIKit 0x309e620e -[UIApplication sendEvent:] + 38
23 UIKit 0x309e5c4c _UIApplicationHandleEvent + 5084
24 GraphicsServices 0x30269e70 PurpleEventCallback + 660
25 CoreFoundation 0x30957a90 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 20
26 CoreFoundation 0x30959838 __CFRunLoopDoSource1 + 160
27 CoreFoundation 0x3095a606 __CFRunLoopRun + 514
28 CoreFoundation 0x308eaebc CFRunLoopRunSpecific + 224
29 CoreFoundation 0x308eadc4 CFRunLoopRunInMode + 52
30 UIKit 0x30a10d42 -[UIApplication _run] + 366
31 UIKit 0x30a0e800 UIApplicationMain + 664
32 Cannon Mobile 0x00002f20 main (main.m:14)
33 Cannon Mobile 0x00002ec8 start + 32
Thread 1:
0 libsystem_kernel.dylib 0x355903ec __workq_kernreturn + 8
1 libsystem_c.dylib 0x356676d8 _pthread_wqthread + 592
2 libsystem_c.dylib 0x35667bbc start_wqthread + 0
Thread 2 name: Dispatch queue: com.apple.libdispatch-manager
Thread 2:
0 libsystem_kernel.dylib 0x35590fbc kevent + 24
1 libdispatch.dylib 0x35261032 _dispatch_mgr_invoke + 706
2 libdispatch.dylib 0x3526203a _dispatch_queue_invoke + 86
3 libdispatch.dylib 0x352615ea _dispatch_worker_thread2 + 186
4 libsystem_c.dylib 0x3566758a _pthread_wqthread + 258
5 libsystem_c.dylib 0x35667bbc start_wqthread + 0
Thread 3 name: WebThread
Thread 3:
0 libsystem_kernel.dylib 0x3558dc5c semaphore_wait_signal_trap + 8
1 libsystem_kernel.dylib 0x3558df52 semaphore_wait_signal + 2
2 libsystem_c.dylib 0x35664734 pthread_mutex_lock + 256
3 WebCore 0x35f533ee _ZL17_WebTryThreadLockb + 150
4 WebCore 0x35f5332e _ZL14WebRunLoopLockP19__CFRunLoopObservermPv + 14
5 CoreFoundation 0x30957a2e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 10
6 CoreFoundation 0x3095945e __CFRunLoopDoObservers + 406
7 CoreFoundation 0x3095a760 __CFRunLoopRun + 860
8 CoreFoundation 0x308eaebc CFRunLoopRunSpecific + 224
9 CoreFoundation 0x308eadc4 CFRunLoopRunInMode + 52
10 WebCore 0x35f5327e _ZL12RunWebThreadPv + 382
11 libsystem_c.dylib 0x3566630a _pthread_start + 242
12 libsystem_c.dylib 0x35667bb4 thread_start + 0
Thread 0 crashed with ARM Thread State:
r0: 0x00000000 r1: 0x00000000 r2: 0x00000001 r3: 0x00000000
r4: 0x3f86348c r5: 0x00000006 r6: 0x1d59051c r7: 0x2fe10f90
r8: 0x3ed19bf8 r9: 0x00000065 r10: 0x3180e2f0 r11: 0x3ed0c964
ip: 0x00000148 sp: 0x2fe10f84 lr: 0x3619b3bb pc: 0x360c4a1c
cpsr: 0x00000010
Thanks so much for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查 Xcode 生成的应用程序包。它是否包含 localHTML/mobile 目录以及您期望的所有文件?
(右键单击 Xcode 导航器面板中“产品”部分下的 appname.app,选择“在 Finder 中显示”,然后右键单击 Finder 中的应用程序包并选择“显示包内容”)
Check the application bundle being produced by Xcode. Does it contain the localHTML/mobile directory and all the files you are expecting?
(Right-click on appname.app under the Products section in the Xcode navigator panel, select 'Reveal in Finder', then right-click on the app bundle in Finder and select 'Show package contents')
由于您尚未发布异常,我猜测问题是您将
nil
传递给了+fileURLWithPath:
。为什么你会在设备上得到nil
而在模拟器上却没有?简单的答案:区分大小写。设备上的文件系统区分大小写,但 Mac OS X 上的默认 HFS+ 文件系统不区分大小写。您要求 localHTML/mobile/index.html,但如果其中任何字符的大小写错误,则-pathForResource:ofType:inDirectory:
最终将返回nil
在设备上。由于您尚未测试它是否为nil
,因此您将其直接传递给+fileURLWithPath:
,这可能会引发异常。Since you haven't posted the exception, I'm guessing the problem is you passed
nil
to+fileURLWithPath:
. Why would you getnil
on the device but not the simulator? Simple answer: case sensitivity. The filesystem on the device is case sensitive, but the default HFS+ filesystem on Mac OS X is not. You're asking for localHTML/mobile/index.html, but if any of those characters have the wrong case, then-pathForResource:ofType:inDirectory:
is going to end up handing backnil
on the device. Since you haven't tested if it'snil
, you're passing it directly to+fileURLWithPath:
and this is likely throwing an exception as a result.好吧,我不太确定为什么会崩溃。从表面上看,你并没有做任何根本错误的事情。我猜这与文件路径或文件不存在有关。杀死它的行是
[NSURL fileURLWithPath:path]
,您可以使用- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)ext 子目录跳过该行:(NSString *)subpath
而不是pathForResource:
。不确定这是否能解决您的问题,但值得一试。编辑:哦,模拟器和设备之间的一个区别是设备上的文件系统区分大小写,而 osx/模拟器上则不区分大小写。您的文件名或文件路径是否存在大小写问题?
尝试一些 NSLog 看看问题出在哪里。
如果您在控制台中看到“Path is null”,那么您在
pathForResource
调用中指定了错误。一个想法:
如果您已将文件添加到 Xcode 项目,但未将它们包含在构建目标中,则它们不会添加到应用程序包中。
如果 localHTML 和 mobile 是 Xcode 中的“组”,而不是实际的文件夹,那么在编译应用程序时它们将被忽略。相对于已编译的应用程序,Xcode 组没有任何意义(查看文件夹引用以了解在应用程序包中创建文件夹的方法)。
Well, I'm not exactly sure why this is crashing. From the looks of it you're not doing anything radically wrong. I'd guess it had something to do with the file path, or the file not being there. The line that's killing it is
[NSURL fileURLWithPath:path]
and you can skip that line by using- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)ext subdirectory:(NSString *)subpath
instead ofpathForResource:
. Not sure if that will solve your problem, but it's worth a shot.Edit: oh, one difference between the simulator and the device, is the file system on the device is case-sensitive, while it's not on osx/the simulator. Could there be a case issue in your file name or file path?
Try some NSLog's to see where the problem is.
If you see in the console "Path is null" then you're specifying something wrong in your
pathForResource
call.A thought:
If you've added files to your Xcode project, but not included them in the build target, they won't be added to app bundle.
if localHTML and mobile are "groups" in Xcode, not actual folders, then they're ignored when the app is compiled. Xcode groups mean NOTHING, in relation to the compiled app (look into folder references for a way to make folders in your app bundle).