Xamarin Forms: Ios Project not launching - ios

wsHi I am developing an app using xamarin forms. My ios project was working fine but few days back it has stopped launching it.
It is saying -
Error: error MT1007: Failed to launch the application '/Users/user/Library/Caches/Xamarin/mtbs/builds/project.iOS/58c9dca0bc9f1c5ed37d1e8652672fb2/bin/iPhone/Debug/project.iOS.app' on the device 'My iPad': Object reference not set to an instance of an object. You can still launch the application manually by tapping on it.
Can anyone tell me why is this coming???
I have tried running every old backup of mine. but same issue is coming.
This is my AppDelegate.cs
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
//
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
ImageCircleRenderer.Init();
MR.Gestures.iOS.Settings.LicenseKey = "XXXXXXXXXXXX";
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
}

For me, this error turned out to be tied to the Free Provisioning Profile and my physical device no longer trusting it. It stopped trusting it immediately after I deleted the application from my phone. I began seeing the error after trying to debug the app again, which reinstalled the app on my phone but failed to launch.
To tell your device to trust the profile again, navigate to Settings -> General -> Device Management. Under the Developer App section, select your provisioning profile, then trust it again.
Within that screen after trusting the app you'll see:
Apps from developer "iPhone Developer: myProfileName" are trusted on this iPhone and will be trusted until all apps from the developer are deleted.
So, if you ever delete your apps from the physical device, you may run into this again.
I created a blog post with a bit more detail on how I came to this conclusion.

Related

Xamarin iPad app never gets past default blue xamarin logo screen

(Microsoft Windows 10 VS2022)
I have a basic Xamarin App that I have been working with and deploying on my Android Phone. (works great on Android.)
I created a Apple Developer Account and added those details into Visual Studios 2022.
I plugged in an iPad and started debugging.
The app launches and Hot Reload at the bottom says Launch succeeded.
But the screen only shows the Blue default Xamarin page that initially loads as default. It never goes past that - I don't get any errors. And all my breakpoints set at all spots on the App.xaml.cs page and AppShell.xaml.cs page never get triggered.
Is it a navigation issue with my AppShell.xaml.cs?
Or what could I be missing? Where do I look to see where it is hanging?
public App()
{
MainPage = new AppShell();
}
public partial class AppShell : Xamarin.Forms.Shell
{
public AppShell()
{
InitializeComponent();
Routing.RegisterRoute(nameof(MainPage), typeof(MainPage));
}
}
It appears I may have found the solution.
I updated the iPad (only one revision back; it's at 15.4 now)… I don't think that was the issue.
I think I had not "Built" the app prior to debugging. This resulted in a bad version being stored on the iPad. (You must delete the app with each change.)
To help - If you have a Windows machine developing on Apple iOS:
When developing on a Windows (win) 10 machine. You have to do a full build First!
Build - Build Solution.
Then debug/deploy on apple device.
Every change made - you must delete the app off the Apple device, Build, and re-deploy. This is the only way I can get it to work.

Xamarin Forms error - 'You MUST call Xamarin.Forms.Init(); prior to using it' - TestFlight (iOS) only

I've seen several other posts on SO and elsewhere about this error (System.InvalidOperationException: You MUST call Xamarin.Forms.Init(); prior to using it. at Xamarin.Forms.Device.get_PlatformServices), but none of them shed any light on my particular situation.
I've created a Xamarin.Forms application that works great on Android (emulators and device, including when distributed through Google Play), and works fine both on the iPhone simulator and when provisioned to the iPhone. And when I say 'works fine', I mean 'is able to get to the MainPage without error.'
However, when I use Ad-Hoc deployment and upload it to Apple so it can be distributed with TestFlight, the TestFlight-downloaded version shows the splash screen as expected, and then crashes without ever showing the MainPage. In looking at the device logs, the above error appears, and in doing some Internet research, it's become apparent that my AppDelegate needs to have global::Xamarin.Forms.Forms.Init(); as the first line in the FinishedLaunching() method.
So I went to take a look and put that line in that method in that file, and lo and behold, it's already there. This is not unexpected, given the fact that the app works on the sim, and on the iPhone when deployed directly from Visual Studio.
So my question is 'Why does Ad-Hoc deployment fail when all other deployment types do not, and what can I do to overcome this problem and get the app to actually distribute through TestFlight in a runnable fashion?'
I was able to resolve the issue by putting the command (global::Xamarin.Forms.Forms.Init();) first thing in every constructor in every class in the iOS platform-specific services. It's possible that only one was really necessary, but it doesn't seem to have caused any issue to over-deploy that particular command, and in the case that more than one of the services is called before the AppDelegate's FinishedLaunching() method is called, that would seem to be necessary in the first one called, which could potentially vary by program flow, depending on environment and other variables that may be checked.
In .iOS project make sure the line
"global::Xamarin.Forms.Forms.Init();"
is the first line in the event
"public override bool FinishedLaunching(UIApplication app, NSDictionary options)"
in
"AppDelegate.cs" file.
In .UWP VB project make sure the line
"Xamarin.Forms.Forms.Init(e)"
is after the line
"AddHandler rootFrame.NavigationFailed, AddressOf OnNavigationFailed" in
"Protected Overrides Sub OnLaunched(e As Windows.ApplicationModel.Activation.LaunchActivatedEventArgs)" event
in "App.Xaml.vb" file.
In .Android project
"global::Xamarin.Forms.Forms.Init(this, savedInstanceState);"
is after the line
"base.OnCreate(savedInstanceState);"
in "protected override void OnCreate(Bundle savedInstanceState)" event.
Hope this helps.

Xamarin.UITests - testing on real device - iOS - app permissions popups issue

I've iOS app that needs some privileges (GPS, Push notifications).
When app starts for a first time iOS asks user if they're ok with granting those permissions to application.
I've written some UITests and want to automate running them on locally connected iPhone.
The problem is that I cannot override permissions questions and my tests fails.
I found out that application deployed by IDE (Xamarin Studio) will ask for permissions, but application deployed via UITests will not.
So I tried with .AppBundle(path_to_app) but it says this is only valid for deploying to Simulator.
SetUp : System.Exception : This app bundle is not valid for running on
a simulator. To fix this issue please ensure that your target device
is a simulator. DTPlatformName is 'iphoneos', not 'iphonesimulator' in
the apps Info.plist.
Like it's trying to deploy iPhone app to Simulator. But Target in Xamarin Studio is set to real device.
I tried to add .DeviceIdentifier. When Used with .InstalledApp it was starting up (still asking for permissions).
But when I used DeviceIdentifier and AppBundle there was the same error as above.
My tests works fine on Test Cloud. They work fine on Simulator.
They work fine when I deploy to device manually, start app and approve permissions then run UI tests.
What I cannot achieve is to make UITests override permissions questions on real device.
Anyone made this work?
Last thing is that I found is in documentation for AppBundle method
"Will force a run on simulator"
https://developer.xamarin.com/api/member/Xamarin.UITest.Configuration.iOSAppConfigurator.AppBundle/p/System.String/
So I may be doomed with the task but maybe someone knows a workaround?
You can dismiss system dialogs with UITest by using InvokeUIA. The test below works by tapping the "OK" button of an iOS system alert:
[Test]
public void AppLaunches ()
{
app.Screenshot ("First screen.");
app.InvokeUia ("uia.query('[:view {:marked \"OK\"}]')");
app.InvokeUia ("uia.tapMark(\"OK\")");
}
A working sample app & UITest is also here:
https://github.com/King-of-Spades/InvokeUia-for-System-Dialogs
Warning about system dialogs in Test Cloud
The reason that you don't see this issue in Test Cloud is because Test Cloud automatically dismisses the system alerts; so usually you don't have to worry about it. However, if your alert launches too soon; so that it appears before the automation has fully started your app, then it will be unable to detect & dismiss the alert and cause your test to fail.
So you want to make sure that when running your app in Test Cloud that the request for permissions are delayed, or you can even deactivate them if they aren't explicitly needed for a particular test. More information is available in this Calabash guide: https://github.com/calabash/calabash-ios/wiki/Managing-Privacy-Alerts%3A--Location-Services%2C-APNS%2C-Contacts
(Even though it's Calabash, you can use the same strategy in UITest; albeit with a C# syntax.)
Update for Xcode 8 / iOS 10
Xcode 8 / iOS 10 removed UIAutomation, so the InvokeUIA workaround will only continue to be possible if you're using Xcode 7 and iOS 7-9. References:
UITest: https://developer.xamarin.com/guides/testcloud/UITest/xcode7/
Calabash: https://developer.xamarin.com/guides/testcloud/calabash/xcode7/#Automation_API
For real devices you dont need any of those.
{
app = ConfigureApp
.iOSAppBundle
.StartApp();
}
this piece of code is good enough, if you are connecting real device to the system, then select that before running.
We use this to execute the UI tests on iPhones:
ConfigureApp.iOS.InstalledApp("com.appcenter.UITestDemo").StartApp();
InstalledApp requires you to build an IPA using the Debug
configuration & a valid provisioning profile, and preinstalling it on
the target device.
https://github.com/microsoft/appcenter-Xamarin.UITest-Demo/blob/main/UITestDemo.UITest/AppInitializer.cs
To confirm system dialogs we use this:
private Query ConfirmLocalNetworkPermissionDialogButton
=> AppInitializer.Platform == Platform.iOS
? new Query(x => x.ClassFull("_UIAlertControllerActionView").Marked("OK"))
: x => x.Class("AppCompatButton").Marked("button1");

HealthKit requestAuthorization dialog not appearing

I have been consistently unable to get the HealthKit requestAuthorization dialog to appear. This has been the case for two different phones with different OS versions and different installation methods (see below). The requestAuthorization code is standard boilerplate code, such as:
HKCharacteristicType *genderType = [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex];
[self.healthStore requestAuthorizationToShareTypes:nil readTypes:[NSSet setWithObjects: genderType, nil] completion:^(BOOL success, NSError *error) {
NSLog(#"requestAuthorization completion block");
}];
I've tried a fresh app with just that code, tried different parameters for the requestAuthorization method call, and have tried resetting our systems in a number of different ways: app uninstall, resetting settings, erasing phone content and setting with existing partial backup; erasing phone content and settings with a full backup, erasing content and settings and starting phone as a new phone. Have tried multiple different readTypes and shareTypes. Each time requestAuthorization is called on a fresh app install, the following error code appears:
"Error occurred = Error Domain=com.apple.healthkit Code=4 "Missing
com.apple.developer.healthkit entitlement." UserInfo=0x7fa748534b00
{NSLocalizedDescription=Missing com.apple.developer.healthkit
entitlement.}"
However, the HealthKit entitlements are set in both Capabilities on xcode as well as Capabilities in the App ID on Apple Developer: Certificates, Identifiers, and Profiles.
- Questions: How do I fix this issue so the requestAuthorization dialog appears? What is a possible cause of phones getting into a state where they get this error consistently?
- Phones used: iPhone 6, iPhone 6 Plus
- OS Versions used: iOS 9.0 / 9.0.2 / 9.1 / 9.2
- Methods of loading app onto phone: iOS App Store, Watch App App Store, TestFlight, xcode
- Situations where this appears to not be broken: With our users' (non-development) phones installing via the App Store.
A few things could be causing this. First, your provisioning profiles could be out of date with what you are actually seeing on the web portal. Try refreshing these manually from XCode.
Does your app have an app extension or watchOS app that uses the HealthKit framework? Each of extension/watch app will have their own profiles on the web portal, and if the HealthKit framework has been added to any of them without having the entitlement added as well then this could also cause the error.

IBM Worklight 6.1 - Direct Update fails when returning to the foreground

Worklight 6.1.0
iOS 7 on iPhone 5s
We have a hybrid application that relies on Worklight server to do the direct update. The problem occurs when the user minimizes and then re-opens the app, the user is able to see the update dialog, however the update would then fail due to the wrong WL-instance id.
Here is the init options:
var wlInitOptions = {
connectOnStartup: true,
onConnectionFailure: function() {
WL.Logger.error("Failed to connect: " + arguments);
}
WLJSX.bind(window, 'load', function() {
WL.Client.init(wlInitOptions);
}
Here are the steps I did:
1) Open the IOS application, it will bring me to the first index page of the application, say Page1
2) Press a button, which brings me to another page, say page2 using GluJs
glu.viewport('MyApp.page2');
3) Minimize the application.
4) Modify the WebResource and then deploy the modified Iphone app to Worklight server.
5) Re-open the minimized application, the user gets a prompt to direct update.
6) The update would then fail, and there is a log in Worklight console of the following:
[ERROR ] FWLSE0203E: Received bad instance Id from client. Server
instance Id:'3f9eveddc7br5mq3ll0nq89miu', client instance
Id:'ut5m5f01i3bkq5l78m54uq137o'. [project trunk]
The existence of GluJs nor step 1 at all is relevant to the process of Direct Update, which happens in 2 cases: on application startup, on return to foreground. Any "page movements" in the app are irrelevant.
You did not mention if this happens in your development environment (Worklight Studio in Eclipse with the built-in Worklight Development Server), or a production environment (clustered, not clustered, load balancer or not, etc...).
I've performed the following without any errors in Worklight Studio 6.1.0:
Created a new project and application
Added the iPhone environment
In initOptions.js, changed connectOnStartup from false to true
Run As > Xcode project
Tested the app in both iOS 7.0 Simulator and iOS 7.1 beta 4 device (iPhone 5s)
Launched the application
Moved the application to the background (clicking the Home button)
In Eclipse, altered the HTML file
Run As > Run on Worklight Development Server
Brought the app back to the foreground
Direct Update detected and processed, application re-inited and displayed the updated HTML file

Resources