iOS Question Deadly combination with LSSupportsOpeningDocumentsInPlace...

OlavRossland

Member
Licensed User
Longtime User
Thank's to @Erel (in this post), I can now open all txt-files with combination of UIFileSharingEnabled and CFBundleDocumentTypes.
B4X:
    #PlistExtra: <key>UIFileSharingEnabled</key><true/>
    #PlistExtra: <key>CFBundleDocumentTypes</key>
    #PlistExtra: <array>
    #PlistExtra:    <dict>
    #PlistExtra:        <key>CFBundleTypeName</key>
    #PlistExtra:            <string>Text File</string>
    #PlistExtra:        <key>LSHandlerRank</key>
    #PlistExtra:            <string>Alternate</string>
    #PlistExtra:        <key>LSItemContentTypes</key>
    #PlistExtra:            <array>
    #PlistExtra:                <string>public.text</string>
    #PlistExtra:            </array>
    #PlistExtra:    </dict>
    #PlistExtra: </array>

But...
in the next step, I can't combine LSSupportsOpeningDocumentsInPlace with UIFileSharingEnabled and CFBundleDocumentTypes.
B4X:
    #PlistExtra: <key>LSSupportsOpeningDocumentsInPlace</key><true/>
    #PlistExtra: <key>UIFileSharingEnabled</key><true/>    
    #PlistExtra: <key>CFBundleDocumentTypes</key>
    #PlistExtra: <array>
    #PlistExtra:    <dict>
    #PlistExtra:        <key>CFBundleTypeName</key>
    #PlistExtra:            <string>Text File</string>
    #PlistExtra:        <key>LSHandlerRank</key>
    #PlistExtra:            <string>Alternate</string>
    #PlistExtra:        <key>LSItemContentTypes</key>
    #PlistExtra:            <array>
    #PlistExtra:                <string>public.text</string>
    #PlistExtra:            </array>
    #PlistExtra:    </dict>
    #PlistExtra: </array>

The error comes when I try to open a text-file from the iOS Files-app on my iPhone.
My app then crash, and I get this error:

B4X:
*** Assertion failure in -[UIApplication _applicationOpenURLAction:payload:origin:], UIApplication.m:7956
Error occurred on line: 241 (Main)
Application has LSSupportsOpeningDocumentsInPlace key, but doesn't implement application:openURL:options: on delegate <B4IAppDelegate: 0x28377ce60>
Stack Trace: (
  CoreFoundation       18E3CD08-C5BD-329B-AADA-4684E60F8805 + 39488
  libobjc.A.dylib      objc_exception_throw + 56
  Foundation           50C2EC83-ADF0-3911-A0E0-FC6E4E0C0E60 + 4976464
  UIKitCore            55D36A14-98DE-3A42-810E-220DE725A53B + 4597276
  UIKitCore            55D36A14-98DE-3A42-810E-220DE725A53B + 13048848
  libdispatch.dylib    694EC4A0-7CF4-3E50-947D-79A142B4D0DD + 407632
  libdispatch.dylib    694EC4A0-7CF4-3E50-947D-79A142B4D0DD + 411592
  libdispatch.dylib    694EC4A0-7CF4-3E50-947D-79A142B4D0DD + 286488
  libdispatch.dylib    694EC4A0-7CF4-3E50-947D-79A142B4D0DD + 285584
  CoreFoundation       18E3CD08-C5BD-329B-AADA-4684E60F8805 + 604200
 CoreFoundation       18E3CD08-C5BD-329B-AADA-4684E60F8805 + 488616
 CoreFoundation       CFRunLoopRunSpecific + 584
 GraphicsServices     GSEventRunModal + 160
 UIKitCore            55D36A14-98DE-3A42-810E-220DE725A53B + 3626536
 UIKitCore            UIApplicationMain + 312
 SplitNote            main + 104
 dyld                 F328B8FD-5C49-3ECE-9A63-A51B44ACDE0C + 81392
)
SignalHandler 6

I have now read the "entire" internet..., but I'm not able to solve this problem.
Any idea?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Add this to the main module:
B4X:
#if OBJC
@end
@interface B4IAppDelegate (openurl)
@end
@implementation B4IAppDelegate (openurl)
- (BOOL)application:(UIApplication *)app 
            openURL:(NSURL *)url 
            options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
        
        return [self application:app openURL:url sourceApplication:nil annotation:options];
}
#End If
 
Upvote 0

OlavRossland

Member
Licensed User
Longtime User
Thank You @Erel !
I tested the code in post #2 - So far, so good. I can now use this without the app is crash - at least:
#PlistExtra: <key>LSSupportsOpeningDocumentsInPlace</key><true/><key>UIFileSharingEnabled</key><true/>

If I'm loading (Application_OpenUrl...) a file from the app File.DirDocuments-area, my app open and read the txt-file without any error.

But:...
If I'm trying to open a file outside of the app-area, for example on iCloud, i still get error.

...The Catch still give me error.
By placing the TextView1.Text=File.ReadString("",f) (just to se what's wrong...) I read this error from the Logs:

B4X:
Private Sub Application_OpenUrl (Url As String, Data As Object) As Boolean
    If Url.StartsWith("file://") Then
        Dim su As StringUtils
        Dim f As String = su.DecodeUrl(Url.SubString(7),"utf8") 'remove the file:// scheme.
        TextView1.Text=File.ReadString("",f) 'Just for testing - se some error...
        Try
            TextView1.Text=File.ReadString("",f)
        Catch
            Msgbox("Error loading file", "")
        End Try
    End If
    Return True
End Sub

The Error:...
Error reading file: Error Domain=NSCocoaErrorDomain Code=257 "Filen «# TIPS.txt» kunne ikke åpnes fordi du ikke har tillatelse til å vise den." UserInfo={NSFilePath=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/# pro-Note/# TIPS.txt, NSUnderlyingError=0x28091bc60 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

But if I open the same file (outside of the app-area), from the app itself with this code:

B4X:
        DocumentPicker.InitializeImport("picker", Array("public.text"))
        DocumentPicker.Show(Page1,Page1.TopRightButtons)
        Wait For Picker_Complete (Success As Boolean, URLs As List)
        If Success Then
            TextView1.Text=(File.ReadString(URLs.Get(0), ""))
        End If

There is no "Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted""

I'm still reading tips'n trick's, but I think I need some help to solve this - please anyone...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Tricky case.

Updated code which seems to work:
B4X:
#if OBJC
@end
@interface B4IAppDelegate (openurl)
@end
@implementation B4IAppDelegate (openurl)
- (BOOL)application:(UIApplication *)app 
            openURL:(NSURL *)url 
            options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
        [url startAccessingSecurityScopedResource];
        [self application:app openURL:url sourceApplication:nil annotation:options];
        [url stopAccessingSecurityScopedResource];
        return true;
}
#End If
based on: https://stackoverflow.com/questions/57596871/no-permission-to-read-icloud-drive-file
 
Upvote 0

OlavRossland

Member
Licensed User
Longtime User
Tricky case.

Updated code which seems to work:
B4X:
#if OBJC
@end
@interface B4IAppDelegate (openurl)
@end
@implementation B4IAppDelegate (openurl)
- (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
        [url startAccessingSecurityScopedResource];
        [self application:app openURL:url sourceApplication:nil annotation:options];
        [url stopAccessingSecurityScopedResource];
        return true;
}
#End If
based on: https://stackoverflow.com/questions/57596871/no-permission-to-read-icloud-drive-file

Thank You @Erel for taking Your time to this.
Everything was OK now when testing with iCloud, but I still have error when opening txt-files from OneDrive.
I made a complete new app for this testing (se attached file).

I think I have to make some more tests - I'll be back...
 

Attachments

  • Test.zip
    2.7 KB · Views: 142
Upvote 0
Top