Reputation: 283
I have a button for sharing a screenshot to Facebook, etc.
Share Button :
ShareButton = UIButton.init(frame: CGRectMake(self.view!.frame.size.width/2 - 80,self.view!.frame.size.height/2 + 140 , 163, 54))
ShareButton.setImage(UIImage(named: "Share-Button.png"), forState: UIControlState.Normal)
ShareButton.addTarget(self, action: "shareButtonClicked:", forControlEvents: .TouchUpInside)
self.view!.addSubview(ShareButton)
ShareButton func :
func shareButtonClicked(sender: UIButton!) {
UIGraphicsBeginImageContextWithOptions(view!.frame.size, false, 0.0)
view!.drawViewHierarchyInRect(view!.frame, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
let activityVC = UIActivityViewController(activityItems: [image], applicationActivities: nil)
let vc = self.view?.window?.rootViewController
//New Excluded Activities Code
if #available(iOS 9.0, *) {
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypeOpenInIBooks, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint]
} else {
// Fallback on earlier versions
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint ]
}
activityVC.popoverPresentationController?.sourceView = vc?.view
activityVC.popoverPresentationController?.sourceRect = ShareButton.frame
vc?.presentViewController(activityVC, animated: true, completion: nil)
}
When I press the button, I see the following in the console:
" whose view is not in the window hierarchy! "
and nothing happens.
What's working:
What's not working:
UIActivityViewContrller
to let the user choose where to shareUpvotes: 2
Views: 1577
Reputation: 283
after a few days I was able to solve the problem myself.
The correct answer :
Share Button :
ShareButton = UIButton.init(frame: CGRectMake(self.view!.frame.size.width/2 - 80,self.view!.frame.size.height/2 + 140 , 163, 54))
ShareButton.setImage(UIImage(named: "Share-Button.png"), forState: UIControlState.Normal)
ShareButton.addTarget(self, action: "pressedShareButton:", forControlEvents: .TouchUpInside)
self.view!.addSubview(ShareButton)
ShareButton func :
func pressedShareButton(sender: UIButton!) {
UIGraphicsBeginImageContextWithOptions(view!.frame.size, false, 0.0)
view!.drawViewHierarchyInRect(view!.frame, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
let activityVC = UIActivityViewController(activityItems: [image], applicationActivities: nil)
//New Excluded Activities Code
if #available(iOS 9.0, *) {
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypeOpenInIBooks, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint]
} else {
// Fallback on earlier versions
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint ]
}
activityVC.popoverPresentationController?.sourceView = view
activityVC.popoverPresentationController?.sourceRect = ShareButton.frame
presentViewController(activityVC, animated: true, completion: nil)
print("Share!")
}
it works perfectly! good luck to everyone!
Upvotes: 2
Reputation: 5451
Try to set the origin of the activityController:
activityVC.popoverPresentationController?.sourceView = vc?.view
activityVC.popoverPresentationController?.sourceRect = ShareButton.frame
Upvotes: 0