我的应用在点击“共享”时崩溃(仅限部分用户)。这是代码:
@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)
知道可能会发生什么吗?它适用于一半的用户。
答案 0 :(得分:0)
在转换前检查发件人:
guard let sourceView = sender as? UIView else {
return
}
...
activityVC.popoverPresentationController?.sourceView = sourceView
答案 1 :(得分:0)
对于以下行
activityVC.popoverPresentationController?.sourceView = sender as! UIView
添加此行
if let senderSourceView = sender as? UIView {
activityVC.popoverPresentationController?.sourceView = senderSourceView
}else{
//Here not getting UIView
}
可能是App崩溃了,因为如果UIView没有获得sourceView.It是正常工作,如我所说。