Showing posts with label Xamarin. Show all posts
Showing posts with label Xamarin. Show all posts

Wednesday, March 22, 2017

Error The "UnpackLibraryResources" task failed unexpectedly

While working with Xamarin Forms project I face a common error basically for Xamarin iOS project when mac disconnected and error like below:

Severity Code Description Project File Line Suppression State
Error The "UnpackLibraryResources" task failed unexpectedly.

and the solution for it just reconnect the mac pc with visual studio going through Tools -> iOS -> Xamarin Mac Agent. and click on Connect button.

if it doesn't work try to restart the visual studio and it will automatically be connected with Xamarin Mac agent.

Hope it will help someone. 

Sunday, March 5, 2017

Invalid Code Signing Entitlements

While working with Xamarin.iOS project I had faced a deployment problem to iTunes Connect using Application Loader and got the error like below:

Invalid Code Signing Entitlements. Your application bundle's singature contains code signing entilements that are not supported on iOS. Specifially, value 'Development' for key 'com.apple.developer.icloud-container-environment' in 'Payload/YourProject.app

and to overcome such error open file Entitlements.plist from Xamarin.iOS project using Json Editor and add these two lines below:

<key>com.apple.developer.icloud-container-environment</key>
<string>Production</string>

so the complete code example could be:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
 <key>com.apple.developer.icloud-container-environment</key>
 <string>Production</string>
</dict>
</plist>

Wednesday, December 28, 2016

Dll failed to uninstall need restart Visual Studio to finish the process

I had suffered a lot with this error like Xamarin.Forms2.x failed to uninstall. Restart Visual Studio to finish the process. In this case I have found a solution that helped me a lot:

Instead of deleting all of ~/packages, see if there are any *.deleteme files in ~/packages and delete them. Then restart Visual Studio.

This is a simple solution but potential one.


Tuesday, December 27, 2016

How to integrate Amazon S3 service with Xamarin.Forms project?

I have worked on  a Xamarin.Forms project to make an app both for android and ios devices. I have maintained files using amazon s3 file storage service and I have done this job making dependency service which is really a life maker for a cross platform like Xamarin.Forms to bring native flavor. However, I will show step by step how I have integrated  amazon s3 service.

First of all, I need to make an interface like below:

using System.IO;
using System.Threading.Tasks;

namespace Services

{
    public interface IAmazonS3Service
    {
        Task<string> UploadFile(string filepath, string filename);

        string GetAmazonS3Url(string keyName);

        void UploadStream(MemoryStream stream, string keyName);

    }


}


then I will keep this IAmazonS3Service interface into our portable project like Services (Portable).

Now I will create a class named AmazonS3Service will inherit IAmazonS3Service  and put this into our iOS project.

using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.IO;
using System.Threading.Tasks;
using iOS;
using Services;

[assembly: Xamarin.Forms.Dependency(typeof(AmazonS3Service))]

namespace iOS
{
    public class AmazonS3Service: IAmazonS3Service
    {
        const string bucketName = "bucketName";
        const string accessKey = "accessKey";
        const string secretKey = "secretKey";
        public async Task<string> UploadFile(string filepath, string filename)
        {
            try
            {
                AmazonS3Client client = SetAmazonCredential();
                PutObjectRequest request = new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key = filename,
                    FilePath = filepath
                };
                // Put object
                PutObjectResponse response = await client.PutObjectAsync(request);
                return "File uploaded to S3 Bucket";
            }
            catch (AmazonS3Exception s3Exception)
            {
                return "Upload failed. " + s3Exception.Message;
            }
        }

        public string GetAmazonS3Url(string keyName)
        {
            var url = string.Empty;
            try
            {
                AmazonS3Client client = SetAmazonCredential();
                var expiryUrlRequest = new GetPreSignedUrlRequest()
                {
                    BucketName = bucketName,
                    Key = keyName,
                    Expires = DateTime.Now.AddDays(7)
                };
                url = client.GetPreSignedURL(expiryUrlRequest);
            }
            catch (AmazonS3Exception s3Exception)
            {
                //
            }

            return url;
        }

        public async void UploadStream(MemoryStream stream, string keyName)
        {
            try
            {
                AmazonS3Client client = SetAmazonCredential();
                PutObjectRequest request = new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key = keyName,
                    InputStream = stream
                };
                // Put object
                PutObjectResponse response = await client.PutObjectAsync(request);
            }
            catch (AmazonS3Exception s3Exception)
            {
            }
        }

        private static AmazonS3Client SetAmazonCredential()
        {
            var credentials = new BasicAWSCredentials(accessKey, secretKey);
            var client = new AmazonS3Client(credentials, RegionEndpoint.APSouth1);
            return client;
        }

    }


}



I will create another class with the same name AmazonS3Service inheriting  IAmazonS3Service and will put this one into our Droid project.

using System;
using Services;
using System.Threading.Tasks;
using Amazon.S3.Model;
using Amazon.S3;
using Amazon.Runtime;
using Amazon;
using Droid;
using System.IO;

[assembly: Xamarin.Forms.Dependency(typeof(AmazonS3Service))]
namespace Droid
{


    public class AmazonS3Service: IAmazonS3Service
    {  
        
const string bucketName = "bucketName";
        const string accessKey = "accessKey";
        const string secretKey = "secretKey";
        public async Task<string> UploadFile(string filepath, string filename)
        {
            try
            {
                AmazonS3Client client = SetAmazonCredential();
                PutObjectRequest request = new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key = filename,
                    FilePath = filepath
                };
                // Put object
                PutObjectResponse response = await client.PutObjectAsync(request);
                return "File uploaded to S3 Bucket";
            }
            catch (AmazonS3Exception s3Exception)
            {
                return "Upload failed. " + s3Exception.Message;
            }
        }
       
        public string GetAmazonS3Url(string keyName)
        {
            var url = string.Empty;
            try
            {
                AmazonS3Client client = SetAmazonCredential();
                var expiryUrlRequest = new GetPreSignedUrlRequest() {
                    BucketName = bucketName,
                    Key = keyName,
                    Expires = DateTime.Now.AddDays(7)
                };
                url = client.GetPreSignedURL(expiryUrlRequest);
            }
            catch (AmazonS3Exception s3Exception)
            {
                //
            }

            return url;
        }

        public async void UploadStream(MemoryStream stream, string keyName)
        {
            try
            {
                AmazonS3Client client = SetAmazonCredential();
                PutObjectRequest request = new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key = keyName,
                    InputStream = stream
                };
                // Put object
                PutObjectResponse response = await client.PutObjectAsync(request);
            }
            catch (AmazonS3Exception s3Exception)
            {
            }
        }
       
        private static AmazonS3Client SetAmazonCredential()
        {
            var credentials = new BasicAWSCredentials(accessKey, secretKey);
            var client = new AmazonS3Client(credentials, RegionEndpoint.APSouth1);
            return client;
        }
    }

}




Now I will call those dependency services using this code below:

DependencyService.Get<IAmazonS3Service>().DownloadStream(keyName);

you can also call remaining methods like above code snippet.