Xamarin iOS: How to open a .pdf file using the default reader?












1














I'm using Xamarin iOS. I'd like to have our application open a .pdf file in the default reader. For Android I managed to get it done by following a two-fold process: (1) copy the file from the Assets folder to the Android device's Downloads folder, and (2) starting an activity to view the file.



I cannot find any similar resources for the iOS. We don't want to use the control in the Xamarin website, as it involves downloading lots of JavaScript (?) code, making the size of the app bigger, and also doesn't support pinching the screen to zoom. We want to use the OS's default pdf reader.



Thank you.










share|improve this question



























    1














    I'm using Xamarin iOS. I'd like to have our application open a .pdf file in the default reader. For Android I managed to get it done by following a two-fold process: (1) copy the file from the Assets folder to the Android device's Downloads folder, and (2) starting an activity to view the file.



    I cannot find any similar resources for the iOS. We don't want to use the control in the Xamarin website, as it involves downloading lots of JavaScript (?) code, making the size of the app bigger, and also doesn't support pinching the screen to zoom. We want to use the OS's default pdf reader.



    Thank you.










    share|improve this question

























      1












      1








      1


      1





      I'm using Xamarin iOS. I'd like to have our application open a .pdf file in the default reader. For Android I managed to get it done by following a two-fold process: (1) copy the file from the Assets folder to the Android device's Downloads folder, and (2) starting an activity to view the file.



      I cannot find any similar resources for the iOS. We don't want to use the control in the Xamarin website, as it involves downloading lots of JavaScript (?) code, making the size of the app bigger, and also doesn't support pinching the screen to zoom. We want to use the OS's default pdf reader.



      Thank you.










      share|improve this question













      I'm using Xamarin iOS. I'd like to have our application open a .pdf file in the default reader. For Android I managed to get it done by following a two-fold process: (1) copy the file from the Assets folder to the Android device's Downloads folder, and (2) starting an activity to view the file.



      I cannot find any similar resources for the iOS. We don't want to use the control in the Xamarin website, as it involves downloading lots of JavaScript (?) code, making the size of the app bigger, and also doesn't support pinching the screen to zoom. We want to use the OS's default pdf reader.



      Thank you.







      c# ios pdf xamarin assets






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 13 '17 at 23:33









      MrProgrammer

      639




      639
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Method 1:



          Set the Build Action to BundleResource. You can set the build action for a file by right-clicking on that file and and choosing Build Action in the menu that opens.

          Create a UIWebView and add it to a view:



          webView = new UIWebView (View.Bounds);
          View.AddSubview(webView);


          Load the file using NSUrl and NSUrlRequest classes:



          string fileName = "Loading a Web Page.pdf"; // remember case-sensitive
          string localDocUrl = Path.Combine (NSBundle.MainBundle.BundlePath, fileName);
          webView.LoadRequest(new NSUrlRequest(new NSUrl(localDocUrl, false)));
          webView.ScalesPageToFit = true;


          Method 2:



          public void OpenFile (NSUrl fileUrl)
          {
          var docControl = UIDocumentInteractionController.FromUrl (fileUrl);

          var window = UIApplication.SharedApplication.KeyWindow;
          var subViews = window.Subviews;
          var lastView = subViews.Last ();
          var frame = lastView.Frame;

          docControl.PresentOpenInMenu (frame, lastView, true);
          }





          share|improve this answer























          • Thanks for the help. It's not compiling, and is offering three solutions: (1a) Fully qualify 'Webclient with Data.Abstractions.WebClient, (1b) Fully qualify 'Webclient with Plugin.WebClient, (2) Generate type 'WebClient,' and (3) Add reference to 'System.dll..' Which one should I use?
            – MrProgrammer
            Sep 14 '17 at 0:23










          • The previous code only works on shared project, try it now @MrProgrammer
            – Angel Arias González
            Sep 14 '17 at 0:59










          • @MrProgrammer is there some way to scroll webview programmatically ?
            – pavel
            May 29 at 12:04













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46208297%2fxamarin-ios-how-to-open-a-pdf-file-using-the-default-reader%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Method 1:



          Set the Build Action to BundleResource. You can set the build action for a file by right-clicking on that file and and choosing Build Action in the menu that opens.

          Create a UIWebView and add it to a view:



          webView = new UIWebView (View.Bounds);
          View.AddSubview(webView);


          Load the file using NSUrl and NSUrlRequest classes:



          string fileName = "Loading a Web Page.pdf"; // remember case-sensitive
          string localDocUrl = Path.Combine (NSBundle.MainBundle.BundlePath, fileName);
          webView.LoadRequest(new NSUrlRequest(new NSUrl(localDocUrl, false)));
          webView.ScalesPageToFit = true;


          Method 2:



          public void OpenFile (NSUrl fileUrl)
          {
          var docControl = UIDocumentInteractionController.FromUrl (fileUrl);

          var window = UIApplication.SharedApplication.KeyWindow;
          var subViews = window.Subviews;
          var lastView = subViews.Last ();
          var frame = lastView.Frame;

          docControl.PresentOpenInMenu (frame, lastView, true);
          }





          share|improve this answer























          • Thanks for the help. It's not compiling, and is offering three solutions: (1a) Fully qualify 'Webclient with Data.Abstractions.WebClient, (1b) Fully qualify 'Webclient with Plugin.WebClient, (2) Generate type 'WebClient,' and (3) Add reference to 'System.dll..' Which one should I use?
            – MrProgrammer
            Sep 14 '17 at 0:23










          • The previous code only works on shared project, try it now @MrProgrammer
            – Angel Arias González
            Sep 14 '17 at 0:59










          • @MrProgrammer is there some way to scroll webview programmatically ?
            – pavel
            May 29 at 12:04


















          1














          Method 1:



          Set the Build Action to BundleResource. You can set the build action for a file by right-clicking on that file and and choosing Build Action in the menu that opens.

          Create a UIWebView and add it to a view:



          webView = new UIWebView (View.Bounds);
          View.AddSubview(webView);


          Load the file using NSUrl and NSUrlRequest classes:



          string fileName = "Loading a Web Page.pdf"; // remember case-sensitive
          string localDocUrl = Path.Combine (NSBundle.MainBundle.BundlePath, fileName);
          webView.LoadRequest(new NSUrlRequest(new NSUrl(localDocUrl, false)));
          webView.ScalesPageToFit = true;


          Method 2:



          public void OpenFile (NSUrl fileUrl)
          {
          var docControl = UIDocumentInteractionController.FromUrl (fileUrl);

          var window = UIApplication.SharedApplication.KeyWindow;
          var subViews = window.Subviews;
          var lastView = subViews.Last ();
          var frame = lastView.Frame;

          docControl.PresentOpenInMenu (frame, lastView, true);
          }





          share|improve this answer























          • Thanks for the help. It's not compiling, and is offering three solutions: (1a) Fully qualify 'Webclient with Data.Abstractions.WebClient, (1b) Fully qualify 'Webclient with Plugin.WebClient, (2) Generate type 'WebClient,' and (3) Add reference to 'System.dll..' Which one should I use?
            – MrProgrammer
            Sep 14 '17 at 0:23










          • The previous code only works on shared project, try it now @MrProgrammer
            – Angel Arias González
            Sep 14 '17 at 0:59










          • @MrProgrammer is there some way to scroll webview programmatically ?
            – pavel
            May 29 at 12:04
















          1












          1








          1






          Method 1:



          Set the Build Action to BundleResource. You can set the build action for a file by right-clicking on that file and and choosing Build Action in the menu that opens.

          Create a UIWebView and add it to a view:



          webView = new UIWebView (View.Bounds);
          View.AddSubview(webView);


          Load the file using NSUrl and NSUrlRequest classes:



          string fileName = "Loading a Web Page.pdf"; // remember case-sensitive
          string localDocUrl = Path.Combine (NSBundle.MainBundle.BundlePath, fileName);
          webView.LoadRequest(new NSUrlRequest(new NSUrl(localDocUrl, false)));
          webView.ScalesPageToFit = true;


          Method 2:



          public void OpenFile (NSUrl fileUrl)
          {
          var docControl = UIDocumentInteractionController.FromUrl (fileUrl);

          var window = UIApplication.SharedApplication.KeyWindow;
          var subViews = window.Subviews;
          var lastView = subViews.Last ();
          var frame = lastView.Frame;

          docControl.PresentOpenInMenu (frame, lastView, true);
          }





          share|improve this answer














          Method 1:



          Set the Build Action to BundleResource. You can set the build action for a file by right-clicking on that file and and choosing Build Action in the menu that opens.

          Create a UIWebView and add it to a view:



          webView = new UIWebView (View.Bounds);
          View.AddSubview(webView);


          Load the file using NSUrl and NSUrlRequest classes:



          string fileName = "Loading a Web Page.pdf"; // remember case-sensitive
          string localDocUrl = Path.Combine (NSBundle.MainBundle.BundlePath, fileName);
          webView.LoadRequest(new NSUrlRequest(new NSUrl(localDocUrl, false)));
          webView.ScalesPageToFit = true;


          Method 2:



          public void OpenFile (NSUrl fileUrl)
          {
          var docControl = UIDocumentInteractionController.FromUrl (fileUrl);

          var window = UIApplication.SharedApplication.KeyWindow;
          var subViews = window.Subviews;
          var lastView = subViews.Last ();
          var frame = lastView.Frame;

          docControl.PresentOpenInMenu (frame, lastView, true);
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 14 '17 at 1:11

























          answered Sep 13 '17 at 23:57









          Angel Arias González

          15416




          15416












          • Thanks for the help. It's not compiling, and is offering three solutions: (1a) Fully qualify 'Webclient with Data.Abstractions.WebClient, (1b) Fully qualify 'Webclient with Plugin.WebClient, (2) Generate type 'WebClient,' and (3) Add reference to 'System.dll..' Which one should I use?
            – MrProgrammer
            Sep 14 '17 at 0:23










          • The previous code only works on shared project, try it now @MrProgrammer
            – Angel Arias González
            Sep 14 '17 at 0:59










          • @MrProgrammer is there some way to scroll webview programmatically ?
            – pavel
            May 29 at 12:04




















          • Thanks for the help. It's not compiling, and is offering three solutions: (1a) Fully qualify 'Webclient with Data.Abstractions.WebClient, (1b) Fully qualify 'Webclient with Plugin.WebClient, (2) Generate type 'WebClient,' and (3) Add reference to 'System.dll..' Which one should I use?
            – MrProgrammer
            Sep 14 '17 at 0:23










          • The previous code only works on shared project, try it now @MrProgrammer
            – Angel Arias González
            Sep 14 '17 at 0:59










          • @MrProgrammer is there some way to scroll webview programmatically ?
            – pavel
            May 29 at 12:04


















          Thanks for the help. It's not compiling, and is offering three solutions: (1a) Fully qualify 'Webclient with Data.Abstractions.WebClient, (1b) Fully qualify 'Webclient with Plugin.WebClient, (2) Generate type 'WebClient,' and (3) Add reference to 'System.dll..' Which one should I use?
          – MrProgrammer
          Sep 14 '17 at 0:23




          Thanks for the help. It's not compiling, and is offering three solutions: (1a) Fully qualify 'Webclient with Data.Abstractions.WebClient, (1b) Fully qualify 'Webclient with Plugin.WebClient, (2) Generate type 'WebClient,' and (3) Add reference to 'System.dll..' Which one should I use?
          – MrProgrammer
          Sep 14 '17 at 0:23












          The previous code only works on shared project, try it now @MrProgrammer
          – Angel Arias González
          Sep 14 '17 at 0:59




          The previous code only works on shared project, try it now @MrProgrammer
          – Angel Arias González
          Sep 14 '17 at 0:59












          @MrProgrammer is there some way to scroll webview programmatically ?
          – pavel
          May 29 at 12:04






          @MrProgrammer is there some way to scroll webview programmatically ?
          – pavel
          May 29 at 12:04




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46208297%2fxamarin-ios-how-to-open-a-pdf-file-using-the-default-reader%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'