NativePHP is currently in alpha development
Let's get to beta!#The System
One of the main advantages of building a native application is having more direct access to system resources, such as peripherals connected to the physical device and APIs that aren't typically accessible inside a browser's sandbox.
NativePHP makes it trivial to access these resources and APIs.
One of the main challenges - particularly when writing cross-platform apps - is that each operating system has its own set of available APIs, along with their own idiosyncrasies.
NativePHP smooths over as much of this as possible, to offer a simple and consistent set of interfaces regardless of the platform on which your app is running.
While some features are platform-specific, NativePHP gracefully handles this for you so that you don't have to think about whether something is Linux-, Mac-, or Windows-only.
Most of the system-related features are available through the System
facade.
1use Native\Laravel\Facades\System;
#Encryption / Decryption
Almost every non-trivial application will require some concept of secure data storage and retrieval. For example, if you want to generate and store an API key to access a third-party service on behalf of your user.
You shouldn't ship these sorts of secrets with your app, but rather generate them or ask your user for them at runtime.
But when your app is running on a user's device, you have far less control and fewer guarantees over the safety of any secrets stored.
On a traditional server-rendered application, this is a relatively simple problem to solve using server-side encryption with keys which are hidden from end users.
For this to work on the user's device, you need to be able to generate and store an encryption key securely.
NativePHP takes care of the key generation and storage for you, all that's left for you to do is encrypt, store and decrypt the secrets that you need to store on behalf of your user.
NativePHP allows you to encrypt and decrypt data in your application easily:
1if (System::canEncrypt()) {2 $encrypted = System::encrypt('secret_key_a79hiunfw86...');3 4 // $encrypted => 'djEwJo+Huv+aeBgUoav5nIJWRQ=='5}
You can then safely store the encrypted string in a database or the filesystem.
When you need to get the original value, you can decrypt it:
1if (System::canEncrypt()) {2 $decrypted = System::decrypt('djEwJo+Huv+aeBgUoav5nIJWRQ==');3 4 // $decrypted = 'secret_key_a79hiunfw86...'5}
#TouchID
For Mac systems that support TouchID, you can use TouchID to protect and unlock various parts of your application.
1if (System::canPromptTouchID() && System::promptTouchID('access your Contacts')) {2 // Do your super secret activity here3}
You must pass a string $reason
as the only argument to System::promptTouchID
. This will show up in the dialog that
TouchID users are familiar with:
Using this, you can gate certain parts of your app, or your entire application, allowing you to offer an extra layer of protection for your user's data.
Note: Despite the name, TouchID only gives you greater confidence that the person using your app is the same as the person who has unlocked the device your app is installed on. It does not allow you to identify that user, nor does it give you any special privileges to their system.
#Printing
You can list all available printers:
1@use(Native\Laravel\Facades\System)2 3@foreach(System::printers() as $printer)4 {{ $printer->displayName }}5@endforeach
Each item in the printers array is a \Native\Laravel\DataObjects\Printer
which contains various device details and
default configuration.
You can send some HTML to be printed like this:
1System::print('<html>...', $printer);
If no $printer
object is provided, the default printer and settings will be used.
You can change the configuration before sending something to be printed, for example if you want multiple copies:
1$printer->options['copies'] = 5;2 3System::print('<html>...', $printer);
You can also print directly to PDF:
1System::printToPDF('<html>...');
This returns the PDF data in a base64_encoded
binary string. So be sure to base64_decode
it before storing it to disk:
1use Illuminate\Support\Facades\Storage;2 3$pdf = System::printToPDF('<html>...');4 5Storage::disk('desktop')->put('My Awesome File.pdf', base64_decode($pdf));
#Time Zones
PHP and your Laravel application will generally be configured to work with a specific time zone. This could be UTC, for example.
But users of your application will think about time differently. Normally, the user's perspective of time is reflected in their operating system's time zone setting.
NativePHP includes a mechanism to translate cross-platform time zone identifiers to consistent identifiers that PHP expects to use.
You can use this to show dates and times in the appropriate time zone without having to ask your users to manually select their current time zone.
Note: In some cases, this mechanism may not select the exact time zone that the user is in. It uses an approximation to simplify things, as there are many overlapping time zones and methods of naming them.
Using this approach, your app will be responsive to changes in the system's time zone settings, e.g. in case the user moves between time zones.
Get the current system time zone:
1$timezone = System::timezone();2 3// $timezone => 'Europe/London'