Forum Xamarin.iOS

How to integrate two apps in xamarin iOS?

marcos_mendesmarcos_mendes Member
edited March 28 in Xamarin.iOS

Hi.

I have two apps. One called SincApp that take a pic and should return it for caller with a status. So, a image and a string.
This app was be developed in swift.
Other called ZipZag that call SincApp and wait for your response that should show the image taked and status string in a label, developed in Xamarin.

I was configured Info.plist like this:

Info.plist

    <key>LSApplicationQueriesSchemes</key>
        <array>
            <string>sincapp-scan</string>
        </array>
        <key>CFBundleURLTypes</key>
        <array>
            <dict>
                <key>CFBundleURLName</key>
                <string>br.com.xxx.SincApp</string>
                <key>CFBundleURLSchemes</key>
                <array>
                    <string>sincapp-scan-back</string>
                </array>
            </dict>
        </array>

        <key>UIApplicationSceneManifest</key>
        <dict>
            <key>UIApplicationSupportsMultipleScenes</key>
            <false/>
        </dict>
        <key>UIRequiresFullScreen</key>
        <true/>
        <key>CFBundleDocumentTypes</key>
        <array>
            <dict>
                <key>CFBundleTypeName</key>
                <string>SincApp</string>
                <key>LSItemContentTypes</key>
                <array>
                    <string>br.com.xxx.sincapp</string>
                </array>
            </dict>
        </array>
    </dict> 

Entitlement.plist like this:

    <key>com.apple.security.application-groups</key>
    <array>
        <string>group.com.xxx.sincapp</string>
    </array>

ScamCameraViewControler.cs

    [Register("UniversalView")]
    public class UniversalView : UIView
    {
        public UniversalView()
        {
            Initialize();
        }

        public UniversalView(RectangleF bounds) : base(bounds)
        {
            Initialize();
        }

        void Initialize()
        {
            BackgroundColor = UIColor.Red;
        }
    }

    [Register("ScamCameraViewController")]
    public class ScamCameraViewController : UIViewController
    {

    public ScamCameraViewController()
    {
    }

    public override void DidReceiveMemoryWarning()
    {

        base.DidReceiveMemoryWarning();


    }

    public override void ViewDidLoad()
    {
        View = new UniversalView();

        base.ViewDidLoad();

        this.UIObserver();

        this.CallSincApp();


    }


    public async Task UIObserver()
    {
        NSNotificationCenter
            .DefaultCenter
            .AddObserver(
                this, 
                new ObjCRuntime.Selector("handleEventNotification:"),
                new NSString("EventNotification"), 
                null);


    }

    [Export("handleEventNotification:")]
    public void handleEventNotification(NSNotification obj)
    {

        // var readValue = NSUserDefaults.StandardUserDefaults.StringForKey("CFBundleURLName");
        NSUserDefaults userDefaults = new NSUserDefaults("group.br.com.xxx.sincapp", NSUserDefaultsType.SuiteName);



        var isSincAppProcessSuccess = userDefaults.BoolForKey("KEY_STATUS"); 

        var isSincAppProcessSuccess2 = userDefaults.StringForKey(new NSString ("KEY_STATUS"));


    }

    public async Task CallSincApp()
    {
        var application = UIApplication.SharedApplication;

        var SDK_KEY = "99999999999999999999999999999999";    

        var urlString = "sincapp-scan://?canoaKey=/" + SDK_KEY + "&scanbarcode=/";

        Foundation.NSUrl urlApp = Foundation.NSUrl.FromString(urlString);
        if (application.CanOpenUrl(urlApp))
        {
            application.OpenUrl(urlApp);
        }
    }

    }

AppDelegate.cs

    // The UIApplicationDelegate for the application. This class is responsible for launching the 
    // User Interface of the application, as well as listening (and optionally responding) to 
    // application events from iOS.
    [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();


        LoadApplication(new App(new iOSInitializer()));


        return base.FinishedLaunching(app, options);
    }



    public override bool HandleOpenURL(UIApplication application, NSUrl url)
    {
        BindValue(url);
        return true;
    }

    private void BindValue(NSUrl url)
    {
        if (url.Scheme == "sincapp-scan-back")
        {
            NSNotificationCenter
                .DefaultCenter
                .PostNotificationName("EventNotification", null, null);
        }

    }

    public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
    {
        // We need to handle URLs by passing them to their own OpenUrl in order to make the SSO authentication works.
        BindValue(url);
        return true;
    }
            }

    public class iOSInitializer : IPlatformInitializer
    {
        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
        // Register any platform specific implementations
        }
    }

    }

ScanCameraiOS

    private async Task callMainViewController()
    {
        ScamCameraViewController scvc = new ScamCameraViewController();
        scvc.ViewDidLoad();
    }

But when I degub it, var isSincAppProcessSuccess is returning always false and isSincAppProcessSuccess2 null.
The same app developed in Swift is working.

Someone could help me, please?

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Have you linked the entitlement.plist file in your project:

    Did you use NSUserDefaults to store them in the shared container before retrieving? Please make sure they are using the same group ID.

Sign In or Register to comment.