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

时间:2015-04-06 00:56:44

标签: swift crash ios-simulator

我的应用程序基本上是一个带有本地存储的webapp的webview。

为了支持 URL-Scheme映射,我在viewDidLoad上调用以下函数来订阅UIApplicationDidBecomeActiveNotification,然后当我收到此消息时,我将一个webapp加载到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(来自观察到的处理程序)时,只要我点击窗口上的任何地方,模拟器就会一直崩溃,下面会有堆栈跟踪。

相反,如果我没有订阅该活动,只需从initWebApp()拨打viewDidLoad(),一切正常(显然网址方案映射没有& #39;工作,但我的意思是 webview及其应用程序100%运行而不会崩溃

任何人都可以看看这个堆栈跟踪(它总是一样的)并给我任何指针?

从事件处理程序操作UIWebView等UI对象是不好的做法? (我来自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

enter image description here

1 个答案:

答案 0 :(得分:0)

如果我将对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();
        }
    }