c++ code and tutorial

Categories:
c++
computer programing
cpp
We don't advocate re-creating websites in mobile apps. You run the risk of violating Apple's terms for an app that isn't unique or "app-like", and more importantly, users likely will avoid your app if you are simply re-creating the same web experience. It's an opportunity to get creative! 👨🎨
fetch
call is standard fare.While a deep dive of the WordPress API is out of the scope of this article, suffice it to say the API is well documented.
Read all about the features provided by NativeScript Sidekick in this "week of Sidekick" blog series.
category
and the second post
.home-page.xml
(comes with blank template)category-page.xml
(you created this)post-page.xml
(you also created this)It's a good time to note that a completed version of this app is available here on Github if you get lost!
main-page
view is just going to be a button. Because who doesn't love a good button?/home/home-page.xml
file just needs some simple layout code with a button:<Page xmlns="http://schemas.nativescript.org/tns.xsd"
class="page bg"
backgroundSpanUnderStatusBar="true">
<Page.actionBar>
<ActionBar title="WordPress + NativeScript = ❤️" class="action-bar">
</ActionBar>
</Page.actionBar>
<StackLayout class="p-20">
<Label text="WordPress Demo" class="h1 text-center m-t-30 heading"/>
<Button text="Load Categories" tap="showCategories" class="btn btn-primary btn-active"/>
</StackLayout>
</Page>
home-page.js
file needs a little plumbing to wire up the button to send us to the next view, category-page
:var frameModule = require('ui/frame');
exports.showCategories = function() {
var navigationEntry = {
moduleName: './category/category-page',
animated: true
};
var topmost = frameModule.topmost();
topmost.navigate(navigationEntry);
};
/category/category-page.xml
and replace the existing code with the following NativeScript ListView
(including an item template) like so:<Page xmlns="http://schemas.nativescript.org/tns.xsd"
class="page bg"
loaded="pageLoaded">
<Page.actionBar>
<ActionBar title="WordPress Categories" icon="" class="action-bar">
<NavigationButton text="Back" android.systemIcon="ic_menu_back" />
</ActionBar>
</Page.actionBar>
<ListView id="listview" items="{{ items }}" class="list-group">
<ListView.itemTemplate>
<StackLayout class="list-group-item" id="{{ id }}" tap="showPost">
<Label text="{{ name }}" class="wp-category" />
<Label text="{{ description }}" textWrap="true" class="wp-subtitle" />
</StackLayout>
</ListView.itemTemplate>
</ListView>
</Page>
category-page.js
, contains two functions. pageLoaded
is, not surprisingly, executed when the page is loaded, and showPost
will navigate us to the next view (post-page
), retaining the context of the category the user tapped:var frameModule = require('ui/frame');
var Observable = require('data/observable').Observable;
var ObservableArray = require('data/observable-array').ObservableArray;
var page;
var items = new ObservableArray([]);
var pageData = new Observable();
exports.pageLoaded = function(args) {
page = args.object;
page.bindingContext = pageData;
fetch('https://demo.wp-api.org/wp-json/wp/v2/categories')
.then(response => {
return response.json();
})
.then(function(r) {
pageData.set('items', r);
});
};
exports.showPost = function(args) {
var navigationEntry = {
moduleName: './post/post-page',
animated: true,
context: { id: args.view.id }
};
var topmost = frameModule.topmost();
topmost.navigate(navigationEntry);
};
category-page.js
is the fetch API. fetch
allows us to request data from a remote endpoint and return it in JSON, making it easily consumable in our app!You'll also quickly notice the API endpoint we are using is leveraging the WordPress demo dataset. With a lot of lorem ipsum.
"id":2,
"count":3,
"description":"Neque quibusdam nihil sequi quia et inventore",
"link":"https:\/\/demo.wp-api.org\/category\/aut-architecto-nihil\/",
"name":"Aut architecto nihil",
"slug":"aut-architecto-nihil",
"taxonomy":"category",
"parent":0,
...
post/post-page.xml
with another ListView
:<Page xmlns="http://schemas.nativescript.org/tns.xsd"
class="page bg"
navigatedTo="pageNavigatedTo">
<Page.actionBar>
<ActionBar title="WordPress Posts" icon="" class="action-bar">
<NavigationButton text="Back" android.systemIcon="ic_menu_back" />
</ActionBar>
</Page.actionBar>
<ListView id="listview" items="{{ items }}" class="list-group">
<ListView.itemTemplate>
<StackLayout class="list-group-item" link="{{ link }}" tap="loadWebSite">
<Label text="{{ title.rendered }}" class="wp-subtitle" />
</StackLayout>
</ListView.itemTemplate>
</ListView>
</Page>
post-page.js
code behind powering the view - and containing another two functions: pageNavigatedTo
and loadWebSite
which, respectively, perform a fetch
request to load our posts and fire up a NativeScript WebView to show the post content's HTML output in an in-app web browser.var frameModule = require('ui/frame');
var pageModule = require('ui/page');
var webViewModule = require('ui/web-view');
var Observable = require('data/observable').Observable;
var ObservableArray = require('data/observable-array').ObservableArray;
var page;
var items = new ObservableArray([]);
var pageData = new Observable();
exports.pageNavigatedTo = function(args) {
page = args.object;
page.bindingContext = pageData;
var id = page.navigationContext.id;
fetch('https://demo.wp-api.org/wp-json/wp/v2/posts?categories=' + id)
.then(response => {
return response.json();
})
.then(function(r) {
pageData.set('items', r);
});
};
exports.loadWebSite = function(args) {
var link = args.view.link;
var factoryFunc = function() {
var webView = new webViewModule.WebView();
webView.src = link;
var page = new pageModule.Page();
page.content = webView;
return page;
};
var topmost = frameModule.topmost();
topmost.navigate(factoryFunc);
};
While I'm glossing over some of the details to save space, a reminder that all of this code is available here on Github.
app.css
, the /images/bg.png
background image, and font files from /fonts
from Github and add those to your app.Any troubles deploying an app? Consult the NativeScript Sidekick docs!