【问题标题】:App crashing when trying to share a view尝试共享视图时应用程序崩溃
【发布时间】:2016-12-19 04:24:57
【问题描述】:

当他们点击分享时,我的应用程序崩溃了(仅适用于部分用户)。这是代码:

@IBAction func btnShare(sender: AnyObject) {
    let message = "Some message"
    let shareUrl = "mydomain://someurl/\(someid)"

    if let url = NSURL(string: shareUrl) {
        let objectsToShare = [message, url]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
        activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList]
        activityVC.popoverPresentationController?.sourceView = sender as! UIView
        self.presentViewController(activityVC, animated: true, completion: nil)
    }
}

这是崩溃报告:

Crashed: com.apple.main-thread
0  libsystem_kernel.dylib         0x184a32014 __pthread_kill + 8
1  libsystem_pthread.dylib        0x184af9460 pthread_kill + 112
2  libsystem_c.dylib              0x1849a63f4 abort + 140
3  libswiftCore.dylib             0x100d83928 swift::fatalError(char const*, ...) + 50
4  libswiftCore.dylib             0x100d6c0f0 swift::swift_dynamicCastFailure(void const*, char const*, void const*, char const*, char const*) + 70
5  libswiftCore.dylib             0x100d6c180 swift::swift_dynamicCastFailure(swift::Metadata const*, swift::Metadata const*, char const*) + 142
6  libswiftCore.dylib             0x100d92460 swift_dynamicCastObjCClassUnconditional + 68
7  Wize                           0x100117198 specialized MyViewController.btnShare(AnyObject) -> () (MyViewController.swift:363)
8  Wize                           0x1001132b0 @objc MyViewController.btnShare(AnyObject) -> () (MyViewController.swift)
9  UIKit                          0x18b8e27b0 -[UIApplication sendAction:to:from:forEvent:] + 96
10 UIKit                          0x18ba565ec -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 168
11 UIKit                          0x18b8e27b0 -[UIApplication sendAction:to:from:forEvent:] + 96
12 UIKit                          0x18b8e2730 -[UIControl sendAction:to:forEvent:] + 80
13 UIKit                          0x18b8ccbe4 -[UIControl _sendActionsForEvents:withEvent:] + 452
14 UIKit                          0x18b8ccd4c -[UIControl _sendActionsForEvents:withEvent:] + 812
15 UIKit                          0x18b8e201c -[UIControl touchesEnded:withEvent:] + 584
16 UIKit                          0x18b8e1b44 -[UIWindow _sendTouchesForEvent:] + 2484
17 UIKit                          0x18b8dcd8c -[UIWindow sendEvent:] + 2988
18 UIKit                          0x18b8ad858 -[UIApplication sendEvent:] + 340
19 UIKit                          0x18c09acb8 __dispatchPreprocessedEventFromEventQueue + 2736
20 UIKit                          0x18c094720 __handleEventQueue + 784
21 CoreFoundation                 0x185a12278 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
22 CoreFoundation                 0x185a11bc0 __CFRunLoopDoSources0 + 524
23 CoreFoundation                 0x185a0f7c0 __CFRunLoopRun + 804
24 CoreFoundation                 0x18593e048 CFRunLoopRunSpecific + 444
25 GraphicsServices               0x1873c1198 GSEventRunModal + 180
26 UIKit                          0x18b918628 -[UIApplication _run] + 684
27 UIKit                          0x18b913360 UIApplicationMain + 208
28 Wize                           0x1001208f4 main (AppDelegate.swift:19)
29 libdispatch.dylib              0x1849205b8 (Missing)

知道会发生什么吗?它适用于一半的用户。

【问题讨论】:

  • 第363行是哪一行?
  • @rmaddy 我认为这是self.presentViewController的最后一行,但我的代码可能已经改变,必须再次崩溃才能获得新的行号。会回来报告的。
  • 既然提到了动态转换,可能是这一行:发件人!界面视图
  • 你在猜测什么?在 Xcode 中运行应用程序,看看是哪一行实际导致了崩溃。然后使用确切的相关代码更新您的问题,并指出导致崩溃的确切行。
  • @rmaddy 它对我来说实际上并没有崩溃——只对某些用户。

标签: ios swift swift2


【解决方案1】:

在转换前检查发件人:

    guard let sourceView = sender as? UIView else {
        return
    }

...

activityVC.popoverPresentationController?.sourceView = sourceView

【讨论】:

    【解决方案2】:

    下面这行

     activityVC.popoverPresentationController?.sourceView = sender as! UIView
    

    添加这一行

     if let senderSourceView = sender as? UIView {
    
        activityVC.popoverPresentationController?.sourceView = senderSourceView
    
     }else{
        //Here not getting UIView
     }
    

    这可能是应用程序崩溃,因为如果 UIView 无法获取 sourceView。如果按照我所说的正确,它是有效的。

    【讨论】:

      猜你喜欢
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多