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?