iOS Question Use of native objects, send referer to Webview

Akwa

Member
Licensed User
Hello,

For my app, I need to send a "REFERER" header with the request in a WebView.

It appears I need to use the native functions (loadRequest ? NSURLRequest ? URLRequest ? NSURL ? Url ?), but, unfortunately, i'm totally unable to understand this brainfuck objective C functions, swift, structs, classes and objects, and all this mess.
And then I absolutely don't succeed to pass my referer.

Example of what I tried :
B4X:
    Dim noWebView As NativeObject = WebView
    Dim noNSURL As NativeObject
    noNSURL.Initialize("NSURL")
    noNSURL.RunMethod("initWithString:", Array As Object(Url))
    Dim noNSURLRequest As NativeObject
    noNSURLRequest.Initialize("NSURLRequest")
    noNSURLRequest.RunMethod("initWithURL:", Array(noNSURL))
    noNSURLRequest.RunMethod("addValue", Array As Object("REFERER", "xxxxxxxxxxxx"))

    noWebView.RunMethod("loadRequest", Array As Object(noNSURLRequest))

For comparison, example of code I used on B4A !
B4X:
    'Crée ls Header de référence
   Dim extraHeaders As Map
   extraHeaders.Initialize
   extraHeaders.Put("REFERER", "xxxxxxxxxxxx")
  
   ' Récupère l'objet Java
   Dim WebViewNative As JavaObject
   WebViewNative = WebView
  
   ' Charge la page
   WebViewNative.RunMethodJO("loadUrl", Array(Url, extraHeaders))

Thank you for you help...
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
    Dim request As NativeObject = CreateMutableUrlRequest("https://www.google.com")
   request.RunMethod("addValue:forHTTPHeaderField:", Array("test", "REFERRER"))
   Dim wv As NativeObject = WebView1
   wv.RunMethod("loadRequest:", Array(request))

Sub CreateMutableUrlRequest (url As String) As Object
   Dim u As NativeObject
   u = u.Initialize("NSURL").RunMethod("URLWithString:", Array(url))
   Dim request As NativeObject
   request = request.Initialize("NSMutableURLRequest").RunMethod("requestWithURL:", Array(u))
   Return request
End Sub
 
Upvote 0

Akwa

Member
Licensed User
Thank you very much Erel ! :)

I haven't seen the "NSMutableURLRequest" class. I better understand now how you initialize and use the native objects.

I really think that this "Objective C" is one of the worst language ever created... Obfuscated, unreadable, illogic, ugly. :eek:
 
Upvote 0
Top