Skip to content

Commit

Permalink
fix: fix crash when showing alerts on iPad
Browse files Browse the repository at this point in the history
The modalPresentationStyle of a UIAlertController is UIModalPresentationPopover
on iPad. It requires to provide a sourceView and sourceRect, otherwise it
crashes with NSGenericException.
  • Loading branch information
noxwell committed Apr 16, 2024
1 parent 84815ee commit 6b8e692
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Features/Save/MediaSave.x
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import "../../InstagramHeaders.h"
#import "../../Manager.h"
#import "../../Utils.h"

// Download photos
%hook IGFeedPhotoView
Expand Down Expand Up @@ -49,6 +50,7 @@
}]];
}
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[BHIUtils prepareAlertPopoverIfNeeded:alert inView:self];

NSLog(@"[BHInsta] Save media: Displaying alert");

Expand Down Expand Up @@ -119,6 +121,7 @@
}]];
}
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[BHIUtils prepareAlertPopoverIfNeeded:alert inView:self];

NSLog(@"[BHInsta] Save media: Displaying alert");

Expand All @@ -143,6 +146,7 @@
}]];
}
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[BHIUtils prepareAlertPopoverIfNeeded:alert inView:self];

NSLog(@"[BHInsta] Save media: Displaying alert");

Expand Down Expand Up @@ -207,6 +211,7 @@
}

[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[BHIUtils prepareAlertPopoverIfNeeded:alert inView:self];
[self.viewController presentViewController:alert animated:YES completion:nil];
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Features/Save/ProfileImageSave.x
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import "../../InstagramHeaders.h"
#import "../../Manager.h"
#import "../../Utils.h"

%hook IGProfilePicturePreviewViewController
%property (nonatomic, strong) JGProgressHUD *hud;
Expand Down Expand Up @@ -33,6 +34,7 @@
[self.hud showInView:topMostController().view];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[BHIUtils prepareAlertPopoverIfNeeded:alert inView:profilePictureView];

NSLog(@"[BHInsta] Save pfp: Displaying alert");

Expand Down
1 change: 1 addition & 0 deletions src/Features/Save/StoriesSave.x
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
}]];
} */
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[BHIUtils prepareAlertPopoverIfNeeded:alert inView:self.mediaView];

NSLog(@"[BHInsta] Save story: Displaying alert");

Expand Down
1 change: 1 addition & 0 deletions src/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
// Functions
+ (BOOL)isNotch;
+ (BOOL)showConfirmation:(void(^)(void))okHandler;
+ (void)prepareAlertPopoverIfNeeded:(UIAlertController*)alert inView:(UIView*)view;

@end
8 changes: 8 additions & 0 deletions src/Utils.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,13 @@ + (BOOL)showConfirmation:(void(^)(void))okHandler {

return nil;
};
+ (void)prepareAlertPopoverIfNeeded:(UIAlertController*)alert inView:(UIView*)view {
if (alert.popoverPresentationController) {
// UIAlertController is a popover on iPad. Display it in the center of a view.
alert.popoverPresentationController.sourceView = view;
alert.popoverPresentationController.sourceRect = CGRectMake(view.bounds.size.width / 2.0, view.bounds.size.height / 2.0, 1.0, 1.0);
alert.popoverPresentationController.permittedArrowDirections = 0;
}
};

@end

0 comments on commit 6b8e692

Please sign in to comment.