Phonegap-ipad app -Adjust Launch image acc to Orientation - objective-c

I am trying to explore ipad dev using phonegap. The problem that i have now is,
I have four different launch images for different orientations. When the app is launched, the correct launch image is being shown for a moment. After this,phonegap tries to load a imageview with default.png and displays it till webview is fully loaded. The problem lies here.. The imageview is autorotated based on the current orientation. So if the current orientation is LandscapeLeft, imageview tries to rotate default.png before displaying it Which is not i wanted and that is why i have different launch images.So in effect, you will have a landscapeleft.png and then the auto-rotated default.png before i get to see the webview.
So i tried changing the phonegapdelegate like this (in applicationDidFinishLaunching)
UIImage* image=nil;
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight ){
NSLog(#"In default5");
image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Default5" ofType:#"png"]];
}
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ){
NSLog(#"In default6");
image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Default6" ofType:#"png"]];
}
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown ){
NSLog(#"In default7");
image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Default7" ofType:#"png"]];
}
imageView = [[UIImageView alloc] initWithImage:image];
[image release];
It didn't work and upon debugging i found that the statusbarorientation is always portrait. That may be because phonegap tries to set portrait as the statusbarorientation in the same applicationDidFinishLaunching method(before loading this image view).
Can someone tell me how to load the image view with correct image?

USE THESE NAMES:
Default-LandscapeLeft-ipad.Png
Default-LandscapeRight-ipad.Png
Default-Portrait-ipad.Png
Default-PortraitUpsideDown-ipad.Png
More: http://www.weston-fl.com/blog/?p=840

applicationDidFinishLaunching is too early for the app to receive orientation change notifications from the os. Hence you cannot determine the orientation here.
So the best possible solution is to do it in a lifecycle stage which is capable of determining the current orientaion.In my case, webViewDidStartLoading

Related

Enable rotate for mwphotobrowser in portrait app

I have a fixed portrait app using objective-c, and i have a button click for open MWPhotoBrowser for browsing images. Now It works well in portrait mode but I want to make the MWPhotoBrowser for both portrait and landscape mode for rotate the phone. How can I do that, here is my code:
- (void) prepareForPhotoAlbum:(NSArray*)photoObjs {
self.photoURLArrays = [[NSMutableArray alloc]init];
for(int i=0 ; i<photoObjs.count ; i++){
NSDictionary* dictOfImageObj = photoObjs[i];
[self.photoURLArrays addObject:[MWPhoto photoWithURL: [NSURL URLWithString:[NSString stringWithFormat:#"%#/%#",ServerTopicImageURL,dictOfImageObj[#"filename"]]]]];
}
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
// Set options
browser.displayActionButton = YES;
browser.displayNavArrows = NO;
browser.zoomPhotosToFill = YES;
[browser setCurrentPhotoIndex:0];
browser.wantsFullScreenLayout = YES;
[self.navigationController pushViewController:browser animated:YES];
[browser showPreviousPhotoAnimated:YES];
[browser showNextPhotoAnimated:YES];
}
You have 2 options, you can enable the app to be portrait + landscape and go through your VCs and add the 3 rotation methods, shouldAutorotate, preferredOrientation and presentationOrientation, OR, you on this specific VC you can register for NotificationCenter notifications for device orientation, and even though the phone orientation may be locked and / or the apps orientation locked you should get a notification of the rotation happening and apply your own animation transformation to make it appear landscape, this is how most camera apps seem to do it as far as I am aware, becuase you dont want a view port of the camera rotating.
NotificationCenter.default.addObserver(self, selector: Selector, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)

UIImageView Won't Show When ViewController Added As Subview

My main aim is to have one background for all of my ViewControllers. Every ViewController I have has a clear background.
To do this, I have made one UIViewController (called backgroundViewController) that will act as the subview to all my other ViewControllers. It has one UIImageView which displays this particular background. I will then add this backgroundViewController as a subview of all my other ViewControllers.
The problem is - this imageView won't show as a subview!
This is how I display the imageView:
- (void)viewDidLoad
{
if ([musicPlayer playbackState] == MPMusicPlaybackStateStopped) {
UIImage *artworkBackgroundView;
artworkBackgroundView = [UIImage imageNamed:#"noArtworkBackground"];
UIImage *effectImage = nil;
backgroundView.image = effectImage;
[backgroundView setImage:artworkBackgroundView];
backgroundView.image = effectImage;
}
}
- (void) handle_NowPlayingItemChanged: (id) notification
{
if ([musicPlayer playbackState] != MPMusicPlaybackStateStopped) {
// Get artwork for current now playing item
MPMediaItem *currentItem = [musicPlayer nowPlayingItem];
MPMediaItemArtwork *artwork = [currentItem valueForProperty: MPMediaItemPropertyArtwork];
UIImage *artworkBackgroundView = [artwork imageWithSize: CGSizeMake(618, 618)];
if (!artworkImage) {
artworkBackgroundView = [UIImage imageNamed:#"noArtworkBackground"];
artworkImage = [UIImage imageNamed:#"noArtwork"];
}
[backgroundView setImage:artworkBackgroundView];
}
}
As you can see, backgroundView changes each time the music player skips song.
To test that backgroundViewController does show as a subview, I added another imageView and changed its image to a static .png in Interface Builder, and that shows correctly as a subview.
This is how I make it a subview for other ViewControllers:
backgroundViewController *backgroundVC = [[backgroundViewController alloc] initWithNibName:#"backgroundViewController" bundle:nil];
[self.view insertSubview:backgroundVC.view atIndex:0];
What I want is the UIImageView called backgroundView to show up when it is being called as subview.
I have tested that backgroundView does change according to what song is playing, and it works correctly.
What am I doing wrong? backgroundView refuses to show up as a subview?! I've searched a ton about adding ViewControllers as subviews but I can't find a similar problem.
Any help would be much appreciated, thanks! :)
This worked for me:
backgroundViewController *backgroundVC = [[backgroundViewController alloc] initWithNibName:#"backgroundViewController" bundle:nil];
[self addChildViewController:backgroundVC];
[self.view addSubview:backgroundVC.view];
I found the solution here. I need to read more about 'Custom Containers'.

UIActivityViewController messes the background on orientation change

I am having a native app, in which I have to show Share feature with UIActivityViewController. My app needs to support both orientations. When the share pops up and I change the device orientation, then the orientation for popup changes but the background screen remains its orientation and when I cancel it out then the background screen looks messy.
Please let me know how to fix this as my app is in release phase and I am stuck with this issue.
NSString *textToShare = #"your text";
NSArray *itemsToShare = #[textToShare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];
[activityVC setCompletionHandler:^(NSString *activityType, BOOL completed){
IsAppLoadedFirstTime=false;
[infoImageabc1 removeFromSuperview];
[InfoLabel1 removeFromSuperview];
[ InfoLabel2 removeFromSuperview];
[InfoShare removeFromSuperview];
[self loadView];
}];
[self presentViewController:activityVC animated:YES completion:nil];
I can change the orientation as soon as the activity popup closes, but before the (when the popup is opened), then the UI is not in proper orientation say if after opening the popup I changed the orientation to Portrait, then my UI remains in Landscape mode, but as it includes text also, so UI looks bad.

Transparent webview: underlying layer sometimes not visible

In my phonegap-based iPhone web-app I implemented a plugin that uses AVCaptureVideoPreviewLayer to take a photo. To do so, when the plugins' startCamera method is called, I set the background of the webview to be transparent and insert the video capture layer below the layer of the webview. This works as expected (most of the time).
But for some strange reason, when I execute startCamera for the first time (after a fresh app start), the video layer isn't visible. Instead, the webview displays a white background, although the background color is set to clearColor. For all subsequent executions, the video layer is visible.
This is what I'm doing to show the camera:
AVCaptureSession * session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetHigh;
AVCaptureVideoPreviewLayer * videoLayer =
[[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
videoLayer.frame = self.webView.bounds;
CALayer *webViewLayer = self.webView.layer;
[webViewLayer.superlayer insertSublayer:videoLayer below:webViewLayer];
// ... session setup excluded
[session startRunning];
[self.webView setBackgroundColor:[UIColor clearColor]];
[self.webView setOpaque:NO];
In stopCamera() I do the following:
if (session) {
[session stopRunning];
}
[self.webView setBackgroundColor:[UIColor blackColor]];
[self.webView setOpaque:NO];
if (videoLayer != nil) {
[videoLayer removeFromSuperlayer];
}
Any ideas why the camera layer isn't visible for the first time?
Solved it: The problem was, that setting the opacity flag of the webview to NO didn't have any effect when it was done in startCamera(). To fix it, I set the opacity of the webview earlier - just when it was created. A webview with no opacity doesn't mean that it is transparent though - you also need to set the background color to [UIColor clearColor] (this is what is done in startCamera(); in stopCamera() the background color is set back to [UIColor blackColor]).

applicationMusicPlayer volume notification

I am using an applicationMusicPlayer and when i try to change the volume appear the visual notification, as shown in the picture.
Here the code I am using:
[MPMusicPlayerController applicationMusicPlayer] setVolume:newVolune];
Anyone knows how to hide this notification?
I don't know where the docs says so, but if you add a MPVolumeView view to your app the system volume overlay goes away. Even if it is not visible:
- (void) viewDidLoad
{
[super viewDidLoad];
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame: CGRectZero];
[self.view addSubview: volumeView];
[volumeView release];
...
}
You can use the hardware volume buttons, the setVolume method or directly interact with the control (if visible) that the overlay doesn't show up.
For iOS6 I had to set an image with alpha 0 and non-zero size to the MPVolumeView's image fields in order to get the default volume change notification to disappear.
// hide the hardware volume slider
UIImage *thumb = [[UIImage alloc] initWithCIImage:[UIImage imageNamed:#"volumeHider"].CIImage scale:0.0 orientation:UIImageOrientationUp];
MPVolumeView *hwVolume = [[MPVolumeView alloc] initWithFrame:self.frame];
[hwVolume setUserInteractionEnabled:NO];
hwVolume.showsRouteButton = NO;
[hwVolume setVolumeThumbImage:thumb forState:UIControlStateNormal];
[hwVolume setMinimumVolumeSliderImage:thumb forState:UIControlStateNormal];
[hwVolume setMaximumVolumeSliderImage:thumb forState:UIControlStateNormal];
[self addSubview:hwVolume];
This made the MPVolumeView be "visible" on the screen, but invisible to the user.
I encountered the same issue recently. Instead of adding the MPVolumeView to current view controller's view, I add it to the application's window once at the start of the app:
CGRect rect = CGRectMake(-500, -500, 0, 0);
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:rect];
[self.window addSubview:volumeView];
This works in both iOS 7 and 8.
Swift 3
You can hide the System MPVolumeView using
override func viewDidLoad() {
super.viewDidLoad()
let volumeView = MPVolumeView(frame: CGRect.zero)
self.view.addSubview(volumeView)
}
I had success with this in iOS 6. Although it wouldn't perform well. It caused quite a bit of lag when sliding the thumbImage. I did have to take out the last 2 lines of code in order for this to work.
[volumeView release];
...
For me, on iOS 7, none of above solutions worked. Here is how I did it:
_volume = [[MPVolumeView alloc] initWithFrame: CGRectMake(-100,-100,16,16)];
_volume.showsRouteButton = NO;
_volume.userInteractionEnabled = NO;
[self.view addSubview:_volume];
[_volume release];
That is, simply set MPVolumeView's frame to an off-screen location such as (-100,-100).