0

Use Xamarin Forms and on iOS, how to check and open Word app when Open file by UrL? Reference: https://learn.microsoft.com/en-us/office/client-developer/integration/integrate-with-office-from-ios-applications

This is my code, it isn't work:

Device.OpenUri(new Uri("ms-word:ofe|u|https://calibre-ebook.com/downloads/demos/demo.docx"));

Please help me! Thanks!

Huu Bao Nguyen
  • 1,051
  • 2
  • 14
  • 40
  • You need a dependency service for this its not a xamarin forms thing! – FreakyAli Nov 22 '18 at 12:05
  • I want write code on iOS. You can supply solution @G.hakim – Huu Bao Nguyen Nov 22 '18 at 12:07
  • I guess [this](https://wopi.readthedocs.io/projects/officewopi/en/latest/overview.html) guide here has necessary information. – FreakyAli Nov 22 '18 at 12:43
  • I only want that view file .doc on external app ("Word" app https://itunes.apple.com/us/app/microsoft-word/id586447913?mt=8). May be your solution don't fit with me. – Huu Bao Nguyen Nov 22 '18 at 15:25
  • I find a answer is same with your answer https://stackoverflow.com/questions/51494932/open-a-word-doc-from-my-ios-application-and-edit-it-in-msword-app-and-get-the-ed But you don't want "edit" file, Do you only want "view" file .docx with Word app?when open it to your app. –  Nov 22 '18 at 16:04
  • Yes, @BillNguyen, Do you have solution for it? – Huu Bao Nguyen Nov 22 '18 at 16:05

2 Answers2

0

What you are asking here can be done using a simple WebView in iOS:

First, create a custom WebView class that allows you to pick file uri:

public class CustomWebView : WebView
{
    public static readonly BindableProperty UriProperty = BindableProperty.Create(propertyName: "Uri",
            returnType: typeof(string),
            declaringType: typeof(CustomWebView),
            defaultValue: default(string));

    public string Uri
    {
        get { return (string)GetValue(UriProperty); }
        set { SetValue(UriProperty, value); }
    }

}

Then in ios make a renderer for the same and do something like this:

  [assembly: ExportRenderer (typeof(CustomWebView), typeof(CustomWebViewRenderer))]
  namespace DisplayPDF.iOS
 {
   public class CustomWebViewRenderer : ViewRenderer<CustomWebView, UIWebView>
  {
    protected override void OnElementChanged (ElementChangedEventArgs<CustomWebView> e)
    {
        base.OnElementChanged (e);

        if (Control == null) {
            SetNativeControl (new UIWebView ());
        }
        if (e.OldElement != null) {
            // Cleanup
        }
        if (e.NewElement != null) {
            var customWebView = Element as CustomWebView;
            string fileName = Path.Combine (NSBundle.MainBundle.BundlePath, string.Format ("Content/{0}", WebUtility.UrlEncode (customWebView.Uri)));
            Control.LoadRequest (new NSUrlRequest (new NSUrl (fileName, false)));
            Control.ScalesPageToFit = true;
          }
       }
   }
}

Then use the custom control like this:

 <local:CustomWebView Uri="FooPath" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" /> 

Where FooPath is the path for the doc file.

FreakyAli
  • 13,349
  • 3
  • 23
  • 63
0

I have solution for my project, I try it:

If you only open file .docx on iOS, you could write code at Share Code of iOS:

  1. Check device of user has setup app (Word, Excel, PP, etc...)

    public static bool HasSetupAppDocument(string extension)
    {
        if (string.IsNullOrEmpty(extension))
            return false;
    
        // Device has setup app?
        var result = UIApplication.SharedApplication.CanOpenUrl(NSUrl.FromString($"{extension}"));
        return result;
    }
    

(Ex: extension is ms-word: or ms-excel: or ms-excel:

Reference: https://learn.microsoft.com/en-us/office/client-developer/integration/integrate-with-office-from-ios-applications#office-protocols)

Notes: Add source to Info.plist:

<key>LSApplicationQueriesSchemes</key>
<array>
     <string>ms-word</string>
     <string>ms-excel</string>
     <string>ms-powerpoint</string>
</array>
  1. At Class Dependencies, open file with URL:

    Device.OpenUri(new Uri($"{convertExtension}{url}"));

Note: url is link file share on Onedrive and Be sure that account Onedrive as same as account login Word app (if you had set security).

If file has mode Read-only, app Word will open file with mode Read-only.

Huu Bao Nguyen
  • 1,051
  • 2
  • 14
  • 40