The iPhone application crashes to the device, but not the simulator. Accessing local files using UIWebView, maybe [problem with NSBundle mainBundle]?

I was hoping that you guys can help me, what is wrong with my code, which leads to the failure of my application on my device, but not in the simulator. This is a very simple application, I only have local files displayed in UIWebView. Here is what I use 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 am only posting this code because I feel that something is causing a problem here, but that I know, let me know if you need to see something else.

Here is the crash log. Some of this probably doesn’t help, but I decided that I would post it all, 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 

Many thanks for your help.

+4
source share
3 answers

Check out the app bundle created by Xcode. It contains the localHTML / mobile directory and all the files you expect?

(Right-click on appname.app in the "Products" section of the Xcode navigation bar, select "Reveal in Finder", then right-click on the application in Finder and select "Show Package Contents")

+2
source

Well, I'm not quite sure why this is crashing down. In appearance, you are not doing anything radical. I suppose this is somehow related to the file path or to a file that does not exist there. The line that kills her is [NSURL fileURLWithPath:path] , and you can skip this line using - (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)ext subdirectory:(NSString *)subpath instead of pathForResource: . Not sure if this will solve your problem, but it's worth it.

Edit: oh, one difference between the simulator and the device is that the file system on the device is case sensitive, while it is not on the osx / simulator. Could there be a problem in your file name or file path?


Try NSLog to find out 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 "Path - null" in the console, you are pointing out something wrong in your pathForResource call.

Think:

If you added files to the Xcode project but did not include them in the build target, they will not be added to the application package.

if localHTML and mobile are “groups” in Xcode and not actual folders, then they are ignored when compiling the application. Xcode groups mean NOTHING for a compiled application (look at the folder links for creating folders in your application).

+1
source

Since you did not throw an exception, I assume the problem is that you passed nil to +fileURLWithPath: Why did you get nil on the device, but not a simulator? The simple answer is case sensitive. The file system on the device is case sensitive, but the default HFS + file system on Mac OS X is missing. You are requesting localHTML / mobile / index.html, but if any of these characters have the wrong case, then -pathForResource:ofType:inDirectory: will end the nil cancellation on the device. Since you have not tested if it is nil , you pass it directly to +fileURLWithPath: and this will most likely throw an exception as a result.

+1
source

All Articles