Popover getting crashes in iPhone - ios

I am using below code for popover for iPad apps. Its working fine for iPad apps but when i use same code for iPhone its getting crash near
" UIPopoverController* removeDefaultPopover=[[UIPopoverController alloc]initWithContentViewController:NavController];"
Can any help me to come out of this..
UIViewController *removeDefaultController = [[UIViewController alloc] init];
UIView *removeDefaultView = [[UIView alloc] init];
removeDefaultController.view = removeDefaultView;
removeDefaultController.contentSizeForViewInPopover = CGSizeMake(100, 100);
UINavigationController *NavController=[[UINavigationController alloc]initWithRootViewController:removeDefaultController];
UIPopoverController* removeDefaultPopover=[[UIPopoverController alloc]initWithContentViewController:NavController];
UIBarButtonItem *edit = [[UIBarButtonItem alloc] initWithTitle:#"EDIT" style:UIBarButtonItemStyleBordered target:self action:#selector(editDefaultLanguage)];
[removeDefaultController.navigationItem setRightBarButtonItem:edit animated:YES];removeDefaultPopover.delegate=self;
[removeDefaultPopover presentPopoverFromRect:CGRectMake(0, 0, 100, 100) inView:self.view permittedArrowDirections:NO animated:YES];
removeDefaultView.backgroundColor=[UIColor redColor];

From the documentation:
Popover controllers are for use exclusively on iPad devices.
Attempting to create one on other devices results in an exception.
You have to implement similar functionality by yourself.

According to Mark Sands UIPopoverController contains the following code:
- (id)initWithContentViewController:(UIViewController *)viewController {
if (([[UIDevice currentDevice] respondsToSelector:#selector(userInterfaceIdiom)]) {
if ([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPad) {
if ([UIPopoverController _popoversDisabled]) {
[NSException raise:NSInvalidArgumentException format:#"-[UIPopoverController initWithContentViewController:] called when not running under UIUserInterfaceIdiomPad."];
}
}
}
...
}
+ (BOOL)_popoversDisabled {
NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
if ([bundleIdentifier isEqualToString:#"com.apple.iBooks"] || [bundleIdentifier isEqualToString:#"com.apple.mobilesafari"] ||
[bundleIdentifier isEqualToString:#"com.apple.itunesu"] || [bundleIdentifier isEqualToString:#"com.apple.Maps"]) {
return NO;
}
return YES;
}
As you can see UIPopoverController is enabled for Apple's applications.
You can create UIPopoverController subclass and implement + _popoversDisabled in the following way:
+ (BOOL)_popoversDisabled {
return NO;
}
Or use method swizzling for it.

You can't use "UIPopOverController" for iPhone apps as this is intended for iPad devices only. Alternatively you can use CMPopTipView which is work in similar way as UiPopoverController does. You can check this control on https://github.com/chrismiles/CMPopTipView
I have used this control in my previous app and it works great.

As mentioned previously, UIPopoverController is not currently enabled in iPhone applications. However, if you would still like the functionality I would recommend WEPopover. Here's how easy it is to use:
SettingsViewController *viewController = [[SettingsViewController alloc]init];
self.popover = [[WEPopoverController alloc]initWithContentViewController:viewController];
[self.popover presentPopoverFromRect:self.settingsButton.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
All the correct lifecycle methods are called and it has several convenience methods as well for presenting, dismissing, etc. I've used it in several projects and have been very satisfied with it.
The only thing to watch out for when using this is to make sure that you keep a strong reference to your popover object (thus, in my example you see me use self.popover instead of an instance variable).

Related

Access view controller without re-initializing

Essentially I'm working with 3 view controllers.
Main view which starts a download. (Webview based which passes the download).
Modal download controller. (Tab based).
Downloader (HCDownload).
In the main view my download gets passed like so:
//Fire download
[activeDL downloadURL:fileURL userInfo:nil];
[self presentViewController:vc animated:YES completion:nil];
activeDL is initialized in viewDidLoad:
activeDL = [[HCDownloadViewController alloc] init];
If I removed the presentViewController, it still downloads, which is fine. Then i tap my Downloads button, it brings up the controller which defines the tabs like so:
center = [[CenterViewController alloc] init];
activeDL = [[HCDownloadViewController alloc] init];
completedDL = [[DownloadsViewController alloc] init];
activeDL.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Active Downloads"
image:nil //[UIImage imageNamed:#"view1"]
tag:1];
completedDL.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Completed Downloads"
image:nil //[UIImage imageNamed:#"view3"]
tag:2];
[self setViewControllers:[NSArray arrayWithObjects:activeDL, completedDL, nil]];
However, it is not passing the current active download. I don't know if it's a initialization problem, or my tab issue of showing the current download.
From his github, he suggests to get the current number of downloads is to call: dlvc.numberOfDownloads which for me would be
[activeDL numberOfDownloads].
I call this in the the Downloader viewWillAppear but nothing shows.
Does anybody has any suggestions or have worked with this controller?
Any help would be appreciated.
When you call:
activeDL = [[HCDownloadViewController alloc] init];
You are creating a new download controller, which has its own internal downloads array. This library, as written, has no way to pass this information from one HCDownloadViewController object to another.
Tying downloads to VC's like this will cause problems -- I recommend you rewrite this code to split that apart.
To hack around it, try to create just one HCDownloadViewController object and pass it around.
Ok so with the last comment of the other answer, "Make activeDL a member variable instead of a local variable.", got me Googling and with some tinkering and bug fixing along the way I managed to get it all up and running perfect.
I declared it all in my AppDelegate.
AppDelegate.h
#interface SharedDownloader : HCDownloadViewController <HCDownloadViewControllerDelegate>
+ (id)downloadingView;
#end
AppDelegate.m
static HCDownloadViewController *active;
#implementation SharedDownloader
+ (id)downloadingView {
if (active == nil)
active = [[HCDownloadViewController alloc] init];
return active;
}
#end
Calling to the class for downloading in my main view controller:
-(id)init{
activeDL = [SharedDownloader downloadingView];
return self;
}
//Spot where I fire the download
if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
//More code here
[activeDL downloadURL:fileURL userInfo:nil];
}
Lastly in my tab bar controller:
-(id)init {
activeDL = [SharedDownloader downloadingView];
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
activeDL.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Active Downloads" image:nil] tag:2];
}
I believe that's all of it. In any case, thanks to Lou Franco for pointing me in the right direction.

iOS 9 custom transition - animationControllerForDismissedController not called

I am a newbee in iOS development and recently run into this problem with customized transition in iOS 9.
I have an object conforms to UIViewControllerTransitioningDelegate protocol and implements animationControllerForDismissedController, something like:
#implementation MyCustomizedTransitioningDelegate
#pragma mark - UIViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
MyCustomizedTransitionAnimator *animator = [[MyCustomizedTransitionAnimator alloc] init];
animator.presenting = NO;
return animator;
}
#end
And the process that triggers the modal transition is something like:
#implementation MyViewController
#pragma mark - Initializers
+ (MyCustomizedTransitioningDelegate *)modalTransitioningDelegateSingletonInstance;
{
static MyCustomizedTransitioningDelegate *delegateInst = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
delegateInst = [[MyCustomizedTransitioningDelegate alloc] init];
});
return delegateInst;
}
#pragma mark - UIViewController
- (void)dismissViewControllerAnimated:(BOOL)animated completion:(void (^)(void))completion;
{
[self prepareForDismissViewControllerAnimated:animated completion:&completion];
[super dismissViewControllerAnimated:animated completion:completion];
}
- (void)prepareForDismissViewControllerAnimated:(BOOL)animated completion:(dispatch_block_t *)completion;
{
self.presentedViewController.modalPresentationStyle = UIModalPresentationCustom;
self.presentedViewController.transitioningDelegate = [[self class] modalTransitioningDelegateSingletonInstance];
}
#end
Since animationControllerForDismissedController method is not called, the MyCustomizedTransitionAnimator is not created, which leads to its animateTransition not called either, which causes unexpected problem in my app. (Sorry for my poor English...)
I am also attaching the screenshot of stack trace for both iOS8 & iOS9.
In iOS 8, animationControllerForDismissedController is called after the stack trace below.
But in iOS9, transitionDidFinish is called somehow in advance, which I guess probably prevent animationControllerForDismissedController being called?
I was wondering if this is an iOS 9 bug or not. Any explanation or work around solution will be greatly appreciated!
I faced the same issue.
I hope this will help someone.
What fixed it for me is to make the object which applies UIViewControllerTransitioningDelegate protocol as variable instance to keep strong relationship with it.
I think because it gets dismissed after the view is presented first time.
I had the same issue.
Turned out I needed to set the delegate on the navigationController of the UIViewController that contains the trigger button.
Having this old code that didn't work:
UIViewController *dvc = [self sourceViewController];
TransitionDelegate *transitionDelegate = [TransitionDelegate new];
dvc.modalPresentationStyle = UIModalPresentationCustom;
dvc.transitioningDelegate = transitionDelegate;
[dvc dismissViewControllerAnimated:YES completion:nil];
I changed the first line to:
UIViewController *dvc = [self sourceViewController].navigationController;
and it worked.
Hope this helps.
You need to say something like:
MyDestinationViewController *viewController = [[MyDestinationViewController alloc] init];
MyCustomizedTransitioningDelegate *transitioningDelegate = [[MyCustomizedTransitioningDelegate alloc]init];
viewController.transitioningDelegate = transitioningDelegate;
viewController.modalPresentationStyle = UIModalPresentationCustom;
[self presentViewController: viewController animated:YES completion:nil];
Or if you're using segues, in prepareForSegue say something like:
MyDestinationViewController *toVC = segue.destinationViewController;
MyCustomizedTransitioningDelegate *transitioningDelegate = [[MyCustomizedTransitioningDelegate alloc]init];
toVC.transitioningDelegate = transitioningDelegate;

iPhone 4 does not present a UIDocumentInteractionController

I have this strange problem that only occurs in iPhone 4 with iOS 7. When I try to present a UIDocumentInteractionController on to the screen, application is stuck on presentPreviewAnimated method. The same thing happens in a different place when I try to use MPMoviePlayerViewController, only this time it is stuck on initWithContentURL. No error is thrown. I know that it is not much info I provided, but I don't have a clue how these things can be related and thus I don't know what information would be helpful. In my project I am using the following structure of Views.
HomeViewController * homeViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
MenuTableViewController * menuViewController = [[MenuTableViewController alloc] initWithStyle:UITableViewStylePlain];
ECSlidingViewController * slidingViewController = [[ECSlidingViewController alloc] init];
slidingViewController.topViewController = homeViewController;
slidingViewController.underLeftViewController = menuViewController;
UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:slidingViewController];
self.window.rootViewController = navigationController;
For example when I debug the code:
_docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
_docController.delegate = self;
[_docController presentPreviewAnimated:YES];
NSLog(#"Document showed");
The #"Document showed" message is never logged.
Application works great on the iPad and iPhone 5.
In my project I am using CocoaPods.
Please ask any questions that can help with finding the solution.
Try below code, hope it will be helpful.
set UIDocumentInteractionControllerDelegate in .h file
#interface NextViewController : UIViewController <UIDocumentInteractionControllerDelegate>
Create object of UIDocumentInteractionController and initialise it.
- (IBAction)openDocument:(id)sender
{
NSString* path = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"fileType"]; // File types - .pdf, .txt, .jpg, .png, or any other.
if (path)
{
NSURL* url = [NSURL fileURLWithPath:path];
UIDocumentInteractionController* docController = [UIDocumentInteractionController interactionControllerWithURL:url];
docController.delegate = self;
[docController presentPreviewAnimated:YES];
}
}
Implement its delegate method.
#pragma mark - UIDocumentInteractionControllerDelegate
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
return self;
}
Turned out that iPhone 4 manages threads differently than other newer devices. I had an error in not directly related place resulting in an endless loop.
while (self.delegate && ![self.delegate dataLoaderOperationCanStop:self])
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
All other devices showed those ViewControllers with no problem, only iPhone 4 was stuck. I hope that my problem will help someone with similar strange application behavior.

UIImagePickerController not full screen

Since the iOS7 upgrade, I have a weird behaviour of the UIImagePickerController. In this application I am using the UIImagePickerController with a cameraOverlayView.
In iOS6 I called the UIImagePickerController using the following code:
_picker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
_picker.sourceType = UIImagePickerControllerSourceTypeCamera;
_picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
_picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
_picker.showsCameraControls = NO;
_picker.navigationBarHidden = NO;
_picker.toolbarHidden = YES;
_picker.wantsFullScreenLayout = YES;
_overlayViewController = [[OverlayViewController alloc] init];
_overlayViewController.picker = _picker;
_overlayViewController.frameSize = self.frameSize;
_overlayViewController.delegate = self;
_picker.cameraOverlayView = _overlayViewController.view;
}
else {
_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
_picker.delegate = self;
Where the OverlayViewController is an UIViewController, with a transparent background which draws some custom controls on screen.
But now in iOS 7 the camera is drawn through the statusbar and a black bar appears beneath the live camera view.
I can solve this by applying a CGAffineTransformMakeTranslation to the cameraViewTransform property of the UIImagePickerController, but why is this like this?
In iOS 7, by default UIViewController views take up the entire screen area including the status bar.
wantsFullScreenLayout
is deprecated and ignored. In some cases, this fix works (in the view controller class):
if ([self respondsToSelector:#selector(setEdgesForExtendedLayout:)]) {
[self setEdgesForExtendedLayout:UIRectEdgeNone];
}
In other cases, it's a bit more complicated. It's late here, so see how you go with it. Helpful things to note - in a UIViewController, the following code will give the correct statusbar height on both iOS 6 and iOS 7, should it come to having to align things using CGRect math:
if (UIDeviceOrientationIsLandscape(self.interfaceOrientation)) {
statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.width;
} else {
statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
}
And then don't forget that in Interface Builder, there are the new "iOS 6 delta" adjustments that allow you to design for iOS 7 and then use offsets to correct for iOS 6.
Anyhow, let me know how you go.
My understanding of the issue, based on a few other SO threads and such, is that UIImagePickerController does not do what we'd expect in terms of managing the status bar via [UIViewController -prefersStatusBarHidden].
This means you either have to disable view controller status bar management entirely, via plist, or figure out a way to get UIImagePickerController to do what we want. On the assumption that you're not looking for the former, I can say I've had success in the latter by putting the picker in a wrapper controller that does what I want (but fall back to your previous code if you still need to detect/support iOS6):
#interface PickerContainer : UIViewController
#property ( nonatomic, weak ) UIImagePickerController* picker;
#end
#implementation PickerContainer
- (void) setPicker: (UIImagePickerController*) picker
{
[self addChildViewController: picker];
[picker didMoveToParentViewController: self];
self->_picker = picker;
}
- (void) viewDidLoad
{
[super viewDidLoad];
self.picker.view.frame = self.view.bounds;
[self.view addSubview: self.picker.view];
}
// Will have no effect in ios6 -- see [-init] for that option
- (BOOL) prefersStatusBarHidden { return YES; }
- (id) init
{
if ( ! ( self = [super init] ) ) return nil;
if ( detectThatThisIsIos6() ) self.wantsFullScreenLayout = YES;
return self;
}
#end
This will work for you, scaled camera, you will have a black bar at the bottom but it will get overlayed by tool bar
https://stackoverflow.com/a/15803947

iOS Controllers for universal (iPhone/iPad) app

I am just working on iOS app and I want to make it universal for both iPhones and iPads. This is done and works without any problems:
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController_iPhone = [[ViewController_iPhone alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
} else {
self.viewController_iPad = [[ViewController_iPad alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
}
if (self.viewController_iPhone == nil)
self.window.rootViewController = self.viewController_iPad;
else
self.window.rootViewController = self.viewController_iPhone;
There is a view for each controller (ViewController_iPad.xib, ViewController_iPhone.xib). It doesn't matter which view is loaded in my problem. In a view there is a subview added (UIScrolView). And in this ScrollView there are two views from xib:
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:#"SubView1" owner:self options:nil];
UIView *view = [nibContents objectAtIndex:0];
view.frame = CGRectMake(2, 0, scrollView.frame.size.width - 2, scrollView.frame.size.height);
[scrollView addSubview:view];
nibContents = [[NSBundle mainBundle] loadNibNamed:#"SubView2" owner:self options:nil];
view = [nibContents objectAtIndex:0];
view.frame = CGRectMake(scrollView.frame.size.width + 2 , 0, scrollView.frame.size.width - 4, scrollView.frame.size.height);
[scrollView addSubview:view];
(This code is in iPad/iPhone controller). Still everything is OK. But I don't know how to set owners (in IB) of these subviews that are shown in ScrollView. These subviews are in ScrollView which is in a main view so I want to set owners of these subviews as iPad/iPhone controller. But as a owner can be only one class. Can you tell me how to set owners if I have two main controllers and I don't know which one will be loaded in runtime. Thank you.
EDIT: I have another question: I have ViewController_iPhone. It has a View property and this property is assigned to the "root" view in the main view in ViewController_iPhone (.xib). Can I assign this view property also to subview view? Because I got EXC_BAD_ACCESS error if I assign view property of ViewController_iPhone to a "root" view of subview in IB.
Looks like you need to use a class cluster. This will abstract the iPhone/iPad instantiation, so you don't explicitly need to instantiate one of the two.
You can read a bit about class clustering in the Apple documentation:
http://developer.apple.com/library/ios/#documentation/general/Conceptual/DevPedia-CocoaCore/ClassCluster.html
It boils down to creating a master view controller which will handle the allocation of iPhone or iPad subclasses based on the current device.
You should override the ViewController alloc class method:
+ (id)alloc {
NSString *classString = NSStringFromClass([self class]);
NSString *append = nil;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
append = #"_iPhone";
} else {
append = #"_iPad";
}
NSString *subClassString = [classString stringByAppendingString:append];
id subClass = NSClassFromString(subClassString);
id object;
if (subClass && ![self isKindOfClass:[subClass class]]) {
object = [subClass alloc];
} else {
object = [super alloc];
}
return object;
}
This way you can just allocate the ViewController class and at runtime the correct class definition will be used to instantiate your view controller.
This will allow you to use the ViewController class as the owner in IB provided that you create an abstraction of iPhone and iPad interfaces and define it in their super class.

Resources