Page 1 of 1

[iOS]Open link

PostPosted: Sun Oct 02, 2016 11:59 am
by Lena
Hi.
I want click on label and open link. I found pascal code:
Code: Select all
uses
  FMX.Helpers.iOS, iOSapi.Foundation

function OpenURL(const AUrl: string): Boolean;
var
  Url: NSURL;
begin
  Url := TNSUrl.Wrap(TNSUrl.OCClass.URLWithString(NSStr(AUrl)));
  Result := SharedApplication.openUrl(Url);
end;


How in C++?

Code: Select all
include <FMX.Helpers.iOS.hpp>
#include <iOSapi.Foundation.hpp>
void __fastcall TForm1::Text1Click(TObject *Sender)
{
  NSURL Url;//<- error
  //***               

  /* for android no problem
   _di_JIntent Intent;
  Intent = TJIntent::JavaClass->init(TJIntent::JavaClass->ACTION_VIEW);
  Intent->setDataAndType(StrToJURI(L"http://bcbj.org"), StringToJString(L"text/html"));
  SharedActivity()->startActivity(Intent);
  */



[bcciosarm Error] UnitBigburger.cpp(29): variable type 'Iosapi::Foundation::NSURL' is an abstract class

Re: [iOS]Open link

PostPosted: Mon Oct 03, 2016 12:41 pm
by rlebeau
Lena wrote:I want click on label and open link. I found pascal code:
...
How in C++?
...
[bcciosarm Error] UnitBigburger.cpp(29): variable type 'Iosapi::Foundation::NSURL' is an abstract class



iOS and Android native classes are exposed to Delphi/C++Builder code using abstract interfaces, which are accessed by pointer. Your code is failing to compile because you are trying to declare an actual instance of the NSUrl class itself, not a pointer to it.

You need to use Embarcadero's wrapper classes, like _di_NSURL (and you really need to learn how to use a search engine to find examples).

Try something more like this:

Code: Select all
#if defined(__ANDROID__)

#include <Androidapi.JNI.JavaTypes.hpp>
#include <Androidapi.JNI.GraphicsContentViewText.hpp>
#include <Androidapi.JNI.Net.hpp>
#include <Androidapi.Helpers.hpp>
#include <FMX.Helpers.Android.hpp>

#elif defined(__APPLE__)

#include <iOSapi.Foundation.hpp>
#include <Macapi.Helpers.hpp>
#include <FMX.Helpers.iOS.hpp>

#elif defined(MSWINDOWS)

#include <shellapi.h>

endif

bool __fastcall OpenURL(const String &AURL)
{
    #if defined(__ANDROID__)

    _di_JIntent Intent = TJIntent::JavaClass->init(TJIntent::JavaClass->ACTION_VIEW, TJnet_Uri::JavaClass->parse(StringToJString(AURL)));
    try
    {
        SharedActivity()->startActivity(Intent);
        return true;
    }
    catch (const Exception &)
    {
        return false;
    }

    #elif defined(__APPLE__)

    _di_NSUrl NSU = StrToNSUrl(AURL);
    if (SharedApplication()->canOpenURL(NSU))
        return SharedApplication()->openUrl(NSU);
    else
        return false;

    #elif defined(MSWINDOWS)

    SHELLEXECUTEINFOW sei = {0};
    sei.cbSize = sizeof(sei);
    sei.lpFile = AURL.c_str();
    sei.nShow = SH_SHOWNORMAL;

    return ShellExecuteEx(&sei);

    #else

    return false;

    #endif
}

void __fastcall TForm1::Text1Click(TObject *Sender)
{
    OpenURL(L"http://bcbj.org");


Re: [iOS]Open link

PostPosted: Tue Oct 04, 2016 2:47 am
by Lena
Thank you very much!!!