Recent

Author Topic: CFBundleURLSchemes Custom URI Scheme [SOLVED]  (Read 587 times)

xenon54

  • New Member
  • *
  • Posts: 11
CFBundleURLSchemes Custom URI Scheme [SOLVED]
« on: May 20, 2020, 04:38:09 pm »
Hi all!

In my OX S application, i need to inbound external interraction.
This is open my app when double click for file with .myapp extensions and open it with custom uri scheme.

For the first case, i found extra simple solution - https://www.tweaking4all.com/forum/delphi-lazarus-free-pascal/lazarus-macos-x-how-to-open-with-for-files-that-open-with-your-application/

For the second case, i found nothing... Can anyone help me with this? I know, that i must register ".myapp" in plist.list by adding this.

Code: Pascal  [Select][+][-]
  1.         <key>CFBundleURLTypes</key>
  2.         <array>
  3.                 <dict>
  4.                         <key>CFBundleURLName</key>
  5.                         <string>MYAPP</string>
  6.                         <key>CFBundleURLSchemes</key>
  7.                         <array>
  8.                                 <string>myapp</string>
  9.                         </array>
  10.                 </dict>
  11.         </array>
  12.  

And application opens by this uri.
But how to parse uri inside application? Somthing like "TForm1.ApplicationProperties1DropFiles" event.
« Last Edit: May 22, 2020, 04:32:24 pm by xenon54 »

trev

  • Hero Member
  • *****
  • Posts: 668
  • Former Delphi 1-7 and 10.2 User
Re: CFBundleURLSchemes Custom URI Scheme
« Reply #1 on: May 21, 2020, 09:39:03 am »
For the first case, i found extra simple solution - https://www.tweaking4all.com/forum/delphi-lazarus-free-pascal/lazarus-macos-x-how-to-open-with-for-files-that-open-with-your-application/

Code: XML  [Select][+][-]
  1.         <key>CFBundleURLTypes</key>
  2.         <array>
  3.                 <dict>
  4.                         <key>CFBundleURLName</key>
  5.                         <string>MYAPP</string>
  6.                         <key>CFBundleURLSchemes</key>
  7.                         <array>
  8.                                 <string>myapp</string>
  9.                         </array>
  10.                 </dict>
  11.         </array>
  12.  

Apple "This key contains the abstract name for this URL type. This is the main way to refer to a particular type. To ensure uniqueness, it is recommended that you use a Java-package style identifier." The highlighted line should therefore be more like:

Code: XML  [Select][+][-]
  1. <string>com.yourdomain.myapp</string>


Additionally, you might also consider including:

Code: XML  [Select][+][-]
  1. <key>CFBundleTypeRole</key>
  2. <string>role</string>

where role is one of: Editor, Viewer, Shell, QLGenerator, None.

[EDIT]

As to the second part of your question, this looks apposite.

Code: Pascal  [Select][+][-]
  1. myDocument = objcclass(NSDocument)
  2.    public
  3.      function getName : NSString; message 'getName';
  4.      function getfileURL: NSURL; message 'fileURL';
  5.    end;
« Last Edit: May 21, 2020, 10:43:58 am by trev »
o Lazarus v2.1.0 r63233, FPC v3.3.1 r45525, macOS 10.14.6 (with sup update), Xcode 11.3.1
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.1 (Parallels VM)
o FPC 3.0.4, FreeBSD 12-STABLE r358002
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

xenon54

  • New Member
  • *
  • Posts: 11
Re: CFBundleURLSchemes Custom URI Scheme
« Reply #2 on: May 21, 2020, 12:40:02 pm »
Thank you very much for the answer. Can you explain me how to use it?

Code: Pascal  [Select][+][-]
  1. myDocument = objcclass(NSDocument)
  2.    public
  3.      function getName : NSString; message 'getName';
  4.      function getfileURL: NSURL; message 'fileURL';
  5.    end;
  6.  

I'm trying do this

Code: Pascal  [Select][+][-]
  1.  
  2. type
  3.  
  4.   myDocument = objcclass(NSDocument)
  5.      public
  6.        function getName : NSString; message 'getName';
  7.        function getfileURL: NSURL; message 'fileURL';
  8.      end;
  9.  
  10. .....
  11.  
  12. implementation
  13.  
  14. procedure TForm1.FormCreate(Sender: TObject);
  15. begin
  16.  
  17.   ShowMessage(myDocument.getName);
  18.   ShowMessage(myDocument.getfileURL);
  19.  
  20. end;  
  21.  

But getting errors:
Code: Pascal  [Select][+][-]
  1. unit1.pas(66,33) Error: Only class methods, class properties and class variables can be accessed in class methods

Looks like this is abstract class and this methods need implementation.

And how to convert NSString to string?
UPD: For this question i find answer:

Code: Pascal  [Select][+][-]
  1. uses CocoaUtils;
  2.  
  3. function NSStringToString(ns: NSString): String;
« Last Edit: May 21, 2020, 01:03:38 pm by xenon54 »

trev

  • Hero Member
  • *****
  • Posts: 668
  • Former Delphi 1-7 and 10.2 User
Re: CFBundleURLSchemes Custom URI Scheme
« Reply #3 on: May 21, 2020, 01:32:36 pm »
I discovered a simpler non-macOS API method. Easier to demo than explain :)

Demo project attached. Opens .xyz files.

[EDIT]

Info.Plist to replace default created by Lazarus.

Code: XML  [Select][+][-]
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3. <plist version="1.0">
  4. <dict>
  5.         <key>CFBundleDevelopmentRegion</key>
  6.         <string>English</string>
  7.         <key>CFBundleExecutable</key>
  8.         <string>SimpleEditor</string>
  9.         <key>CFBundleName</key>
  10.         <string>SimpleEditor</string>
  11.         <key>CFBundleIdentifier</key>
  12.         <string>com.company.SimpleEditor</string>
  13.         <key>CFBundleInfoDictionaryVersion</key>
  14.         <string>6.0</string>
  15.         <key>CFBundlePackageType</key>
  16.         <string>APPL</string>
  17.         <key>CFBundleSignature</key>
  18.         <string>Simp</string>
  19.         <key>CFBundleShortVersionString</key>
  20.         <string>0.1</string>
  21.         <key>CFBundleVersion</key>
  22.         <string>1</string>
  23.         <key>CSResourcesFileMapped</key>
  24.         <true/>
  25.         <key>CFBundleDocumentTypes</key>
  26.         <array>
  27.                 <dict>
  28.                         <key>CFBundleTypeRole</key>
  29.                         <string>Editor</string>
  30.                         <key>CFBundleTypeExtensions</key>
  31.                         <array>
  32.                                 <string>xyz</string>
  33.                         </array>
  34.                         <key>CFBundleTypeOSTypes</key>
  35.                         <array>
  36.                                 <string>fold</string>
  37.                                 <string>disk</string>
  38.                                 <string>****</string>
  39.                         </array>
  40.                 </dict>
  41.         </array>
  42.         <key>NSHighResolutionCapable</key>
  43.         <true/>
  44. </dict>
  45. </plist>
  46.  
« Last Edit: May 21, 2020, 02:38:33 pm by trev »
o Lazarus v2.1.0 r63233, FPC v3.3.1 r45525, macOS 10.14.6 (with sup update), Xcode 11.3.1
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.1 (Parallels VM)
o FPC 3.0.4, FreeBSD 12-STABLE r358002
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

xenon54

  • New Member
  • *
  • Posts: 11
Re: CFBundleURLSchemes Custom URI Scheme
« Reply #4 on: May 21, 2020, 01:49:37 pm »
I discovered a simpler non-macOS API method. Easier to demo than explain :)

Demo project attached. Opens .xyz files.

Your demo about open files with custom extension. This is i can ddo.
I need open application by custom URI and parse it when app opened.

trev

  • Hero Member
  • *****
  • Posts: 668
  • Former Delphi 1-7 and 10.2 User
Re: CFBundleURLSchemes Custom URI Scheme
« Reply #5 on: May 21, 2020, 02:02:13 pm »
Am I misunderstanding?

If you double-click on a  file (you'll need to re-assemble the Info.plist file as the publish project doesn't preserve it), then you can get the filename and path from the FormDropFiles() FileNames[0]. Is that not what you wanted?
o Lazarus v2.1.0 r63233, FPC v3.3.1 r45525, macOS 10.14.6 (with sup update), Xcode 11.3.1
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.1 (Parallels VM)
o FPC 3.0.4, FreeBSD 12-STABLE r358002
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

xenon54

  • New Member
  • *
  • Posts: 11
Re: CFBundleURLSchemes Custom URI Scheme
« Reply #6 on: May 21, 2020, 02:33:58 pm »
Am I misunderstanding?

If you double-click on a  file (you'll need to re-assemble the Info.plist file as the publish project doesn't preserve it), then you can get the filename and path from the FormDropFiles() FileNames[0]. Is that not what you wanted?

How to get filename from double click i know :)
There is link in my first message to easy way for it.

I need other.
If in browser write "myapp://12345", opens my application. In Application i need to know this string.


I have delphi file, that include what i need (TURLEventHandler), but i cant do this for lazarus.
« Last Edit: May 21, 2020, 02:43:57 pm by xenon54 »

trev

  • Hero Member
  • *****
  • Posts: 668
  • Former Delphi 1-7 and 10.2 User
Re: CFBundleURLSchemes Custom URI Scheme
« Reply #7 on: May 22, 2020, 11:00:59 am »
See https://kaihao.dev/posts/Make-your-own-custom-URI-scheme-resolver

Be warned though - it does not appear to work for me and given that it is a security issue (it has been used by malware in the last couple of years), it's possible it has been "deprecated" out of existence.
o Lazarus v2.1.0 r63233, FPC v3.3.1 r45525, macOS 10.14.6 (with sup update), Xcode 11.3.1
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.1 (Parallels VM)
o FPC 3.0.4, FreeBSD 12-STABLE r358002
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

xenon54

  • New Member
  • *
  • Posts: 11
Re: CFBundleURLSchemes Custom URI Scheme
« Reply #8 on: May 22, 2020, 02:03:50 pm »
See https://kaihao.dev/posts/Make-your-own-custom-URI-scheme-resolver

Be warned though - it does not appear to work for me and given that it is a security issue (it has been used by malware in the last couple of years), it's possible it has been "deprecated" out of existence.

I Think this is bad way...
Would be better natively in lazarus.

trev

  • Hero Member
  • *****
  • Posts: 668
  • Former Delphi 1-7 and 10.2 User
Re: CFBundleURLSchemes Custom URI Scheme
« Reply #9 on: May 22, 2020, 02:43:13 pm »
The first part is easy to resolve in Lazarus by extending the Info.plist file:

Code: XML  [Select][+][-]
  1.         <key>CFBundleURLTypes</key>
  2.         <array>
  3.                 <dict>
  4.                         <key>CFBundleTypeRole</key>
  5.                         <string>Viewer</string>
  6.                         <key>CFBundleURLName</key>
  7.                         <string>com.company.SimpleEditor.URLScheme</string>
  8.                         <key>CFBundleURLSchemes</key>
  9.                         <array>
  10.                                 <string>xyz</string>
  11.                         </array>
  12.                 </dict>
  13.         </array>
  14.  

So, Safari will then ask you if you want SimpleEditor to open if you give it "xyz://blah".

However, you now need to find a way to actually open the file "blah".

Apple documentation for this.
o Lazarus v2.1.0 r63233, FPC v3.3.1 r45525, macOS 10.14.6 (with sup update), Xcode 11.3.1
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.1 (Parallels VM)
o FPC 3.0.4, FreeBSD 12-STABLE r358002
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

trev

  • Hero Member
  • *****
  • Posts: 668
  • Former Delphi 1-7 and 10.2 User
Re: CFBundleURLSchemes Custom URI Scheme
« Reply #10 on: May 22, 2020, 02:50:15 pm »
See: this post.
o Lazarus v2.1.0 r63233, FPC v3.3.1 r45525, macOS 10.14.6 (with sup update), Xcode 11.3.1
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.1 (Parallels VM)
o FPC 3.0.4, FreeBSD 12-STABLE r358002
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

xenon54

  • New Member
  • *
  • Posts: 11
Re: CFBundleURLSchemes Custom URI Scheme
« Reply #11 on: May 22, 2020, 04:21:28 pm »
See: this post.

This is what I need, thanks a lot.

But i have same errors in linker as topic starter.

http://prntscr.com/slszja

Im trying to paste into Project options -> Compilation and Linking
http://prntscr.com/slt1ft

But getting errors
http://prntscr.com/slt1up

xenon54

  • New Member
  • *
  • Posts: 11
Re: CFBundleURLSchemes Custom URI Scheme [SOLVED]
« Reply #12 on: May 22, 2020, 04:31:55 pm »
Ok, i just commented in uses "AppleEvents" and all workds fine. Link that you give - is awesome. Dont know why my search not give me this result. Thanks again.

trev

  • Hero Member
  • *****
  • Posts: 668
  • Former Delphi 1-7 and 10.2 User
Re: CFBundleURLSchemes Custom URI Scheme [SOLVED]
« Reply #13 on: May 23, 2020, 03:50:56 am »
Glad we got there in the end :)
o Lazarus v2.1.0 r63233, FPC v3.3.1 r45525, macOS 10.14.6 (with sup update), Xcode 11.3.1
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.1 (Parallels VM)
o FPC 3.0.4, FreeBSD 12-STABLE r358002
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

 

TinyPortal © 2005-2018