Skip to main content
 首页 » 编程设计

swift之快速简单的应用程序总是在模拟器上崩溃

2025年05月04日36txw1958

我的应用基本上是带有本地存储的Web应用的Web View 。

为了支持 URL方案映射,我在viewDidLoad上调用以下函数,以订阅UIApplicationDidBecomeActiveNotification,然后在收到此类消息时,将一个Web应用程序加载到UIWebView中:

/** 
* Setup the observer to get notifications about app activation 
*/ 
 
func initObserver() { 
    NSNotificationCenter.defaultCenter().addObserver( 
        self, 
        selector: "initWebApp", 
        name: UIApplicationDidBecomeActiveNotification, 
        object: nil); 
} 
 
/** 
* Launches the WebApp, this is called from the observer,  
* and appDelegate.appParameters has possible parameters to the webapp 
*/ 
 
func initWebApp() { 
    var appDelegate=UIApplication.sharedApplication().delegate as AppDelegate; 
    println ("- Launch Parameters: "+appDelegate.initParameters); 
    let path=appDelegate.initParameters, 
        url=NSURL(string: URL_BASE + "?" + (path ?? "")), 
        requestObj=NSURLRequest(URL: url!); 
    mWebView.loadRequest(requestObj); 
 
} 

但是,当我以这种方式(来自观察到的处理程序)初始化webview时,只要单击窗口中的任意位置,模拟器始终会崩溃,并附带下面的stacktrace。

相反,如果 我不订阅事件,而只是从 initWebApp()调用 viewDidLoad(),则一切正常(显然URL方案映射不起作用,但我的意思是 Webview及其应用程序100%正常运行而不会崩溃)

谁能看看这个堆栈跟踪(总是一样的)并给我任何指针?

从事件处理程序操纵UI对象(例如UIWebView)是否是不好的做法? (我来自Android,在类似情况下,您经常不得不更改线程)
2015-04-06 02:49:34.907 FunqTV[14285:518122] -[NSURL _effectiveStatusBarStyleViewController]: unrecognized selector sent to instance 0x7ae1cac0 
2015-04-06 02:49:34.919 FunqTV[14285:518122] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL _effectiveStatusBarStyleViewController]: unrecognized selector sent to instance 0x7ae1cac0' 
*** First throw call stack: 
*** First throw call stack: 
( 
    0   CoreFoundation                      0x00648466 __exceptionPreprocess + 182 
    1   libobjc.A.dylib                     0x02037a97 objc_exception_throw + 44 
    2   CoreFoundation                      0x006502c5 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277 
    3   CoreFoundation                      0x00598bc7 ___forwarding___ + 1047 
    4   CoreFoundation                      0x0059878e _CF_forwarding_prep_0 + 14 
    5   UIKit                               0x00fd3fa8 +[UIViewController _currentStatusBarStyleViewController] + 75 
    6   UIKit                               0x00e63c81 -[UIApplication _updateCurrentStatusBarViewControllerAppearance] + 59 
    7   UIKit                               0x00eb06ea -[UIWindow _updateContextOrderingAndSetLayerHidden:] + 548 
    8   UIKit                               0x00eade74 -[UIWindow _initWithFrame:debugName:scene:attached:] + 336 
    9   UIKit                               0x00eadd1e -[UIWindow _initWithFrame:debugName:attached:] + 79 
    10  UIKit                               0x00eadc7f -[UIWindow _initWithFrame:attached:] + 71 
    11  UIKit                               0x011e6f27 -[UIAutoRotatingWindow _initWithFrame:attached:] + 80 
    12  UIKit                               0x00eadc33 -[UIWindow initWithFrame:] + 63 
    13  UIKit                               0x011e6eba -[UIAutoRotatingWindow initWithFrame:] + 72 
    14  UIKit                               0x011e4e2a -[UITextEffectsWindow initWithFrame:] + 72 
    15  UIKit                               0x00eae09a -[UIWindow initWithContentRect:] + 164 
    16  UIKit                               0x011e4b12 -[UITextEffectsWindow(UIObjectsForPerScreen) _basicInitWithScreen:options:] + 141 
    17  UIKit                               0x011e4bcf -[UITextEffectsWindow(UIObjectsForPerScreen) _initWithScreen:options:] + 182 
    18  UIKit                               0x016fcf8c +[_UIObjectPerScreen objectOfClass:forScreen:withOptions:createIfNecessary:] + 556 
    19  UIKit                               0x011e645c +[UITextEffectsWindow _sharedTextEffectsWindowforScreen:aboveStatusBar:allowHosted:matchesStatusBarOrientationOnAccess:] + 416 
    20  UIKit                               0x011e65b4 +[UITextEffectsWindow sharedTextEffectsWindowForScreen:] + 121 
    21  UIKit                               0x013d173a -[UIPeripheralHost(UIKitInternal) containerWindow] + 156 
    22  UIKit                               0x013d178a -[UIPeripheralHost(UIKitInternal) containerRootController] + 34 
    23  UIKit                               0x013c6e15 -[UIPeripheralHost currentState] + 38 
    24  UIKit                               0x013c6f10 -[UIPeripheralHost isOnScreen] + 35 
    25  UIKit                               0x013d3a6a -[UIPeripheralHost(UIKitInternal) _isCoordinatingWithSystemGestures] + 82 
    26  UIKit                               0x00e70da1 -[UIApplication _shouldDelayTouchesForControlCenter] + 56 
    27  UIKit                               0x00ebd506 -[UIWindow _shouldDelayTouchForSystemGestures:] + 41 
    28  UIKit                               0x0128f8e8 -[_UISystemGestureGateGestureRecognizer touchesBegan:withEvent:] + 498 
    29  UIKit                               0x00eb3946 -[UIWindow _sendGesturesForEvent:] + 567 
    30  UIKit                               0x00eb4abf -[UIWindow sendEvent:] + 769 
    31  UIKit                               0x00e79bb1 -[UIApplication sendEvent:] + 242 
    32  UIKit                               0x00e89bf6 _UIApplicationHandleEventFromQueueEvent + 21066 
    33  UIKit                               0x00e5dbc7 _UIApplicationHandleEventQueue + 2300 
    34  CoreFoundation                      0x0056b98f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15 
    35  CoreFoundation                      0x0056149d __CFRunLoopDoSources0 + 253 
    36  CoreFoundation                      0x005609f8 __CFRunLoopRun + 952 
    37  CoreFoundation                      0x0056037b CFRunLoopRunSpecific + 443 
    38  CoreFoundation                      0x005601ab CFRunLoopRunInMode + 123 
    39  GraphicsServices                    0x040152c1 GSEventRunModal + 192 
    40  GraphicsServices                    0x040150fe GSEventRun + 104 
    41  UIKit                               0x00e619b6 UIApplicationMain + 1526 
    42  FunqTV                              0x0007dfbe top_level_code + 78 
    43  FunqTV                              0x0007dffb main + 43 
    44  libdyld.dylib                       0x027a5ac9 start + 1 
    45  ???                                 0x00000001 0x0 + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

请您参考如下方法:

如果我将对initWebApp()的调用包装到dispatch_async()块中,则它将起作用:

   /** 
    * Setup the observer to get notifications about app activation 
    */ 
 
    func initObserver() { 
        NSNotificationCenter.defaultCenter().addObserver( 
            self, 
            selector: "requestInitWebApp", 
            name: UIApplicationDidBecomeActiveNotification, 
            object: nil); 
    } 
 
   /** 
    * Called from the observer. It now dispatches async a call to the init 
    * function rather than calling it directly from the observer thread. 
    */ 
 
    func requestInitWebApp() { 
        println ("Dispatching async call to init webview"); 
        dispatch_async(dispatch_get_main_queue()) { 
            self.initWebApp(); 
        } 
    }