Cache WebView in Xamarin.Forms for iOS - c#

I am creating an app in Xamarin.Forms(C#) that is cross platform. I have a website that is also a pwa(progressive web app). I am trying to access it through a WebView and also allow for offline viewing of the last data cached. I have it working perfectly on Android by enabling:
webView.Settings.SetAppCacheEnabled(true);
However I cannot seem to get it to work correctly on iOS.
I am very new to app development and am trying to learn. Any help in learning would be appreciated.
Simple mock/poc example:
ToolbarItem refresh;
WebView webView;
public TestingWebCache() {
InitializeComponent();
Title = "Website";
IconImageSource = "airport.png";
webView = new WebView();
refresh = new ToolbarItem {
IconImageSource = "refresh.png"
};
refresh.Clicked += Refresh;
ToolbarItems.Add(refresh);
init();
Content = webView;
}
private void init() {
webView.Source = "https://www.google.com" //not the website im using but used for testing
}
private async void Refresh(object sender, EventArgs e) {
TRCWebViewItem.Reload();
}
I have looked at many resources, including the iOS WKWebView documentation. I have a feeling that I am just missing a vital piece of information that will stitch everything together.
I cannot download the html and display that way because the actual website it’s not just basic HTML and CSS

This issue was fixed by having the web developer also set the Application Cache in addition to the Cache Storage. As of iOS 10 WKWebview default is set to cache the webview but AppCache must be set in the website level. Android can handle if just the Cache Storage is set but iOS cannot.

Related

The test succeeds in the editor, but it does not appear on the mobile

I'm currently developing a game for Android on Unity 2020 as solo.
I have done most of the game development already, all I have to do is enable ads.
I worked by referring to the official Google Ads documentation and other literature, but it didn't work.
Specifically, what I did was register a new app with Admob and issue a new ad ID. In addition, I am applying to publish the app on the Google Play console.
The code is exactly as it was introduced in the documentation;
void Start()
{
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize(initStatus => { });
RequestBanner();
}
private void RequestBanner()
{
string adUnitId = "(correct Unit ID)";
// Create a 320x50 banner at the top of the screen.
BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the banner with the request.
bannerView.LoadAd(request);
// Create a 320x50 banner at the top of the screen.
//bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);
}
The test was displayed successfully in the editor, like below;
Then I built the game and tried it on my mobile phone.
Then the advertisement will not be displayed.
Is it because the app hasn't been published to Google Play yet?
Thank you.
For some reason, the ad just started to appear.
Probably because it wasn't long enough since I created the project.

How can I read form fields from a UIWebView site using Xamarin.ios?

I currenly use a webview to load a site into a native xamarin.ios app.
public override void ViewDidLoad()
{
base.ViewDidLoad();
addressTxt.Text = "192.0.0.0";
var request = new NSMutableUrlRequest(new
NSUrl("http://test/"));
request.HttpMethod = "GET";
webview.LoadRequest(request);
}
This loads the site correctly into the webview. But i need to grab a hidden field value from the page for use within the app. Is there any i can do this?
I've seen a few examples, but nothing for xamarin.ios.
Thanks
You need to evaluate JavaScript returning needed field to get value from page in UIWebView.
string result = _webViewInstance.EvaluateJavascript(yourJavaScriptCode);
also you can see this answer WKWebView evaluate JavaScript return value

The request could not be completed (Not Found)

I have added a vanilla mobile service, selecting the defaults all the way. It is a .Net backend, using a new 20MB DB on an existing server I use in the North Europe DC. I created a blank Windows Store App, added the MobileServices nuget package and then followed the instructions to connect my MobileService to my app by adding the MobileServicesClient to my App.xaml class. Then I added a button with an event handler. The handler calls an async function to insert a TodoItem object into the database.
App.xaml.cs
public static MobileServiceClient MobileService = new MobileServiceClient(
"https://???.azure-mobile.net/",
"???");
I have replaced the URI and Token with ???. The one I used came from my Azure Portal for this Mobile Service.
MainPage.xaml.cs
private void Button_Click(object sender, RoutedEventArgs e)
{
CreateItem();
}
private async void CreateItem()
{
TodoItem item = new TodoItem { Text = "Sort This", Complete = false };
try
{
await App.MobileService.GetTable<TodoItem>().InsertAsync(item);
}
catch(MobileServiceInvalidOperationException ex1)
{
Debug.WriteLine(ex1.Message);
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
When I run this code and click the button a MobileServiceInvalidOperationException is raised:
"{"The request could not be completed. (Not Found)"}"
I am using VS2013, with Service Pack 2 installed and WindowsAzure.MobileServices 1.2.3.
The MobileService is running as I can navigate to https://MyMobileService.azure-mobile.net/ but, if I click Check It Out, I am asked to Authenticate (MyMobileService is not the actual Uri).
The post request looks ok, but the response is a 404.
I have been working on this all day, including using Windows Phone, with no luck...
I know I must be doing something wrong, or there is an issue with Azure in Northern Europe, but I can't sort it out.
Any help much appreciated.
Jason.
I had the same issue awhile ago. I switch from .net backend to javascript. Also, make sure your mobileservice and db schema match.

Facebook and Twitter missing in ShareMediaTask

I am developing a WP8.1 app, when I try using ShareMediaTask to share a picture it shows me a list of apps where I will share the picture. Facebook, Twitter and other apps like Whatsapp are not included in the list; it shows me only share to OneNote. Taking note that I debug from device with Facebook and Twitter installed on it.
If someone could help me thankfully.
This is my simple code :
ShareMediaTask shareMediaTask = new ShareMediaTask();
shareMediaTask.FilePath = fileName;
shareMediaTask.Show();
Do I have to add something ?!
You'll want to take a look at Sharing content for Windows Runtime apps. Sharing is different between Silverlight and Runtime apps.
Here is a basic sample on how to share files:
When you are ready to share:
DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager,
DataRequestedEventArgs>(this.ShareStorageItemsHandler);
And then populating the data:
private async void ShareStorageItemsHandler(DataTransferManager sender,
DataRequestedEventArgs e)
{
DataRequest request = e.Request;
request.Data.Properties.Title = "Share StorageItems Example";
request.Data.Properties.Description = "Demonstrates how to share files.";
// Because we are making async calls in the DataRequested event handler,
// we need to get the deferral first.
DataRequestDeferral deferral = request.GetDeferral();
// Make sure we always call Complete on the deferral.
try
{
StorageFile logoFile =
await Package.Current.InstalledLocation.GetFileAsync("Assets\\Logo.png");
List<IStorageItem> storageItems = new List<IStorageItem>();
storageItems.Add(logoFile);
request.Data.SetStorageItems(storageItems);
}
finally
{
deferral.Complete();
}
}
Only apps that implement sharing will show up in the list. Currently Twitter and Facebook have not released an app that implements sharing.
For more information of receiving shared content, see this doc.

Xamarin iOS C# Open Links in Safari from UIWebView

I'm coding an iPhone app with a UIWebView, in Xamarin using C#.
By default embedded links within the web view open a web page in that same web view. I would instead like them to launch the linked page in a new safari browser instance.
This has been answered for objective C in X-Code but as far as I can see not for Xamarin C#
webView.LoadHtmlString(html, new NSUrl(Const.ContentDirectory, true));
Thanks in advance
Adam
You can open a web page in the brower of the device (Safari) with this command.
UIApplication.SharedApplication.OpenUrl(new NSUrl("www.google.com"));
You don't have to use UIWebView at all. If you want to open some web pages in UIWebView and some with Safari you need to implement the ShouldStartLoad delegate. Here you can determine whether to open the web page in UIWebView or rather in Safari.
private bool HandleShouldStartLoad(UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType)
{
// you need to implement this method depending on your criteria
if (this.OpenInExternalBrowser(request))
{
// open in Safari
UIApplication.SharedApplication.OpenUrl(request.Url);
// return false so the UIWebView won't load the web page
return false;
}
// this.OpenInExternalBrowser(request) returned false -> let the UIWebView load the request
return true;
}
At last somewhere in ViewDidLoad (or other place where you initiliaze the WebView) add the following code.
webView.ShouldStartLoad = HandleShouldStartLoad;
If loading content into a UIWebView and you want to use Safari to open links, doing it the way described above will get you a blank page. You need to check the NavigationType.
private bool HandleShouldStartLoad(UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navType)
{
if (navType == UIWebViewNavigationType.LinkClicked)
{
UIApplication.SharedApplication.OpenUrl(request.Url);
return false;
}
return true;
}
The easiest/simplest way to do it is to assign a delegate to the UIWebView's ShouldStartLoad property:
webView.ShouldStartLoad = (w, urlRequest, navigationType) => {
// Open the url in Safari here.
// The urlRequest parameter is of type NSUrlRequest, you can get the URL from it.
return false; //return true for urls you actually want your web view to load.
};
Make sure the delegate returns false, for the links you want to load in Safari, so that your UIWebView does not load the link.

Categories

Resources