Browser is an important part of running the web application. But for all web applications, the important part is the browser’s permission. The browser asks for permission whenever we try to access the application which contains notifications, camera access, microphone, etc.
But have we ever tried to deny those permissions and run the application? Or we have asked the permission deliberately to set the permission for the application.
How browsers ask permission whenever we try to access the web application.
Let’s consider the application using a camera and microphone.
If you want the browser to ask permission
navigator.mediaDevices.getUserMedia() .then((stream)=>{ stream.getTracks().forEach((track)=>{ console.log(track); }); })
returns MediaStream which contains media devices information such as deviceId, deviceName, enabled, etc.
console.log(track) prints all the devices connected and their permissions.
If you want to ask permission for only audio then:
let params = { audio: true};
For both audio and video:
let params = { audio: true, video: true };
To set the camera by default front or rear, we can set facingMode
let params = { audio: true, video: { facingMode: { exact: "environment" } } };
For Front camera view “facingMode”: “front”
navigator.mediaDevices.getUserMedia(params) .then((stream)=>{ stream.getTracks().forEach((track)=>{ console.log(track); }); })
Get deviceId and set the device to change the camera view
enumerateDevices() is a function used to get device data.
navigator.mediaDevices.enumerateDevices() .then(function(devices) { devices.forEach(function(device) { console.log(device.kind + ": " + device.label + " id = " + device.deviceId); }); }) .catch(function(err) { console.log(err.name + ": " + err.message); });
deviceId can be used to switch cameras or any device which is connected to the system.
How to get state if permission is given manually
Navigators.permissions detect browser permission which is set manually by the user.
navigator.permissions.query({name:'camera'}) .then(function(result) { if (result.state === 'granted') { showMap(); } else if (result.state === 'prompt') { showButtonToEnableMap(); }
Result.state returns the value as “granted”,”prompt”,”denied”.
name param contains values such as ‘camera’,’geolocation’,’bluetooth’ , ‘microphone’, ‘notifications’ etc.
Let’s consider an example that shows how permissions can be handled.
Example :
let errorCallback = (error)=> { if ((error.name == 'NotAllowedError') || (error.name == 'PermissionDismissedError')) { return false; } }; if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { navigator.mediaDevices.getUserMedia({video:true,audio:true}) .then((success)=>{ if(navigator.mediaDevices || navigator.getUserMedia){ let navigator : Navigator; navigator.permissions.query({name: "camera"}) .then(({state}) => { if(state == 'granted'){ console.log(“permission granted!”); }else if(state == 'denied'){ console.log(“permission denied!”); } }) } },errorCallback); }
In the above example, we are checking if getUserMedia function is applicable for the browser. After that, we are asking for permission for ‘camera’ and ‘microphone’ bypassing params {video: true, audio: true}.
After asking permission we are checking if permission is granted using the navigator.permissions.query we can do further execution.
Let’s consider an example of how to switch cameras
Html file
<div id="video"></div> <button id="cameraButton" type"button">Switch Camera</button>
Typescript file
public videoTag = document.getElementById("video"); Public cameraBtn = document.getElementById("cameraButton"); this.cameraBtn.addEventListener('click', event => { const constraints = { video: true, audio: false, facingMode : “environment” }; navigator.mediaDevices.getUserMedia(constraints) .then(stream => { videoTag.srcObject = stream; }) .catch(error => { console.error(error); }); });
In the above example, we will switch the camera by clicking the switch camera button. By default, we have given the rear camera view, on click we can change the camera.
constraints.facingMode = “user”;
navigator.mediaDevices.getUserMedia will return device information and we will assign that stream to the video object which will switch the camera easily.
I have used a camera as an example, but instead, you can set any property and use it to ask for permission for your web application.
Refer link for more information – Navigator
I hope you like this blog and surely help you to make use of permission for web applications.
How to Effectively Hire and Manage a Remote Team of Developers.
Download NowMindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, SafeZone Safety Systems
Mindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and MindBowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Our CISO was extremely impressed by Mindbowser’s work. It is pretty rare to see this kind of clean security report so early in the company’s journey. Huge Thank you for the disciplined approach here.
Founder, TrestleIQ
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
The flexibility and capacity of the Mindbower staff has been impressive.
CEO, ProofPilot
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
MindBowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
As a founder of a budding start-up, it has been a great experience working with Mindbower Inc under Ayush's leadership for our online digital platform design and development activity.
Founder, Courtyardly
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork