Ionic: ScreenOrientation to Portrait - ios

I am developing an Ionic-App and I want to set the Screen Orientation to Portrait.
and I know I could set it with this:
<preference name="orientation" value="portrait" />
but this isn't working,
I found a cordova plugin called screen-orientation-plugin but this is not working too. Do you have any another solution for this? Or Can anyone explain me how these 2 methods are working correctly?
.config(function($ionicConfigProvider, $stateProvider, $urlRouterProvider, $compileProvider) {
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
$scope.changeOriantationPortrait = function() {
screen.lockOrientation('portrait');
}
changeOriantationPortrait();
}

I have used this plugin for my project and it is worked perfectly
cordova plugin add net.yoik.cordova.plugins.screenorientation
Controller
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
$scope.changeOriantationLandspace = function() {
screen.lockOrientation('landscape');
}
$scope.changeOriantationPortrait = function() {
screen.lockOrientation('portrait');
}
}
HTML
<button class="button button-full button-positive " ng-click="changeOriantationLandspace()">Change To Landspace Mode</button>
<br/>
<button class="button button-full button-positive " ng-click="changeOriantationPortrait ()">Change To Portrait Mode</button>
Reference

Related

Status bar style in NativeScript

I'm trying to reproduce the effect seen on first page here: https://github.com/sebawita/starships, where there is a status bar with light content and no action bar.
I have been trying the instructions specified here: https://github.com/NativeScript/nativescript-angular/issues/1779#issuecomment-522586849
I'm working with NativeScript Vue rather than angular, but I don't suppose it matters. Here is my component code:
<template>
<Page
class="page"
ref="page"
statusBarStyle="light"
#loaded="onPageLoaded()">
<GridLayout rows="*, 60, 60, 20, 60, *">
<Label row="0" textWrap="true" class="header" text="Starship Service" />
<TextField
id="emailField"
hint="Email"
keyboardType="email"
returnKeyType="next"
autocorrect="false"
row="1"
class="textfield"
ios.clearButtonMode="1"
ref="field1" />
<TextField
row="2"
secure="true"
hint="Password"
returnKeyType="send"
class="textfield" />
<Button row="4" text="Log in" />
</GridLayout>
</Page>
</template>
<script>
import { topmost } from "tns-core-modules/ui/frame";
import { isIOS } from "tns-core-modules/platform";
export default {
methods: {
onPageLoaded: function(args) {
console.log('page loaded');
if (isIOS) {
Object.defineProperty(UIViewController.prototype, 'preferredStatusBarStyle', {
get: function () {
return this._preferredStatusBarStyle || UIStatusBarStyle.Default;
},
enumerable: true,
configurable: true
});
let controller = topmost().ios.controller;
controller._preferredStatusBarStyle = UIStatusBarStyle.Default;
controller.setNeedsStatusBarAppearanceUpdate();
}
console.log("page loaded done");
},
},
computed: {
message() {
return "Welcome to the first version of the starship app :-)";
}
}
};
</script>
<style scoped lang="scss">
.page {
background-image: url("https://raw.githubusercontent.com/sebawita/starships/master/icons/space-bg.jpg?raw=true");
color: #fff;
}
</style>
I have this in my Info.plist file:
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
You may natively set the status bar style on your page loaded events
HTML
<Page class="page" #loaded="onPageLoaded">
JS
onPageLoaded: function(args) {
let controller = args.object.frame.ios.controller;
controller.navigationBar.barStyle = 0; // `0` for Black or `1` for Light
}
You can also hide the action bar for each component.
import { Page } from 'tns-core-modules/ui/page/page';
constructor(
private page: Page) {
page.actionBarHidden = true;
}
Like was answered in another comment, you need to make sure your Info.plist file has this entry:
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>
The crucial bit of code needed was added in either NativeScript 6.4 or 6.5 here:
https://github.com/NativeScript/NativeScript/pull/8241/files
This the iOS preferredStatusBarStyle() function on the NativeScript Page object. NativeScript-Vue exposes this as a prop that you can pass in 'light' or 'dark' values to. See the updated Vue documentation.

Ionic Popover Works on XCode Simulator but not on iPhone

I have an Ionic Popover on my app. The popover appears when I run ionic serve, ionic emulate ios, and the XCode simulator. However, as soon as I simulate the app to my iPhone 4S on XCode or use the Ionic View app to view my own application, the popover does not appear. I've debugged EVERYTHING and the code does not work. No errors appear on my console when running the app.
Once in a blue moon the popover will appear on my 4S, but there is no logic to how the popover appears. I would change a piece of code, the popover appears, then when I change it again, the popover disappears. I repeat this process and go back to my old code version that worked and it doesn't work. This is frustrating. What's worse I fear no one will respond to this message. Any help as to why there is a discrepancy between the iPhone simulator and my actual iPhone would be great. Thanks.
Button HTML
<div ng-controller="FilterPopoverController as filterPopover" class="text-right">
<div on-tap="filterPopover.open()" ng-class="{filterButtonOpened: filterPopover.opened}" id="filter-button">
<span class="assertive" >
<i class="icon ion-arrow-down-b"></i>
<span class="bold">FILTER</span>
</span>
</div>
</div>
Popover HTML
<ion-popover-view id="filterPopover">
<ion-header-bar class="bar-dark">
<h1 id="popoverTitle" class="bold">FILTER BY</h1>
</ion-header-bar>
<ion-content>
<p>Content here</p>
</ion-content>
</ion-popover-view>
The Popover Controller
.controller('FilterPopoverController', filterPopoverController)
filterPopoverController.$inject = ['$ionicPopover', '$filter', '$scope', '$timeout'];
function filterPopoverController($ionicPopover, $filter, $scope, $timeout) {
var vm = this;
vm.open = open;
vm.popover = null;
vm.opened = false;
activate();
//Cleanup the popover when we're done with it!
$scope.$on('$destroy', function() {
vm.popover.remove();
vm.opened = false;
});
$scope.$on('popover.hidden', function() {
vm.opened = false;
});
function activate( ) {
$ionicPopover.fromTemplateUrl('/templates/search/filter-popover.html', {
scope: $scope
}).then(function(popover) {
vm.popover = popover;
});
}
function open( ) {
vm.opened = true;
vm.popover.show();
}
}
I've had to remove sensitive info from some of this code but this is the gist of it.
I have made two modifications to your posted code:
first one is to change the path of popover template to be:
'templates/search/filter-popover.html'
instead of
'/templates/search/filter-popover.html'
You need to reference this file starting from current directory instead of root directory
Second changed is to pass $event input while opening the popover, this is from official documentation of ionic Popover
After applying both of these changes to the posted code, I manage to see popover on desktop browser, ios simulator, real iPhone 4 consistenly
Here is the final code:
angular.module('starter', ['ionic'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
.controller('FilterPopoverController', filterPopoverController)
filterPopoverController.$inject = ['$ionicPopover', '$filter', '$scope', '$timeout'];
function filterPopoverController($ionicPopover, $filter, $scope, $timeout) {
var vm = this;
vm.open = open;
vm.popover = null;
vm.opened = false;
activate();
//Cleanup the popover when we're done with it!
$scope.$on('$destroy', function() {
vm.popover.remove();
vm.opened = false;
});
$scope.$on('popover.hidden', function() {
vm.opened = false;
});
function activate( ) {
$ionicPopover.fromTemplateUrl('templates/search/filter-popover.html', {
scope: $scope
}).then(function(popover) {
vm.popover = popover;
});
}
function open( $event ) {
vm.opened = true;
vm.popover.show($event);
}
}
<div ng-controller="FilterPopoverController as filterPopover" class="text-right">
<div on-tap="filterPopover.open($event)" ng-class="{filterButtonOpened: filterPopover.opened}" id="filter-button">
<span class="assertive" >
<i class="icon ion-arrow-down-b"></i>
<span class="bold">FILTER</span>
</span>
</div>
</div>
I hope that this solves your problem.

Phonegap + Jquery Mobile: initialization / registration best practices

I am developing an Phonegap (3.3.0) + Jquery Mobile (1.4) app.
I get an infinite loading page (white page with ui-loader icon). This is erratic and sometimes the app starts well.
I see a very strange bug: none of the first "console.logs" I use in my js file are displayed in the Phonegap Build Weinre debug console.
Only after a certain line (which contain by the way the first asynchronous function) the console.log are displayed in the Weinre console.
So I guess I have a binding order problem related to Jquery Mobile and Phonegap, but I can't find what's wrong in my initialization.
Can I be also due to the order in which I call js files in my index.html ?
I followed this post to register Phonegap and JQM : Correct way of using JQuery-Mobile/Phonegap together?
recommended here : jQuery Mobile : What is the order of page events triggering?
by #Gajotres.
Can you help ?
Thanks
HTML:
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<!-- SPLASH PAGE -->
<div id="splash-page" data-role="page">
<div class='toastenjs' style='display:none'></div>
<center id="splashLogoCenter">
<img src="images/splash.png" width="200" />
</center>
</div>
<!-- WELCOME PAGE -->
<div id="welcome-page" data-role="page">
...
</div>
<script src="js/jquery-1.9.1.js"></script>
<script src="js/jquery.jsonp-2.4.0.min.js"></script>
<script src="js/functions.js"></script>
<script src="js/functionCUgly.js"></script>
<script src="js/boardDims.js"></script>
<script src="phonegap.js"></script>
<script src="js/jquery.mobile.config.js"></script>
<script src="js/jquery.mobile-1.4.3.min.js"></script>
<!--POUCHDB -->
<script src="js/pouchdb-2.2.3.min.js"></script>
<!-- Flexslider-->
<!-- <script src="js/flexslider-v2.js"></script>--> <!-- v2.2 doesn't work, maybe because we're not using last versions of jquery and jqm -->
<script src="js/flexsliderV2.3.js"></script>
<!-- iScroll-->
<script type="application/javascript" src="js/iscroll.js"></script>
<script type="application/javascript" src="js/jquery.mobile.iscrollview.js"></script>
<!-- Add2home : create a shortcut icon of the wep app on the phone homescreen -->
<script type="application/javascript" src="js/add2home.js"></script>
<script src="js/GoogleLogin.js"></script> <!--Phonegap module by eric valenzia https://github.com/valenzia10/PhonegapGoogleLogin-->
<script src="js/jquery.ddslick.min.js"></script>
<script src="js/jquery-geturlvar.js"></script>
<script src="js/html2canvas.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
if (typeof(google) != 'undefined'){
google.load('visualization', '1.0', {'packages':['corechart']});
}
</script>
JS file:
var deviceReadyDeferred = $.Deferred();
var jqmReadyDeferred = $.Deferred();
$(document).one("mobileinit", function () {
console.log('mobileinit just fired'); //this one is not displayed in the weinre console
jqmReadyDeferred.resolve();
});
if ( isPhoneGap() ) {
document.addEventListener("deviceReady", onDeviceReady, false);
function onDeviceReady() {
deviceReadyDeferred.resolve();
}
$.when(deviceReadyDeferred, jqmReadyDeferred).then( EVERYTHING() ); // !!!!! normalement il faut virer ces parenthèses pour respecter le $.when....mais ça fait tout bugger !!!!!!!!!
} else {
console.log("NOT Running on PhoneGap!");
$.when(jqmReadyDeferred).then( EVERYTHING );
}
function EVERYTHING() {
console.log("on est entré dans EVERYTHING()"); //not displayed in the weinre console
insideEVERYTHING = 1;
console.log("jqmReadyDeferred is "+jqmReadyDeferred.state()); //not displayed in the weinre console
console.log("deviceReadyDeferred is "+deviceReadyDeferred.state()); //not displayed in the weinre console
//FOR EVERY PAGE
$(document).on('pagecontainershow', function (e, ui) {
//...
});
$(document).on('pagecontainershow', function (e, ui) {
//...
});
// --------------- SPLASH PAGE ---------------------
//$(document).on('pagecreate','#splash-page', function(){
$(document).on('pagecontainershow', function (e, ui) {
var activePageId = $(':mobile-pagecontainer').pagecontainer('getActivePage').attr('id');
if (activePageId === 'splash-page') {
console.log("we are in splash-page");
if (typeof debugOptionUseLocalDB != 'undefined' && debugOptionUseLocalDB) {
fallbackToLocalDBfile();
console.log('on yess');
}else{
if(connectionStatus == 'online'){
console.log("launching getJsonpFile...");
//DEBUG TIMER
var time=[];
var dummy;
dummy = new Date().getTime();
time.push(dummy);
getJsonpFile(dbUrl())
.done(function(data) {
console.log("...getJsonpFile done.");
if(localStorage) {
if ( isPhoneGap() || !isIOS() ) { //BUG iOS safari doesn't work with this (Cf. Philippe's ipad), si on est sur phonegap ok, si on n'est pas sur phonegap et pas sur iOS ok
localStorage.setItem("proDB", JSON.stringify(data)); //write to localStorage
}
}
//...JQM bindings are continued below
The best registration is the following :
var isPhoneGap;
var deviceReadyDeferred = $.Deferred();
var jqmReadyDeferred = $.Deferred();
isPhoneGap = checkIfPhoneGap();
if ( isPhoneGap ) {
$.when(deviceReadyDeferred, jqmReadyDeferred).done( Everything );
} else {
console.log("NOT Running on PhoneGap!");
$.when(jqmReadyDeferred).done( Everything );
}
$(document).on("mobileinit", function () {
//alert('mobileinit just fired');
//popShortToast("mobileinit just fired");
jqmReadyDeferred.resolve();
});
document.addEventListener("deviceReady", onDeviceReady, false);
function onDeviceReady() {
//popShortToast("deviceReady just fired");
deviceReadyDeferred.resolve();
}
function checkIfPhoneGap() {
var app = document.URL.indexOf( 'http://' ) === -1 && document.URL.indexOf( 'https://' ) === -1; // && document.URL.indexOf( 'file://' );
if ( app ) {
return true;
} else {
return false;
}
}
function Everything() {
//enter your JQM bindings here, and use Phonegap's features
}

Getting Geolocation working with JQM/phone gap

I have an application using phonegap and jQM. i'm trying to get my geolocation to work when clicking the button but i get an error in the console saying geolocation is not defined.
The error is pointing to the onclick="getLocation()". Can anyone help me please?
Heres an example of my code:
<div data-role="page" id="geo" data-add-back-btn="true" onload="onBodyLoad()">
<header data-role="header" data-position="fixed">
<h1>Geolocation</h1>
</header>
<div data-role="content">
<button data-role="button" onclick="getLocation()">Click to get your current position</button>
<div id="mapholder"></div>
</div>
</div>
$("#geo").on('pageinit', function () {
console.log("Geo page loaded!");
function onBodyLoad(){
document.getElementById('geolocation').empty();
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady(){
phoneGapReady.innerHTML = ("")
}
var x=document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition,showError);
}else {
x.innerHTML="Your Browser does not support Geolocation.";}
}
function showPosition(position) {
lat=position.coords.latitude;
lon=position.coords.longitude;
latlon=new google.maps.LatLng(lat, lon);
mapholder=document.getElementById('mapholder')
mapholder.style.height='250px';
mapholder.style.width='100%';
var myOptions= {
center:latlon,zoom:14,
mapTypeId:google.maps.MapTypeId.ROADMAP,
mapTypeControl:false,
navigationControlOptions:
{style:google.maps.NavigationControlStyle.SMALL}
};
var map = new
google.maps.Map(document.getElementById("mapholder"),myOptions);
var marker=new
google.maps.Marker({position:latlon,map:map,title:"Your Location."});
}
function showError(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED:
x.innerHTML="User did not allow Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML="Location Data is not Available."
break;
case error.TIMEOUT:
x.innerHTML="Request Timed Out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML="Unknown Error."
break;
}
}
});
Assuming you're using Phonegap 3.x, make sure you've added the geolocation plugin to your project:
phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation.git
Make sure the config.xml includes it:
<feature name="Geolocation">
<param name="android-package" value="org.apache.cordova.GeoBroker" />
</feature>
Make sure you allow access in the android manifest:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
Try changing
if (navigator.geolocation) {
to
if (typeof(navigator.geolocation) != "undefined") {

Inappbrowser not loading

Im using Phonegap 2.9.0 in ios, i have gone through the tutorial for inappbrowser .and i have tried the following to in my application
App.js Calling method
indexpnl.element.on({
tap: function (e) {
var el = Ext.get(e.target);
var bval = el.getId();
if (bval == "btnfacebook") {
// onDeviceReady()//using this application screen its self loading the site view not in inappbrowser
document.addEventListener("deviceready",onDeviceReady, false);//here nothing happening
}
}
});
jsfunction.js Method
function onDeviceReady()
{
var ref = window.open('http://www.facebook.com', '_blank', 'location=yes');
ref.addEventListener('loadstart', function() { alert('start: ' + event.url); });
ref.addEventListener('loadstop', function() { alert('stop: ' + event.url); });
ref.addEventListener('exit', function() { alert(event.type); });
}
And i have include the plug in config.xml
<feature name="InAppBrowser">
<param name="ios-package" value="CDVInAppBrowser" />
</feature>
When i call using the above method nothing happens.When i call On onDeviceReady() the site is loading in application itslef but not in the inapp browser.What wrong with my code
Dunno but have you checked you have a
<script src="phonegap.js"></script>
in your script as in
https://build.phonegap.com/docs/plugins-using
Leaving it out can produce those kind of errors

Resources