ShowURL(with special characters) does not work

When I use ShowURL(“1010%20Cit%20Administrative%20de%20l’Etat%20Bruxelles-Capitale”) in an iOS app, the URL will not open and no error is shown.

When I use this declares, I get “Can not open URL”

			Declare Function NSClassFromString Lib "Foundation" (className As CFStringRef) As Ptr
			Declare Function sharedApplication Lib "Foundation" Selector "sharedApplication" _
			          (UIApplicationClass As Ptr) As Ptr
			Declare Function URLWithString Lib "UIKit" Selector "URLWithString:" _
			          (NSURLClass As Ptr, url As CFStringRef) As Ptr
			Declare Function openURL Lib "UIKit" Selector "openURL:" (UIApplication As Ptr, NSURL As Ptr) As Boolean
			
			Dim UIApplicationPtr As Ptr = sharedApplication(NSClassFromString("UIApplication"))
			Dim NSURLPtr As Ptr = URLWithString(NSClassFromString("NSURL"), "1010%20Cit%20Administrative%20de%20l'Etat%20Bruxelles-Capitale")
			
			If openURL(UIApplicationPtr, NSURLPtr) Then
					  // Success
			Else
					  // Error
					MsgBox "Can not open URL: " + URL
			End

That looks like a STREET ADDRESS, not a URL

http://postal-codes.findthedata.com/l/36677/1010-Cit-Administrative-De-L-etat

Sorry. Here is the complete URL:

http://www.google.com/#q=1010%20Cit%20Administrative%20de%20l’Etat%20Bruxelles-Capitale

you might need to encode a few more characters

  • = %C3
  • ’ = %27

I don’t know if “Xojo for iOS” has this function or not, but use the Desktop version to encode your URL if necessary

URL=EncodeURLComponent("http://www.google.com/#q=1010 Cit Administrative de l'Etat Bruxelles-Capitale")

Yes, I know. But “EncodeURLComponent” ist now available for iOS.

so use the DESKTOP version to determine what to use in iOS

or perhaps one of the others here can publish a declare that exposes

stringByAddingPercentEncodingWithAllowedCharacters

A basic encodeURLComponent is not that difficult to do : after all, a series of search replace suffices to encode the 4 vowels with grave, acute, circumflex and dieresis, which is total 32 with upper and lowercase, then lower and uppercase.

That should suffice for French, German (add ), Italian. Spanish (add and ).

There’s a declare for the iOS EncodeURLComponnent floating around.

Here you go:

Public Function EncodeURLComponent(value as text) as text

Declare Function CFURLCreateStringByAddingPercentEscapes lib "Foundation" (allocator as Ptr, origString as CFStringRef , charactersToLeaveUnescaped as CFStringRef , legalURLCharactersToBeEscaped as cfStringRef,encoding as uint32) as CFStringRef
  
return CFURLCreateStringByAddingPercentEscapes(nil, value, nil, nil, &h08000100)
  
End Function		

I didn’t write it but I don’t have a record of who did so I apologise for not crediting anyone.

You know what, it was already part of XojoiOSWrapper, and I had forgotten about it :wink: