iPhone app crashes on device but not simulator. Accessing local files with UIWebView, possible [NSBundle mainBundle] issue?
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-man开发者_开发问答ager
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.
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')
Since you haven't posted the exception, I'm guessing the problem is you passed nil
to +fileURLWithPath:
. Why would you get nil
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 back nil
on the device. Since you haven't tested if it's nil
, you're passing it directly to +fileURLWithPath:
and this is likely throwing an exception as a result.
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 of pathForResource:
. 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.
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"localHTML/mobile"];
NSLog(@"Path is %@",path);
NSURL *url = [NSURL fileURLWithPath:path];
NSLog(@"URL is %@",url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
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).
精彩评论