Block AirPlay Mirroring on iOS 5 - iphone

On iOS 5 with an iPad 2 or iPhone 4S, users can enable screen mirroring with their Apple TV and AirPlay. How can I prevent my app from being mirrored in this way? Is there any way to detect that this mirroring is taking place so I can prevent my content from being mirrored?
The reason for doing this is because I have content I'm not legally allowed to display on a television screen.

This is a really really bad idea and I hate it as you are inhibiting your users. With that said, AirPlay mirroring works the same way as connecting the VGA/HDMI adapter, when you connect an adapter you have the ability to display whatever you want on the "second monitor". If you want to "block" mirroring you could set the external display's window to a blank/solid black view.
Most iOS applications create and use only one window during their lifetime. This window spans the entire main screen of the device and is loaded from the application’s main nib file (or created programmatically) early in the life of the application. However, if an application supports the use of an external display for video out, it can create an additional window to display content on that external display. All other windows are typically created by the system, and are usually created in response to specific events, such as an incoming phone call.
Check out the View Programming Guide for iOS, specifically the Windows section and Displaying Content on an External Display

Just to add the code for doing this pretty simple work Here
if ([[UIScreen screens] count] > 1)
{
UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1];
CGRect screenBounds = secondScreen.bounds;
UIWindow *secondWindow = [[UIWindow alloc]initWithFrame:screenBounds];
secondWindow.screen = secondScreen;
UIView *anyView= [[UIView alloc]initWithFrame:screenBounds];
anyView.backgroundColor= [UIColor blackColor];
[secondWindow addSubview:anyView];
}

Related

iPhone 5 Optimisation

I'm super confused about how the iPhone 4 and below apps are optimised for the iPhone 5. I'm a designer. How is optimisation done? Basically I have an app with loads of knobs and buttons and other interactive components. The way I see it, there are 3 options but what is the best way (or least, standard practice)?
1) Keep the layout the same for iPhone 5 but just add extra length on the background.
2) Scale the images/layout of components from iPhone 4 to iPhone 5 so everything is proportional.
3) Have completely separate images and different layouts for both. i.e for iPhone 5, I can move components to utilise the space more. The problem with this (and I'm not a developer) is that the interaction position of the components have moved so in effect, the iPhone 4 and 5 are separate apps?
To identify if it is a iphone 5 iphone, use this code in the file nameYourApp-prefix.pch:
//Macro iPhone5
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
find the nameYourApp-prefix.pch file in the supporting files, the code must be written between:
# ifdef __ OBJC__
//Code Macro iPhone5
# endif
and then just use the "if" to check that device, like this:
if (IS_IPHONE_5) {
//Code for 4 inch screen
}else{
//Code for 3.5 inch screen
}
putting the macro in the nameYourApp-prefix.pch, all classes see it.
With a UIKit application, none of your options are ideal. You simply use Springs and Struts or Auto Layout to have your user interface elements snap to where they should be.
The solution is the same when switching between portrait and landscape orientation. You define which user interface elements can grow and which ones can't, which edges to snap to, etc.
Run it on the simulator to test the layout if you don't have an iPhone 5 device.
For example, for a UITableViewController, the UITableView should grow vertically on an iPhone 5 so that you see more rows.
I almost always have at least one control that would be nice to grow given the room. If you don't, then you'll probably just have more empty space at the bottom of the view.
How iPhone 5 Optimization Works
When you tell iOS that your app is optimized for the iPhone 5 (through use of a cleverly named image file), iOS uses your existing Springs and Struts or Auto Layout (whatever you're using) to rearrange the interface.
The first choice is the easiest and works pretty well. You'd only need to worry about having specific assets for full screen images, and make sure that NavigationBar and TabBar (in case your app have one), remains on top and bottom respectively.
Scaling other images/components such as buttons, you'll change the general aspect ratio (which you probably wouldn't want), since iPhone 4&5 display sizes only differ in height.
For developing app option two is better. Although you dont have to change all the images for iPhone 5 some images will me same like TabBarItem. But you have to create background image different for iPhone4 and 5.
Read out this apple document to create user interface.
And you can differentiate iPhone 4 from iPhone 5 using this code
UIBtton * btn = [[UIButton alloc] init];
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568)
{
btn.frame = CGRectMake(60,60, 205, 175);
//this is for iPhone5
}
else
{
btn.frame = CGRectMake(60,50, 199, 170);
//this is for iPhone4
}
Here my button place and length are different for iPhone 5 and iPhone 4
And set your control's hight and width according to iPhone Screen Size.
If you didn't, adjust your view layouts with proper auto resizing masks or look into Auto Layout if you only want to support iOS 6 going forward. You have to check height of [[UIScreen mainScreen] bounds].
Example:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
// code for 4-inch screen (iPhone 5)
} else {
// code for 3.5-inch screen (< iPhone 5)
}
Note that [UIImage imageNamed:#"yourImage.png"] will only load either "background.png" or "yourImage#2x.png", it will not load "yourImage-568h#2x.png" if it exists.
Check Auto-Rotation API as Well.

How to adjust app size when converting an iPhone app to a iPad app?

I have made a iPhone app that is on the app store. It's a simple view based application. It does have several XIBs and images. I have taken the necessary steps to convert the app to the iPad. It runs on the iPad simulator, but the size is too small. How do I adjust the size of the iPhone app for the iPad? If I need to do it by code, where would I implement the code?
Configure springs and struts in Interface Builder.
If that is not enough make device-based adjustments in code. E.g.
if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone) /* iPad */
{
self.someView.center = CGPointMake(100, 100);
}
else /* iPhone, iPod */
{
self.someView.center = CGPointMake(200, 200);
}
Typically, you would do that in controller's viewDidLoad method.
If you transitioned your iPhones target to iPad in Xcode it will have duplicated all xibs. You can open the -iPad versions in IB and realign everything from there. Programmatically, you can query the user idiom and position things differently depending on whether the current idiom is iPhone, retina or iPad.

Adding iPad XIB/VIEW to a "View Based Application" in iOS 4

I've created a View Based Application in XCode4 and trying to do the code and UI design according to the way Apple have intended it to be.
So I designed the UI in the AppNameViewController.xib and it works fine; now I want to enable it as a UNIVERSAL application that runs on iPad as well. I changed the App-Target>>Summary>>Devices from iPhone to Universal and miraculously XCode had automatically created MainWindow-iPad.xib (Apple, very nice...), but since I designed the application first screen on the AppNameViewController.xib and not on the MainWindow.xib when I run the app on iPad Simulator I get this ugly screen where my UI objects size and location is distorted (their size is still set for iPhone so they are all crumbled on the left-top corner of the screen).
In my previous app I used the code appearing below to distinct between the AppNameViewControllerForIPHONE.xib and the AppNameViewControllerForIPAD.xib; It was done from the AppDelegate.m file, but in the new XCode View Based Application template the AppDelegate doesn't go through initWithNibName method at all.
Code I used on XCode 3 that cannot be used on XCode 4:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
viewController = [[AppViewController alloc] initWithNibName:#"AppViewControllerIPAD" bundle:[NSBundle mainBundle]];
}
else {
viewController = [[AppViewController alloc] initWithNibName:#"AppViewControllerIPHONE" bundle:[NSBundle mainBundle]];
}
Long question... still, anyone had any idea how it should be done here? Where should I design the iPad UI or is there a way to easily transform the iPhone xib to an iPad one?
You have to follow the new naming scheme (idiom in Apple-speak) specified for Universal Applications for the NSMainNibFile key in your info.plist. For example, if you set your NSMainNibFile to be "MainWindow-iPhone", the xib for ipad would be "MainWindow-iPad".
The same naming convention should hold for the views in a view based application (I can't test right now, installing new xcode).

Best way for creating universal native iPhone/iPad application?

Which approach is good to create universal native application for iPhone/iPad:
1) Create separate NIB files for iphone and iPad versions and load the specific version by checking (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad).
2) Get the dimension of the main screen and frame the view accordingly
CGRect mainWindow = [[UIScreen mainScreen] bounds];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, mainWindow.size.width, mainWindow.size.height)];
Please suggest if any other approach that I missed
Thanks,
Ajay
I am using the first approach, in many cases, at least for my own it is sufficient to set autoresizing masks. You have not said anything about this, but if your application will look pretty much the same (up-scaled layout) this works fine. In the cases I need another layout I have checked whether it is an iPad/iPhone and loaded the correct NIB, or used the GUI code in loadView based on the device.
From you example code with the webview you could use autoresizing masks and it would resize to the correct proportions when the frame changes.

How do I write an iPhone/iPad app to take care of different screen sizes?

With the new iPad (which has a different screen resolution of the iPhone/iPod Touch), how do I set the application to automatically size appropriately to the desired screen size?
Read the iPad Programming Guide in the 3.2 SDK beta documentation. It will tell you everything you need. The NDA means we can't talk about it here publically.
Although the iPad SDK is still under NDA at this time, the iPhone SDK which preceeds it introduced the ability to change the screen size:
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)opts
{
UIScreen *s = [UIScreen mainScreen]; // ask for the main screen
[window setFrame:[s applicationFrame]]; // get the application Frame for the screen
return YES;
}
If you have used NIBs for the UI construction, you need to ensure that they're auto-resizable in order for them to flow properly.
The issue is not in the code. It is in the project conversion from iPhone to iPad: http://www.cocos2d-iphone.org/forum/topic/4108