[iOS]Open link

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

[iOS]Open link

Postby Lena » Sun Oct 02, 2016 11:59 am

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
Lena
BCBJ Master
BCBJ Master
 
Posts: 450
Joined: Sun Feb 06, 2011 1:28 pm

Re: [iOS]Open link

Postby rlebeau » Mon Oct 03, 2016 12:41 pm

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");

Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1395
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: [iOS]Open link

Postby Lena » Tue Oct 04, 2016 2:47 am

Thank you very much!!!
Lena
BCBJ Master
BCBJ Master
 
Posts: 450
Joined: Sun Feb 06, 2011 1:28 pm


Return to Technical

Who is online

Users browsing this forum: No registered users and 5 guests

cron