This line of code always fail on iOS 9.2 Simulator, but it is ok on iOS 8.4 and iOS 10.1 Simulators
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft] forKey:#"orientation"];
It trows this error
XPC connection interrupted
Terminating since there is no system app.
I created a new MasterDetail Application in XCode and this is viewDidLoad method in the DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft] forKey:#"orientation"];
}];
}
Related
My app will be launched in Landscape,only one view can change to Portrait, in which there is a button,I click this button to change the orientation to Portrait.
It performed as expected on iPad(iOS 8.4,Deployment target: 7.0),and I made an iPhone version,in which I just made some adjust on UI,and it runs on iPhone 6 Plus simulator(Base SDK: 9.1,Deployment target: 7.0),but the button for chagning orientation doesn't work.
And I want to know how to implement this in iOS 9;
The Below is my code:
- (void)changeOrientation
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation==UIInterfaceOrientationLandscapeLeft||orientation==UIInterfaceOrientationLandscapeRight){
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
if (orientation==UIInterfaceOrientationLandscapeLeft) {
value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
}else {
value = [NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown];
}
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"ScreenToPortrait" object:nil];
}else{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
if (orientation==UIInterfaceOrientationPortrait) {
value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
}else {
value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
}
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"ScreenToLandscapeLeft" object:nil];
}
[UIViewController attemptRotationToDeviceOrientation];
}
This works on iOS9
NSNumber *orientationValue = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue: orientationValue forKey:#"orientation"];
Go to General tab from app target, and enable all orientations.
For all view which support only landscape orientation implement supportedInterfaceOrientations() and return landscape orientation value.
seen or set tick on General setting.
for i want to build a movie controller,and it`s pushed by a navigationController,how can i set it ,also only this vc can be landscape,others should only be portraitUp,please help,thanks in advance.
Try..
- (void)viewDidAppear:(BOOL)animated
{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
}
- (void)viewDidDisappear:(BOOL)animated
{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
}
I have an event that is called like this:
[[NSNotificationCenter defaultCenter] postNotificationName:VIDEO_REQUESTED object:nil userInfo:dictionary];
The event method IS called when I run the code on my iPad device or the ipad simulator.
In the iphone simulator however, the method IS NEVER called, it fails silently.
The method called is like this:
- (void)videoRequested:(NSNotification *) notification {
NSDictionary *dictionary = [notification userInfo];
NSString *stringValueToUse = [dictionary valueForKey:#"imgID"];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
[self dismissViewControllerAnimated:YES completion:nil];
} else {
//
[self.popoverController dismissPopoverAnimated:YES];
}
[AdColony playVideoAdForZone:#"-" withDelegate:self withV4VCPrePopup:YES andV4VCPostPopup:YES];
}
- (IBAction)buttonPressed:(id)sender
{
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
if ([[UIDevice currentDevice] isMultitaskingSupported]) {
[self beingBackgroundUpdateTask];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(batteryChanged:) name:#"UIDeviceBatteryLevelDidChangeNotification" object:device];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(batteryChanged:) name:#"UIDeviceBatteryStateDidChangeNotification" object:device];
[self currentBatteryState];
}
- (void) beingBackgroundUpdateTask
{
self->backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundUpdateTask];
}];
}
- (void) endBackgroundUpdateTask
{
[[UIApplication sharedApplication] endBackgroundTask: self->backgroundUpdateTask];
self->backgroundUpdateTask = UIBackgroundTaskInvalid;
}
For some reason, the notification's are not being observed. Am i doing something wrong? I want to observe for unto 10 minutes when unplugged
You shouldn't be calling endBackgroundUpdateTask in your buttonPressed: method, since that cancels your background task. Try removing this code:
if ([[UIDevice currentDevice] isMultitaskingSupported]) {
[self endBackgroundUpdateTask];
}
Also, you should pass the UIDeviceBatteryLevelDidChangeNotification constant to the "name" parameter, not the string. It should look like this (note the lack of double quotes):
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(batteryChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:device];
(It's also possible that UIDevice simply doesn't send those notifications in the background.)
Have you added the applicable backgrounding code to your AppDelegate? Here are backgrounding instructions in the iOS Programming Guide
My app has a battery level indicator in it, and I would like to have it update while still on the battery level indicator page. Currently, if I keep my app open to that screen and the battery level changes (by more than 5%), the app crashes. What would I need to add to my code to do that? Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
deviceType.text = [[UIDevice currentDevice] model];
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
[[UIDevice currentDevice] batteryLevel];
// Enable monitoring of battery status
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
// Request to be notified when battery charge or state changes
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(batteryStatus) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
}
- (void) viewWillAppear:(BOOL)animated {
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
LabelBatteryStatus.text = [NSString stringWithFormat:#"%.0f%%", device.batteryLevel * 100];
[device addObserver:self forKeyPath:#"batteryLevel" options:0x0 context:nil];
[super viewWillAppear:animated];
}
- (void) viewDidDisappear:(BOOL)animated {
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = NO;
[device removeObserver:self forKeyPath:#"batteryLevel"];
[super viewDidDisappear:animated];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
UIDevice *device = [UIDevice currentDevice];
if ([object isEqual:device] && [keyPath isEqual:#"batteryLevel"]) {
LabelBatteryStatus.text = [NSString stringWithFormat:#"%.0f%%", device.batteryLevel * 100];
}
}
This code works with my app when I load and unload the page, but I want it to work when on the page as well.
Thank you in advanced.