The Blog

On computers and human factors.


Web apps, design, architecture, trends, reviews, C#, .NET Framework, databases, security and best practices.

Fixing Cordova Camera Issue On iOS

22 Dec 2017   No Comments   756 Views

I'm currently developing a mobile app with Cordova 7.1/Ionic 1.3 and we're targetting both Android and iOS platforms. At the heart of our application, we need access to Camera features for recording audio/video and taking pictures. We use cordova-plugin-media-capture to access the device camera, and everything works perfectly fine on Android. However, the app crashes on iOS 10/11 when you try to use the camera. No error messages, no warning no nothing. The app just crashes and fails silently. So I've been banging my head against the wall trying to understand the problem.

After much investigation and going through StackOverflow threads, I learned to my surprise that the Xcode project generated by Cordova, lacks the required access permissions in the info.plist file. One step closer, now we know what's causing the app to crash. Turns out iOS 10 specifically, terminates your application if the required privacy permissions are not found to be valid. Solution? configure access permissions in the iOS build. But how?

If you google Cordova iOS 10 Camera Permissions you'll find loads of threads addressing this issue. Like this one on GithHub. You can override config settings for each platform by hand, or you could use <edit-config> which was introduced in Cordova 5. It isn't quite straightforward though, not optimal. Also, there have been a few people who suggested you can fix this problem by adding CDV-* key/value pairs in your config.xml, which of course didn't work. Let's stop this horrible search for a solution right here, and use a cordova plugin for the win.

 

iOS Camera Permissions Plugin

I tried everything I could find on the internet, only to fail. None of the methods worked and overriding permissions manually, is not at all an option. Why? Simply because you should be able to clone a repository, install dependencies and build the project without an error. Good news is someone already took care of this with a beautiful plugin.

Install the plugin, remove/add the iOS platform and voila! your build now includes the required permissions.

 

Usage

First install the plugin into your cordova project:

cordova plugin add cordova-plugin-ios-camera-permissions --save

Or if you're on Mac:

sudo cordova plugin add cordova-plugin-ios-camera-permissions --save

For the changes to plugin.xml to take effect, you must refresh the ios.json file (under the /plugins directory). So you'll need to remove and readd the platform:

$ cordova platform rm ios --save
$ cordova platform add ios --save

You're all set! If you open your package.json file, you'll find the plugin definition like this:

"cordova-plugin-ios-camera-permissions": {
    "CAMERA_USAGE_DESCRIPTION": "This app needs camera access",
    "MICROPHONE_USAGE_DESCRIPTION": "This app needs microphone access",
    "PHOTOLIBRARY_ADD_USAGE_DESCRIPTION": "This app needs write-access to photo library",
    "PHOTOLIBRARY_USAGE_DESCRIPTION": "This app needs read/write-access photo library access"
}

This is listed under the cordova/plugins section. Feel free to change the description values, they will be reflected in your config.xml file everytime you build the project. Happy coding :smile_cat:!


Identity Email Service with Mailgun

03 Nov 2017   No Comments   969 Views

If you've worked with ASP.NET Identity before you should be familiar with EmailService and SmsService classes. They're responsible for sending user notifications, like when a password is changed, or when a user requests to reset their password. There's no default implementation so you'll have to write your own services to actually use them. If you have access to a valid email account and its SMTP settings, you can configure <mailSettings> under Web.config and send your emails. You can see an example in the listing below.

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network" from="yourname@gmail.com">
            <network host="smtp.gmail.com" port="587" userName="yourname@gmail.com" password="your_password" enableSsl="true" />
        </smtp>
    </mailSettings>
</system.net>

Another option is to utilize an excellent platform such as Mailgun. which is pretty awesome and covers all your mailing needs. you can learn more from the official guide and docs. assuming you have signed up and have access to your API keys, you can write an email service like the listing below. nothing fancy here, we just create a new HttpClient and send a HTTP request to send our emails.

public class EmailService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        const string apiKey = "key-9927c9ba92d7871faa30a24398dc4db1";
        const string sandBox = "sandboxe3121b4943524cfc9fc7f071a47e90a5.mailgun.org";
        byte[] apiKeyAuth = Encoding.ASCII.GetBytes($"api:{apiKey}");
        
        var httpClient = new HttpClient { BaseAddress = new Uri("https://api.mailgun.net/v3/") };
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(apiKeyAuth));

        var form = new Dictionary<string, string>
        {
            ["from"] = "postmaster@sandboxe3121b4943524cfc9fc7f071a47e90a5.mailgun.org",
            ["to"] = message.Destination,
            ["subject"] = message.Subject,
            ["text"] = message.Body
        };

        HttpResponseMessage response = httpClient.PostAsync(sandBox + "/messages", new FormUrlEncodedContent(form)).Result;

        return Task.FromResult((int)response.StatusCode);
    }
}

Happy coding!


REST Countries

19 Nov 2016   No Comments   2600 Views

Some time ago I was googling to find useful and fun APIs. There are hundreds of them available nowadays, you can find popular APIs on Mashape, AnyApi, ProgrammableWeb and other sources, or just by googling. Building clients for public APIs is fun, and a great learning opportunity. I play with these APIs anytime I get a chance. I build simple apps to use the data, learn and compare different technologies and beyond.

I came across RESTCountries which gives you information about countries! Stuff like their native names, capital cities, location and region, bordering countries and a bunch of other useful information. You don't need to signup for an API Key or anything, all endpoints are publicly available. In this post I'm gonna create a little angular app to use this API. I'm using Angular 1.x and a few other libraries that you probably already know: Bootstrap, Font Awesome, nprogressui-router and jQuery.

This project is a good example for angular beginners. You'll see how to create an app, define routes and controllers, talk to a REST API and a few other things. But I'm assuming you have experience with HTML/CSS, JavaScript and Angular fundamentals. The source code is available on github and you can also play with a live fiddle at the end of this post.

Continue reading →

First Post

31 Oct 2016   No Comments   4182 Views

Well, hello! This is my first post, exciting. I've often blogged on different communities before and I have always wanted a personal website. But procrastination is a bitch, so I didn't find the time to launch a blog and keep it up. On the other hand I wanted a blog engine I have written myself. For various reasons, I wanted to build a web app myself, learn and tinker with the project, and have a live demo of my work. So I was mostly interested in writing a ASP.NET web app and build my own website. I was working full time at the time, so this project had to be scheduled in leisure time. Any chance I got, I spent time working on the website. It took a long time, nearly 2 years to get here. But many good features have been implemented along the way, I have upgraded or replaced entire parts of the project many times and it is still a work in progress. Of course if I had the time to work on this project full time, I would've reached this release much sooner. But I'm okay with that, I took my time and learned a lot of different things.  I'm quite pleased with the result so far, and of course many fixes and upgrades are schedule to follow soon. You will notice UI changes, new features or re-designs in the coming weeks but the building blocks are in place and most of the website will remain the same.

What Will I Be Writing About

This website is intended to be my online portfolio, so my blog is mostly a technical blog. I'll be blogging about .NET, Web Technologies, Databases, Design Patterns and Architectural Styles, Code Quality, Testing, Plugins and Tools, I could go all night, you get the gist. Of course I'll post different kinds of material as time goes by, add and improve features and designs, create new sections like Projects or Portfolio and so on. So tune in, much will change and new stuff are coming soon, I'll try my best to add valuable content each day, or at least several days a week.

You might have noticed that this website is localized, it is available in two languages: English and Persian. Public facing pages like About, Contact, Hire etc. show the same data and are localized only on the UI. But dynamic views like Blog related pages show localized data. English content is completely separate from Persian content. All posts, categories, tags, comments etc. are separated by culture. So if you speak both languages, make sure to visit both versions as the content differs on each culture.

Continue reading →