I have an activity controller, which sometimes crashes and I dont understand why. This is my code:
func screenShotMethod() {
//Create the UIImage
let image = view?.snapshot
//Save it to the camera roll
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
self.socialShare(sharingText: "text", sharingImage: image, sharingURL:NSURL(string: "itms-apps://itunes.apple.com/app/bars/XXXXXXXX"))
}
func socialShare(#sharingText: String?, sharingImage: UIImage?, sharingURL: NSURL?) {
var sharingItems = [AnyObject]()
if let text = sharingText {
sharingItems.append(text)
}
if let image = sharingImage {
sharingItems.append(image)
}
if let url = sharingURL {
sharingItems.append(url)
}
let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypeCopyToPasteboard,UIActivityTypeAirDrop,UIActivityTypeAddToReadingList,UIActivityTypeAssignToContact,UIActivityTypePostToTencentWeibo,UIActivityTypePostToVimeo,UIActivityTypePrint,UIActivityTypeSaveToCameraRoll,UIActivityTypePostToWeibo]
var currentViewController:UIViewController=UIApplication.sharedApplication().keyWindow!.rootViewController!
currentViewController.presentViewController(activityViewController, animated: true, completion: nil)
shareButton.alpha = 1.0
}
and here is my UIView snapshot extension:
extension UIView {
var snapshot: UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)
drawViewHierarchyInRect(bounds, afterScreenUpdates: true)
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return result
}
}
and here are the errors I am getting:
Thread : Fatal Exception: NSGenericException
0 CoreFoundation 0x25a14fef __exceptionPreprocess
1 libobjc.A.dylib 0x33e00c8b objc_exception_throw
2 UIKit 0x2970ae63 -[UIPopoverPresentationController presentationTransitionWillBegin]
3 UIKit 0x2934209d __71-[UIPresentationController _initViewHierarchyForPresentationSuperview:]_block_invoke
4 UIKit 0x29340a17 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke
5 UIKit 0x290e4a91 _applyBlockToCFArrayCopiedToStack
6 UIKit 0x2905f38f _afterCACommitHandler
7 CoreFoundation 0x259dafed __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__
8 CoreFoundation 0x259d86ab __CFRunLoopDoObservers
9 CoreFoundation 0x259d8ab3 __CFRunLoopRun
10 CoreFoundation 0x25925201 CFRunLoopRunSpecific
11 CoreFoundation 0x25925013 CFRunLoopRunInMode
12 GraphicsServices 0x2d101201 GSEventRunModal
13 UIKit 0x290c9a59 UIApplicationMain
14 A Void 0x000ba600 main (AppDelegate.swift:19)
15 libdyld.dylib 0x3438caaf start
and
Thread : Crashed: com.apple.main-thread
0 A Void 0x0013aa90 function signature specialization <Arg[0] = Owned To Guaranteed and Exploded, Arg[1] = Owned To Guaranteed, Arg[2] = Owned To Guaranteed> of A_Void.GameScene.touchesMoved (A_Void.GameScene)(Swift.Set<ObjectiveC.NSObject>, withEvent : ObjectiveC.UIEvent) -> () (GameScene.swift:2036)
1 A Void 0x0013a950 function signature specialization <Arg[0] = Owned To Guaranteed and Exploded, Arg[1] = Owned To Guaranteed, Arg[2] = Owned To Guaranteed> of A_Void.GameScene.touchesMoved (A_Void.GameScene)(Swift.Set<ObjectiveC.NSObject>, withEvent : ObjectiveC.UIEvent) -> () (GameScene.swift:2026)
2 A Void 0x0010b2bc #objc A_Void.GameScene.touchesMoved (A_Void.GameScene)(Swift.Set<ObjectiveC.NSObject>, withEvent : ObjectiveC.UIEvent) -> () (GameScene.swift)
3 SpriteKit 0x28f0a8af -[SKView touchesMoved:withEvent:] + 710
4 UIKit 0x2909a46b -[UIWindow _sendTouchesForEvent:] + 350
5 UIKit 0x29093df1 -[UIWindow sendEvent:] + 544
6 UIKit 0x29069fe5 -[UIApplication sendEvent:] + 196
7 UIKit 0x292e08fb _UIApplicationHandleEventFromQueueEvent + 14414
8 UIKit 0x290689f9 _UIApplicationHandleEventQueue + 1352
9 CoreFoundation 0x259dafaf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14
10 CoreFoundation 0x259da3bf __CFRunLoopDoSources0 + 218
11 CoreFoundation 0x259d8a25 __CFRunLoopRun + 772
12 CoreFoundation 0x25925201 CFRunLoopRunSpecific + 476
13 CoreFoundation 0x25925013 CFRunLoopRunInMode + 106
14 GraphicsServices 0x2d101201 GSEventRunModal + 136
15 UIKit 0x290c9a59 UIApplicationMain + 1440
16 A Void 0x0014d600 main (AppDelegate.swift:19)
17 libdyld.dylib 0x3438caaf start + 2
The app is a game made with sprite kit, so I am calling this game method inside SKScene. Also, the game game view controller is the one controlling game scene and it is embed navigation view controller, in which I present gamecenter. Anyone nows how to fix this?
Related
There is a very strange crash that occurs only on 5, 5s and SE models with iOS 10.2. Firebase says that 100% of crashes happened in background.
I got able to exactly reproduce the stack of crash (but not the crash itself on the simulator) and only figured out that it happens when the user quit an app. Seems like no other code of viewDidDisappear get called on real crash, because the stack changes in this case.
Actually, the line that mentioned in crash report goes straight after
super.viewDidDisappear(animated) and it is blank…
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
self.keyboardAvoiding.endAvoiding()
}
KeyboardAvoiding
func endAvoiding()
{
self.avoiding = false
// Update view frame
updateConstraint(nil)
}
private func updateConstraint(_ keyboardFrame: CGRect?, animationDuration: TimeInterval? = 0.0)
{
// Disable animation
let state = UIView.areAnimationsEnabled
UIView.setAnimationsEnabled(false)
// Force to recalculate view frame if needed (without animation)
self.view.superview?.layoutIfNeeded()
// Restore previous animation state
UIView.setAnimationsEnabled(state)
if let keyboardFrame = keyboardFrame
{
// Decrease view frame
self.constraint.constant = keyboardFrame.height
}
else {
// Reset bottom constraint to initial value
self.constraint.constant = self.initialConstraintConstant
}
// Layout superview
UIView.animate(withDuration: animationDuration ?? 0.0, animations: {
self.view.superview?.layoutIfNeeded()
if let keyboardAvoidingScrollView = self.view.firstViewOfClass(TPKeyboardAvoidingScrollView.self)
{
// Scroll to active text field if scroll view frame changed
keyboardAvoidingScrollView.scrollToActiveTextField()
}
})
}
Here is a crash report:
Hardware Model: iPhone6,2
Code Type: ARM-64
Parent Process: ??? [1]
OS Version: iPhone OS 10.2 (14C92)
Exception Type: SIGTRAP
Exception Codes: #0 at 0x100255178
Crashed Thread: 0
Application Specific Information:
Selector name found in current argument registers: release
Thread 0 Crashed:
0 _______ 0x0000000100255178 _______.PhoneNumberController.viewDidDisappear(Swift.Bool) -> () (PhoneNumberController.swift:97)
1 _______ 0x00000001002551a4 #objc _______.PhoneNumberController.viewDidDisappear(Swift.Bool) -> () (PhoneNumberController.swift:0)
2 UIKit 0x000000018e7026f4 -[UIViewController _setViewAppearState:isAnimating:] + 524
3 UIKit 0x000000018e7765b4 -[UIViewController __viewDidDisappear:] + 140
4 UIKit 0x000000018e7c02e0 -[UINavigationController viewDidDisappear:] + 228
5 UIKit 0x000000018e7026f4 -[UIViewController _setViewAppearState:isAnimating:] + 524
6 UIKit 0x000000018e7765b4 -[UIViewController __viewDidDisappear:] + 140
7 CoreFoundation 0x00000001887ceb10 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 16
8 CoreFoundation 0x00000001887ce214 _CFXRegistrationPost + 396
9 CoreFoundation 0x00000001887cdf90 ___CFXNotificationPost_block_invoke + 56
10 CoreFoundation 0x000000018883db8c -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1500
11 CoreFoundation 0x000000018870fe64 _CFXNotificationPost + 372
12 Foundation 0x0000000189244e0c -[NSNotificationCenter postNotificationName:object:userInfo:] + 64
13 UIKit 0x000000018e9785d8 __102-[UIApplication _handleApplicationDeactivationWithScene:shouldForceExit:transitionContext:completion:]_block_invoke.2100 + 288
14 UIKit 0x000000018e97bfdc _runAfterCACommitDeferredBlocks + 288
15 UIKit 0x000000018e96dd50 _cleanUpAfterCAFlushAndRunDeferredBlocks + 556
16 UIKit 0x000000018e6dd0b4 _afterCACommitHandler + 164
17 CoreFoundation 0x00000001887e20c0 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 28
18 CoreFoundation 0x00000001887dfcf0 __CFRunLoopDoObservers + 368
19 CoreFoundation 0x00000001887e0180 __CFRunLoopRun + 1020
20 CoreFoundation 0x000000018870e2b8 CFRunLoopRunSpecific + 440
21 GraphicsServices 0x000000018a1c2198 GSEventRunModal + 176
22 UIKit 0x000000018e7557fc -[UIApplication _run] + 680
23 UIKit 0x000000018e750534 UIApplicationMain + 204
24 _______ 0x00000001000e3188 main (AppDelegate.swift:25)
25 ??? 0x00000001876f15b8 0x0 + 0
My app is crashing (only for some users) when they click on Share. This is the code:
#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)
}
}
This is the crash report:
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)
Any idea what may be going on? It works fine for half the users.
To check the sender before converting it:
guard let sourceView = sender as? UIView else {
return
}
...
activityVC.popoverPresentationController?.sourceView = sourceView
For this below line
activityVC.popoverPresentationController?.sourceView = sender as! UIView
add this line
if let senderSourceView = sender as? UIView {
activityVC.popoverPresentationController?.sourceView = senderSourceView
}else{
//Here not getting UIView
}
It's possible App crash because if UIView not get for sourceView.It is work if right as i said.
This is the crash log I got from Crashlytics:
Thread : Crashed: com.apple.main-thread
0 Trenìt! 0x1000b93e4 SearchHistoryProvider.getMostRecentStations(Int) -> [String] (SearchHistoryProvider.swift)
1 Trenìt! 0x10007985c specialized MasterViewController.onTouchedTextField(UITextField) -> () (MasterViewController.swift:265)
2 Trenìt! 0x100075414 #objc MasterViewController.onTouchedTextField(UITextField) -> () (MasterViewController.swift)
3 UIKit 0x186fa0ad0 -[UIApplication sendAction:to:from:forEvent:] + 100
4 UIKit 0x186fa0a4c -[UIControl sendAction:to:forEvent:] + 80
5 UIKit 0x186f88740 -[UIControl _sendActionsForEvents:withEvent:] + 436
6 UIKit 0x186fa9248 -[UIControl touchesBegan:withEvent:] + 400
7 UIKit 0x186f9fdc0 -[UIWindow _sendTouchesForEvent:] + 376
8 UIKit 0x186f98b08 -[UIWindow sendEvent:] + 784
9 UIKit 0x186f68f4c -[UIApplication sendEvent:] + 248
10 UIKit 0x186f67528 _UIApplicationHandleEventQueue + 6568
11 CoreFoundation 0x181dd5124 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
12 CoreFoundation 0x181dd4bb8 __CFRunLoopDoSources0 + 540
13 CoreFoundation 0x181dd28b8 __CFRunLoopRun + 724
14 CoreFoundation 0x181cfcd10 CFRunLoopRunSpecific + 384
15 GraphicsServices 0x1835e4088 GSEventRunModal + 180
16 UIKit 0x186fd1f70 UIApplicationMain + 204
17 Trenìt! 0x10009b2fc main (AppDelegate.swift:14)```
(edited)
and this is my code:
MasterViewController.swift:
263 func showRecentStations(textField: UITextField) {
264 textField.text = ""
265 suggestionStations = masterContainerManager!.homeController?.searchHistoryProvider?.getMostRecentStations(10)
266 updateStationsTable(textField)
267 }
SearchHistoryProvider.swift
func getMostRecentStations (maxSize : Int) -> [String] {
let stationsByOldestArray = getRecentStationsByOldest()
let stations = NSMutableOrderedSet()
for i in (0...(stationsByOldestArray.count-1)).reverse() {
stations.addObject(stationsByOldestArray[i].depStation)
if stations.count==maxSize {
return stations.array as! [String]
}
stations.addObject(stationsByOldestArray[i].arrStation)
if stations.count==maxSize {
return stations.array as! [String]
}
}
return stations.array as! [String]
}
Can anyone understand what's the crash about?
I found the bug:
in case stationsByOldestArray.count was 0
the for loop would have been
for i in (0...-1).reverse()
I fixed it by rewriting the loop as
for i in (0..<stationsByOldestArray.count).reverse()
However I think the Swift crash reports are often hopelessly unhelpful!
I am getting this crash report from my users. I am not able to reproduce it so only few devices are having this issue.
Thread : Crashed: com.apple.main-thread
0 MY_PROJECT 0x00089fb8 static MY_PROJECT.GCHelper.rematch (MY_PROJECT.GCHelper.Type)(Swift.ImplicitlyUnwrappedOptional) -> () (GCHelper.swift:156)
1 libobjc.A.dylib 0x37624a37 objc_object::sidetable_retain() + 82
2 MY_PROJECT 0x000403f0 MY_PROJECT.GameScene.ButtonFunction (MY_PROJECT.GameScene)() -> () (GameScene.swift:727)
3 MY_PROJECT 0x00052ae0 function signature specialization of MY_PROJECT.GameScene.touchesEnded (MY_PROJECT.GameScene)(Swift.Set, withEvent : ObjectiveC.UIEvent) -> () (GameScene.swift:355)
4 MY_PROJECT 0x0003d7ac #objc MY_PROJECT.GameScene.touchesEnded (MY_PROJECT.GameScene)(Swift.Set, withEvent : ObjectiveC.UIEvent) -> () (GameScene.swift)
5 SpriteKit 0x2c668755 -[SKView touchesEnded:withEvent:] + 896
6 UIKit 0x2c7fc133 -[UIWindow _sendTouchesForEvent:] + 522
7 UIKit 0x2c7f5a41 -[UIWindow sendEvent:] + 540
8 UIKit 0x2c7cbc05 -[UIApplication sendEvent:] + 196
9 UIKit 0x2ca4231f _UIApplicationHandleEventFromQueueEvent + 14538
10 UIKit 0x2c7ca607 _UIApplicationHandleEventQueue + 1350
11 CoreFoundation 0x2915722f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14
12 CoreFoundation 0x29156643 __CFRunLoopDoSources0 + 222
13 CoreFoundation 0x29154cc1 __CFRunLoopRun + 768
14 CoreFoundation 0x290a09a1 CFRunLoopRunSpecific + 476
15 CoreFoundation 0x290a07b3 CFRunLoopRunInMode + 106
16 GraphicsServices 0x3085f1a9 GSEventRunModal + 136
17 UIKit 0x2c82b695 UIApplicationMain + 1440
18 MY_PROJECT 0x00086790 main (AppDelegate.swift:16)
19 libdyld.dylib 0x37bb2aaf start + 2
Below is my rematch function from GCHelper class.
class func rematch(match:GKTurnBasedMatch!){
GCHelper.sharedInstance.rematch(match)
}
func rematch(match:GKTurnBasedMatch!){
match.rematchWithCompletionHandler({ (mMatch:GKTurnBasedMatch!, error:NSError!) -> Void in
if(error != nil){
println("\(error.description)")
} else{
println("rematch")
for var i = 0; i < mMatch.participants.count; i++ {
let participants = mMatch.participants as NSArray
let part = participants.objectAtIndex(i) as! GKTurnBasedParticipant
println("part \(i) \(part.description)")
if let playerID = part.playerID {
if(part.playerID != GKLocalPlayer.localPlayer().playerID){
self.opponentName = part.player.displayName
}
} else{
self.opponentName = "Opponent"
}
// println("participant name is \(part.playerID.)")
}
println("participant name is \(self.opponentName)")
self.delegate?.enterRematch(match,opponentName: self.opponentName)
}
})
}
And this is how I call rematch in my GameScene class.
GCHelper.rematch(self.currentTurnBasedMatch)
I don't even know at which line exactly the crash is happening. Anyone have any ideas?
Im getting an crash in my app when i am using UIActivityViewController here activityItems: [self.myimage.image!] :
let activityViewController = UIActivityViewController(
activityItems: [self.myimage.image!],
applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)
myimage is defined as UIImageView :
#IBOutlet weak var myimage: UIImageView!
The stack crash is here :
Thread 0 name:
Thread 0 Crashed:
0 MyPictures 0x000000010008e394 #objc MyPictures.DetailController.ActionShare (My.DetailController)(Swift.AnyObject) -> () + 540 (DetailController.swift:95)
1 UIKit 0x000000018a5f9400 -[UIApplication sendAction:to:from:forEvent:] + 92 (UIApplication.m:3452)
2 UIKit 0x000000018a5f9400 -[UIApplication sendAction:to:from:forEvent:] + 92 (UIApplication.m:3452)
3 UIKit 0x000000018a5e24dc -[UIControl _sendActionsForEvents:withEvent:] + 608 (UIControl.m:647)
4 UIKit 0x000000018a5f8d9c -[UIControl touchesEnded:withEvent:] + 588 (UIControl.m:417)
5 UIKit 0x000000018a5b7fbc _UIGestureRecognizerUpdate + 8532 (UIGestureRecognizer.m:347)
6 CoreFoundation 0x0000000185b3c2a0 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 28 (CFRunLoop.c:1622)
7 CoreFoundation 0x0000000185b3922c __CFRunLoopDoObservers + 356 (CFRunLoop.c:1718)
8 CoreFoundation 0x0000000185b3960c __CFRunLoopRun + 832 (CFRunLoop.c:2548)
9 CoreFoundation 0x0000000185a652d0 CFRunLoopRunSpecific + 392 (CFRunLoop.c:2795)
10 GraphicsServices 0x000000018f27b6f8 GSEventRunModal + 164 (GSEvent.c:2245)
11 UIKit 0x000000018a62afa8 UIApplicationMain + 1484 (UIApplication.m:3039)
12 MyPictures 0x0000000100091acc main + 152 (AppDelegate.swift:12)
13 libdyld.dylib 0x0000000197a26a04 start + 0 (start_glue.s:78)
It seems like the image is nil that generates a crash, so you should update your code like this:
if let image = self.myimage.image{
let activityViewController = UIActivityViewController(
activityItems: [image],
applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)
} else{
// Show some kind of error
}