【问题标题】:UIWebView init crash on ipadipad上的UIWebView初始化崩溃
【发布时间】:2026-01-27 11:30:01
【问题描述】:

我使用以下代码来初始化 UIWebView 的实例:

-(void)viewDidLoad {

    [super viewDidLoad];

    UIWebView *webView;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
      webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 964)]; //crash
    } else {
      webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 380)];
    }   
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
   // Return YES for supported orientations
   return YES;
}

很奇怪,上面的代码在iphone模拟器上可以运行,但是在ipad模拟器上会崩溃。我的 xcode 是 3.2.4。你能提供任何线索吗?提前致谢!

崩溃日志:

flushCaches 中的 0 0x031b5574
#1 remethodizeClass 中的 0x031b38fc
_read_images 中的 #2 0x031b60ea

map_images_nolock 中的 3 0x031c3784
map_images 中的 #4 0x031b5724
#5 0x8fe03f2d 中

__dyld__ZN4dyldL18notifyBatchPartialE17dyld_image_statesbPFPKcS0_jPK15dyld_image_infoE

6 0x8fe0decd 在 __dyld__ZN11ImageLoader4linkERKNS_11LinkContextEbbRKNS_10RPathChainE

7 0x8fe056af在__dyld__ZN4dyld4linkEP11ImageLoaderbRKNS0_10RPathChainE结果,#8 0x8fe0b242在__dyld_dlopen结果,#9 0x9754a868在dlopen的结果,#10 0x0063b8e6在initWebUILocalStorageSupport结果,在#11 0x0063a0d6 - [UIWebView的_webViewCommonInit:]结果,#12 0x0063abfd在 -[UIWebView initWithFrame:]
#13 0x0002c467 在 -[WebViewController

viewDidLoad] 在 WebViewController.m:55
#14 0x00525f85 in -[UIViewController view]
#15 0x0052439f in -[UIViewController contentScrollView]
#16 0x00532fee in -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:]
#17 0x00531f40 in -[UINavigationController _layoutViewController:]
#18 0x00533f39 in -[UINavigationController _startTransition:fromViewController:toViewController:]
#19 0x0052dfa1 in -[UINavigationController _startDeferredTransitionIfNeed] 0x00673c8a in -[UILayoutContainerView layoutSubviews]
#21 0x02b10916 in -[CALayer layoutSublayers]
#22 0x02b10625 in CALayerLayoutIfNeeded
#23 0x02b3b0e7 in -[CALayer layoutIfNeeded]
#24 0x00527296 in -[UIViewController window:willAnimateRotationToInterfaceOrientation:duration:]

25 0x004afef1 in -[UIWindow _setRotatableClient:toOrientation:duration:force:]
#26 0x00736425 in -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:]

27 0x005297ac in -[UIViewController presentModalViewController:withTransition:]
#28 0x00522e70 in

-[UIViewController _tryRecursivelyPresentModalViewController:withTransition:]
#29 0x0052901d in -[UIViewController presentModalViewController:withTransition:]
#30 0x0002b8b4 in -[TweetViewController showLocation:] at TweetViewController.m:608
#31 0x0048dcee in -[UIApplication sendAction:to:from:forEvent:]
#32 0x0050743e in -[UIControl sendAction:to:forEvent:]
#33 0x005098c0 in -[UIControl(Internal) _sendActionsForEvents:withEvent:]
#34 0x0050846d in -[UIControl touchesEnded:withEvent:]
#35 0x0071aaad in _UIGestureRecognizerUpdateObserver
#36 0x03016a52 in __CFRunLoopDoObservers
#37 0x02fe3345 in CFRunLoopRunSpecific
#38 0x02fe28a8 in CFRunLoopRunInMode

39 0x034d089d in GSEventRunModal
#40 0x034d0962 in GSEventRun
#41 0x00498372 in UIApplicationMain

42 0x00002dd8 在 main.m:14

【问题讨论】:

  • 请向我们提供崩溃日志。代码本身看起来不错。
  • @Schot,感谢格式化
  • 看起来是其他原因导致崩溃...,同意 Stream Trout。无论如何,如果它是一个指针,将局部变量初始化为 nil 通常是个好主意。
  • @Steam & SegFault,感谢重播。附上日志。

标签: iphone ipad uiwebview crash


【解决方案1】:

您是否更改了 WebView initWithFrame 的某些内容?这不是 webview 的错,这是用框架创建它的正确方法。 你能发布控制器的其余部分吗?

【讨论】:

  • 我刚发现会先调用AutorotateToInterfaceOrientation方法,然后在控制器初始化时调用viewDidLoad方法。您认为我错过了 ipad 指定的自动旋转要求的任何设置吗?谢谢。
  • 我删除了 AutorotateToInterfaceOrientation 方法,然后可以创建 webView 而不会崩溃。我只是想将我的应用程序从 iphone 移植到 ipad,所以也许我在代码中遗漏了 ipad 的一些关键设置,你能提供任何线索吗?谢谢。
  • 尝试将webview作为类属性(保留,非原子)并添加到ib中。之后更改它的大小应该会更容易。