- UIWindow setRootViewController: equivalent in pre 4.0 - iphone

I started iphone development with 4.0 sdk. I am trying to make my app compatible with 3.2. I realized that -[UIWindow setRootViewController:] is added to the sdk in version 4.0. So instead of it, what should I use in pre 4.0 releases? In other words, what does rootViewController property do in essence except assigning window's primary subview? If I extend UIWindow and redefine the method as below, would it be a problem in future?
- (void) setRootViewController:(UIViewController *)controller
{
if (systemVersion < 4.0)
{
while(self.subviews.count > 0)
[[self.subviews objectAtIndex:0] removeFromSuperview];
[self addSubview:controller.view];
}
else [super setRootViewController:controller];
}

We used to do this:
[window addSubview:[navigationController view]];
That is what it used to be in Apple's samples and still works ok.

Related

Orientation issue in XCode 4.5.2 GM Seed version with iOS 6

I am working on iPhone application with both Portrait orientations support (Portrait and Portrait UpsideDown).
In earlier XCode4.5.1, I have resolved this issue by:
Setting rootViewController in AppDelegate
Mentioning shouldAutorotateToInterfaceOrientation like this:
-(BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation {
return UIInterfaceOrientationIsPortrait(toInterfaceOrientation); }
Mentioning supportedInterfaceOrientation in info.plist file
Now I am doing the same things for newer XCode but in iPhone simulator v6.0 its not supporting rotation properly.
I have tried with these methods as well:
-(BOOL) shouldAutorotate {
BOOL returnValue = NO;
int interface = [self preferredInterfaceOrientationForPresentation];
if(UIInterfaceOrientationIsPortrait(interface)) {
// Code to handle portrait orientation
returnValue = YES;
}
else {
// Code to handle Landscape orientation
returnValue = NO;
}
return returnValue;
}
- (NSUInteger) supportedInterfaceOrientations {
return (UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown);
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return (UIInterfaceOrientationPortrait |
UIInterfaceOrientationPortraitUpsideDown);
}
Please guide me how to support both the Portrait orientations for iOS > 4.3 all the versions.
Thanks in advance,
Mrunal
Why Did Orientation Change to Landscape Stop Working in iOS 6?
Starting in IOS 6.0 there are several orientation changes that stopped my app from rotation out of Portrait.
The fix for me, and the one applicable here, is that you must setRootViewController on the window in your AppDelegate. The earlier answer offers several suggestions that are all correct, but misses the one item that was relevant for me.
In application didFinishLaunchingWithOptions, after:
[window makeKeyAndvisible]
or
[window addSubview: viewController.view];
You must replace with:
if([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
[window setRootViewController:viewController];
} else {
[window addSubview: viewController.view];
(or [window makeKeyAndvisible])
}
You also need to add the new shouldAutoRotate instead of the depreciated shouldAutorotateToInterfaceOrientation, but this was easier to find and less crucial for me.
Same with making sure all your orientations are specified in your .plist file.
I did not need to override supportedInterfaceOrientations because I am satisfied with the default orientations (all for iPad UIInterfaceOrientationMaskAll, all but upside-down for iPhone UIInterfaceOrientationMaskAllButUpsideDown).
I have to admit, that when iOS 6 came around, I fiddled around with this until I got something to work, and then stopped. But, here is my guess:
I believe that shouldAutorotate is intended to be used in situations when you might want to dynamically change whether or not your app supports autorotation at all. I don't think it's intended for you to tell iOS which orientations you support (like the original shouldAutorotateToInterfaceOrientation: method). So, if your app supports any autorotation, I think you should just do this:
-(BOOL) shouldAutorotate {
return YES;
}
supportedInterfaceOrientations is where you are supposed to identify which orientations you support.
However, in that method, you're doing that differently than what I've been using (which might, or might not be a problem). I think you should use the mask constants, for this method. So, instead of this:
- (NSUInteger) supportedInterfaceOrientations {
return (UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown);
}
use this:
- (NSUInteger) supportedInterfaceOrientations {
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}
Also, I don't think the preferredInterfaceOrientationForPresentation method is supposed to return more than one orientation. Preferred should probably just be one, such as:
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
Here is the relevant Apple documentation
Update:
The other thing you might try is using this method:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}
in your App Delegate class. This method is optional, but can set the default values for which orientations your app supports, in case you don't specify them in all your View Controllers, or in the app's plist file. Since you don't show your plist file, and your other View Controllers, I can't tell if this might help, or not. But, it might be something to try.

iPhone 5 device modifier for dynamic xib loading

I'm working on a universal iOS app and wanting to use different xib files for iPhone 5. The way it currently works is by automatically selecting the file with the right device modifier (as specified here: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/LoadingResources/Introduction/Introduction.html#//apple_ref/doc/uid/10000051i-CH1-SW2). However there's no mention there of how to handle the iPhone 5. I'm surprised because I'm under the impression they'd prefer developers to create a different/unique experience on the 5 instead of just auto scaling...
Anyone aware of a modifier that works? (eg myxib~iphone5.xib) It would be more convenient than having to handle the device detection and switching myself.
Cheers!
There's no naming convention unfortunately, but there's a way. This is what I'm using:
I have a global .h file called GlobalConstants that I put all my #define macros in. There I have this:
static BOOL is4InchRetina()
{
if ((![UIApplication sharedApplication].statusBarHidden && (int)[[UIScreen mainScreen] applicationFrame].size.height == 548) || ([UIApplication sharedApplication].statusBarHidden && (int)[[UIScreen mainScreen] applicationFrame].size.height == 568))
return YES;
return NO;
}
then in any of my view controller classes - I override the init method like this:
#import GlobalConstants.h
-(id) init {
if(is4InchRetina()) self = [super initWithNibName:#"myNibName5" bundle:[NSBundle mainBundle]];
else self = [super initWithNibName:#"myNibName" bundle:[NSBundle mainBundle]];
return self
}

Problems building for both iOS4.3 and iOS5.0

I'm running up against problems trying to incorporate some iOS5-specific libraries into an app targeted at both iOS5 and iOS4.3. I've gone through the following steps:
weakly-linked the Twitter framework by setting it as optional in 'Link Binary with Libraries"
added it as a framework for the iOS5.0 SDK in Other Linker Flags with `-framework Twitter.framework'
conditionally linked the framework in the class header:
#if defined(__IPHONE_5_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_5_0
#import <Twitter/Twitter.h>
#import <Accounts/Accounts.h>
#endif
then in the method itself, I'm then checking whether the user's set up for Twitter:
if ([TWTweetComposeViewController class]) {
self.canTweet = [TWTweetComposeViewController canSendTweet];
}
This works beautifully on both the 5.0 and 4.3 simulators. However, I've got problems getting it to run on, or archive for, actual devices.
When I've got either a 3GS running 5.0, or a 4 running 5.0 attached, both show up twice in the Scheme dropdown. Selecting the top one, and attempting build or run the project fails with an Use of unidentified identifier 'TWTweetComposeViewController' error.
Using the second device entry, the build fails with a ld: framework not found Twitter.framework error.
I'm sure there's something I'm missing here, but I'm stumped. Can anyone advise?
If you are using a week linking then you have to check first availability of API using
NSClassFromString, respondsToSelector, instancesRespondToSelector etc.
So, change your if condition. First try to get your class object using above specified runtime function.
here is a link explaining in detail how to do such.
link
The code for presenting twitter controller
Before this you have to add the frameworks as optional and make the import in h file if iOS is min iOS 5
Class TWTweetComposeViewControllerClass = NSClassFromString(#"TWTweetComposeViewController");
if (TWTweetComposeViewControllerClass != nil) {
if([TWTweetComposeViewControllerClass respondsToSelector:#selector(canSendTweet)]) {
UIViewController *twitterViewController = [[TWTweetComposeViewControllerClass alloc] init];
[twitterViewController performSelector:#selector(setInitialText:)
withObject:NSLocalizedString(#"TwitterMessage", #"")];
[twitterViewController performSelector:#selector(addURL:)
withObject:url];
[twitterViewController performSelector:#selector(addImage:)
withObject:[UIImage imageNamed:#"yourImage.png"]];
[self.navigationController presentModalViewController:twitterViewController animated:YES];
[twitterViewController release];
}
}
Further digging into the error thrown back by the compiler suggested that it was ignoring the weak link flag. Although I've no idea how or why, it was fixed by a reinstallation of XCode.
if you link to 4.2 or later and deploy to 3.1 or later, you can use the new weak linking features to make this check simple.
you have to add Twitter frameworks as optional and then
Class TWTweetComposeViewControllerClass = NSClassFromString(#"TWTweetComposeViewController");
if (TWTweetComposeViewControllerClass != nil)
{
if([TWTweetComposeViewControllerClass respondsToSelector:#selector(canSendTweet)])
{
UIViewController *twitterViewController = [[TWTweetComposeViewControllerClass alloc] init];
[twitterViewController performSelector:#selector(setInitialText:)
withObject:NSLocalizedString(#"TwitterMessage", #"")];
[twitterViewController performSelector:#selector(addURL:)
withObject:url];
[twitterViewController performSelector:#selector(addImage:)
withObject:[UIImage imageNamed:#"yourImage.png"]];
[self.navigationController presentModalViewController:twitterViewController animated:YES];
[twitterViewController release];
}
}

Universal iOS app crashing on iPhone/iTouch 3.1.3 due to UIPopoverController

I just updated my app so that it's a universal app. In doing so I added support for UIPopoverController in a certain place. Now the app seems to be crashing on 3.1.3 iPhone/iTouch devices:
OS Version: iPhone OS 3.1.3 (7E18)
Report Version: 104
Exception Type: EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x00000001, 0xe7ffdefe
Crashed Thread: 0
Dyld Error Message:
Symbol not found: _OBJC_CLASS_$_UIPopoverController
What I don't get is that I'm only trying to call UIPopoverController if the hardware is an iPad:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:btc];
CGSize popoverSize = { 300.0, 500.0 };
popover.delegate = self;
popover.popoverContentSize = popoverSize;
self.bmPopover = popover;
[popover release];
[self.bmPopover presentPopoverFromBarButtonItem:self.bmBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
[self presentModalViewController:nav animated:YES];
}
I do have an iVar and a property of type UIPopoverController declared but I wouldn't have expected this to cause an issue at runtime if I didn't actually try to call methods in the class.
What am I supposed to do to make sure that the system doesn't try to link with UIPopoverController at runtime when this isn't supported?
For Universal app you should not only check if this is ipad or does this class exists but you should link UIKit as Weak reference a not default ( strong ), to do this:
get to target info
select general
in linked libraries change UIKit required to UIKit weak
Have fun making universal apps :]
Even though this code would most likely never run on the iPhone, it will still be linked and thus you are receiving the error. Before instantiating, you need to check if the class exists. You can modify your code above to the following which will fix it.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
Class UIPopoverControllerClass = NSClassFromString(#"UIPopoverController");
if (UIPopoverControllerClass != nil) {
UIPopoverController *popover = [[UIPopoverControllerClass alloc] initWithContentViewController:btc];
CGSize popoverSize = { 300.0, 500.0 };
popover.delegate = self;
popover.popoverContentSize = popoverSize;
self.bmPopover = popover;
[popover release];
[self.bmPopover presentPopoverFromBarButtonItem:self.bmBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
} else {
[self presentModalViewController:nav animated:YES];
}
Also, you could weak link against the UIKit framework, which would also solve the problem. I prefer the code solution as it is more safe.
You need to stop the compiler errors by referencing the class from a string. This should be used in tandem with UI_USER_INTERFACE_IDIOM() and will compile on an SDK that does not know about UIPopoverController. Here is an example:
Class popClass = NSClassFromString(#"UIPopoverController");
if(popClass) {
id infoPop = [[popClass alloc] initWithContentViewController:popViewController];
[infoPop presentPopoverFromRect:CGRectMake(20, 70, 10, 10) inView:self.view permittedArrowDirections:4 animated:YES];
}
The easy answer:
In your Xcode project, Select the top level project icon. Do a Get Info, go to the BUILD panel. set Configurations to All Configurations, set Show: to ** All Settings**
Set the iOS Deployment Target to iOS 3.1.
Recompile. Your program is failing because the minimum OS is set too high, so the loader can't resolve the symbol UIPopoverController. The change I just walked you through makes that symbol weak: it will resolve to NULL, but at least your program will load.
Are you loading a NIB file that implicitly creates a UIPopoverController? That's another way to crash.
I found it useful to add this, so that I could use UI_USER_INTERFACE_IDIOM() on pre-3.2 systems.
#ifndef __IPHONE_3_2 // if iPhoneOS is 3.2 or greater then __IPHONE_3_2 will be defined
typedef enum {
UIUserInterfaceIdiomPhone, // iPhone and iPod touch style UI
UIUserInterfaceIdiomPad, // iPad style UI
} UIUserInterfaceIdiom;
#define UI_USER_INTERFACE_IDIOM() UIUserInterfaceIdiomPhone
#endif // ifndef __IPHONE_3_2

Custom iPhone KeyBoard for SDK 4 (iOS4 OS)

Old SDK solution:
- (void)modifyKeyboard:(NSNotification *)notification
{
UIView *firstResponder = [[[UIApplication sharedApplication] keyWindow] performSelector:#selector(firstResponder)];
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows])
for (UIView *keyboard in [keyboardWindow subviews])
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
{
MyFancyKeyboardView *customKeyboard = [[MyFancyKeyboardView alloc] initWithFrame: CGRectMake(0, 0, keyboard.frame.size.width, keyboard.frame.size.height);
[keyboard addSubview: customKeyboard];
[customKeyboard release];
}
}
Following the above method, I now find that the iOS4 is different. It will not work. I am sure this is due to differences in naming subviews (or the sort). Does anyone know how to get around this same problem, for the iphone SDK 4?
You can have a look at this post:
http://www.neoos.ch/news/46-development/54-uikeyboardtypenumberpad-and-the-missing-return-key
It resolves the issue and works for all version of the SDK
Yes. iOS 4 supports custom input views—you can swap your own MyFancyKeyboardView in for any UIResponder-inheriting class (e.g. UITextField and UITextView)'s keyboard by setting the responder's inputView property.
The 4.1 SDK now has a UIKeyboardTypeDecimalPad which is what you're trying to do, right?
for ios 5, it seems that doesn't work. but when I change the code like this :
change UIKeyboard to UIPeripheralHostView
it works. I hope it's useful for some other developer.