iOS Question Open external txt-files with my app

OlavRossland

Member
Licensed User
Longtime User
I have tested this code from @Erel in a smal project for CSV-files. Everything worked as it should.
But what about txt-files?....

B4X:
#PlistExtra:<key>CFBundleDocumentTypes</key>
#PlistExtra:<array>
#PlistExtra:    <dict>
#PlistExtra:        <key>CFBundleTypeName</key>
#PlistExtra:            <string>[B]CSV File[/B]</string>
#PlistExtra:        <key>LSHandlerRank</key>
#PlistExtra:            <string>Alternate</string>
#PlistExtra:        <key>LSItemContentTypes</key>
#PlistExtra:            <array>
#PlistExtra:                <string>[B]public.comma-separated-values-text[/B]</string>
#PlistExtra:            </array>
#PlistExtra:    </dict>
#PlistExtra:</array>

B4X:
Private Sub Application_OpenUrl (Url As String, Data As Object) As Boolean
   If Url.StartsWith("file://") Then
     Dim f As String = Url.SubString(7) 'remove the file:// scheme.
     Try
       Msgbox(File.ReadString("", f), "")
     Catch
       Msgbox("Error loading file", "")
     End Try
   End If
   Return True
End Sub


I have tried this code, but get a lot of errors...
B4X:
#PlistExtra:<key>CFBundleDocumentTypes</key>
#PlistExtra:<array>
#PlistExtra:    <dict>
#PlistExtra:        <key>CFBundleTypeName</key>
#PlistExtra:            <string>[B]Text File[/B]</string>
#PlistExtra:        <key>LSHandlerRank</key>
#PlistExtra:            <string>Alternate</string>
#PlistExtra:        <key>LSItemContentTypes</key>
#PlistExtra:            <array>
#PlistExtra:                <string>[B]public.txt[/B]</string>
#PlistExtra:            </array>
#PlistExtra:    </dict>
#PlistExtra:</array>
 

OlavRossland

Member
Licensed User
Longtime User
Which error?
Hello @Erel

With the Try Catch, I got the "Error loading file".
To find out what error I got, I copied the Msgbox(File.ReadString("", f), "") and pasted it right after the "Dim f as string":

Dim f As String = Url.SubString(7) 'remove the file:// scheme.
Msgbox(File.ReadString("", f), "")
Try
Msgbox(File.ReadString("", f), "")

Then I got this error (copy from the log...) :
Application_Start
Application_Active
Application_Inactive
Application_Background
Application_Foreground
openURL: file:///private/var/mobile/Containers/Data/Application/75E998B0-F7C6-43B7-86D4-62DBC8E4772F/tmp/b4i.test-Inbox/Test%202.txt, (null), (null)​
Application_Openurl​
file:///private/var/mobile/Containers/Data/Application/75E998B0-F7C6-43B7-86D4-62DBC8E4772F/tmp/b4i.test-Inbox/Test%202.txt​
/private/var/mobile/Containers/Data/Application/75E998B0-F7C6-43B7-86D4-62DBC8E4772F/tmp/b4i.test-Inbox/Test%202.txt​
Error occurred on line: 76 (Main)​
Error reading file: Error Domain=NSCocoaErrorDomain Code=260 "The file “Test%202.txt” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/private/var/mobile/Containers/Data/Application/75E998B0-F7C6-43B7-86D4-62DBC8E4772F/tmp/b4i.test-Inbox/Test%202.txt, NSUnderlyingError=0x283d66c10 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}​
Stack Trace: (​
CoreFoundation A68FCE15-6C1A-38C6-9ED3-7063272D237E + 39568​
libobjc.A.dylib objc_exception_throw + 56​
CoreFoundation A68FCE15-6C1A-38C6-9ED3-7063272D237E + 975724​
B4i Example -[B4IFile ReadString2:::] + 428​
B4i Example -[B4IFile ReadString::] + 108​
B4i Example -[b4i_main _application_openurl::] + 884​
CoreFoundation A68FCE15-6C1A-38C6-9ED3-7063272D237E + 458224​
CoreFoundation A68FCE15-6C1A-38C6-9ED3-7063272D237E + 128312​
B4i Example +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1400​
B4i Example -[B4IShell runMethod:] + 420​
B4i Example -[B4IShell raiseEventImpl:method:args::] + 1968​
B4i Example -[B4IShellBI raiseEvent:event:params:] + 1360​
B4i Example -[B4IAppDelegate application:eek:penURL:sourceApplication:annotation:] + 704​
UIKitCore AA0A89BD-E48F-31DB-AD5C-5C724CE77D12 + 4596772​
UIKitCore AA0A89BD-E48F-31DB-AD5C-5C724CE77D12 + 13025228​
UIKitCore AA0A89BD-E48F-31DB-AD5C-5C724CE77D12 + 13024916​
UIKitCore AA0A89BD-E48F-31DB-AD5C-5C724CE77D12 + 7930016​
libdispatch.dylib 19F4CD59-AB92-3ED4-9730-174FA4B1468E + 407632​
libdispatch.dylib 19F4CD59-AB92-3ED4-9730-174FA4B1468E + 411592​
libdispatch.dylib 19F4CD59-AB92-3ED4-9730-174FA4B1468E + 286488​
libdispatch.dylib 19F4CD59-AB92-3ED4-9730-174FA4B1468E + 285584​
CoreFoundation A68FCE15-6C1A-38C6-9ED3-7063272D237E + 604232​
CoreFoundation A68FCE15-6C1A-38C6-9ED3-7063272D237E + 488660​
CoreFoundation CFRunLoopRunSpecific + 584​
GraphicsServices GSEventRunModal + 160​
UIKitCore AA0A89BD-E48F-31DB-AD5C-5C724CE77D12 + 3625928​
UIKitCore UIApplicationMain + 312​
B4i Example main + 104​
dyld A2AA3A68-07F7-3D52-A49E-D29D136813F3 + 81392​
)​
Application_Active


"Solution" (sort of...):
I then removed the spaces in all the filenames i tested, and I got rid of the errors.

But...
Why can't I have spaces, or special characters like #, in the filenames in this little test?
 
Upvote 0

OlavRossland

Member
Licensed User
Longtime User
Don't use Msgbox.
B4X:
Dim su As StringUtils
f = su.DecodeUrl(f, "utf8")
log(f)
I know. I’ve also used log, as you said, but that’s not the problem.

As I told in post #3:


"Solution" (sort of...):
I then removed the spaces in all the filenames i tested, and I got rid of the errors.

But...
Why can't I have spaces, or special characters like #, in the filenames in this little test?

So I’m still stuck in this iOS problem…
 
Upvote 0
Top