martes, 18 de abril de 2017

Enviar una URL a otra App en Android/iOS usando C++

Algunos protocolos comunes que se asocian a esta acción:
  • http, tel, sms, fb, mailto, twitter, geo, etc.
Ejemplos soportados según cada plataforme:
Android:
  • content://contacts/people/
  • content://contacts/people/1
  • geo://0,0?q=5617 Scotts Valley Drive, Scotts Valley, CA 95066
  • geo://46.191200, -122.194400 
iOS:
  • http://maps.apple.com?q=5617 Scotts Valley Drive, Scotts Valley, CA 95066

Para ambos:
  • http://www.embarcadero.com/
  • tel://(01)734-3131
  • sms://1234
  • http://twitter.com/coderage (Abre el cliente twitter)
  • mailto:jim.mckeeth@embarcadero.com
  • twitter://user?screen_name=coderage
  • fb://profile/34960937498 (ID del usuario Facebook)
  • fb-messenger://share?link=http://www.embarcadero.com
  • whatsapp://send?text=Hola
El código es una base para que lo perfeccionen, en algunos casos se requiere URLEncode y en otros no, juega con ello en cada caso.

OpenURL.h:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//---------------------------------------------------------------------------

#ifndef OpenURLH
#define OpenURLH
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <System.SysUtils.hpp>
#include <IdURI.hpp>
#include <FMX.Dialogs.hpp>
#if defined(__ANDROID__)
 #include <Androidapi.Helpers.hpp>
 #include <FMX.Helpers.Android.hpp>
 #include <Androidapi.JNI.GraphicsContentViewText.hpp>
 #include <Androidapi.JNI.Net.hpp>
 #include <Androidapi.JNI.JavaTypes.hpp>
#endif
#if defined(__APPLE__)
 #include <Macapi.Helpers.hpp>
 #include <iOSapi.Foundation.hpp>
 #include <FMX.Helpers.iOS.hpp>
#endif

class TOpenURL : public TObject
{
 private:

 public:
 __fastcall TOpenURL();
 __fastcall ~TOpenURL();

 bool OpenURL(const String pURL, const bool pDisplayError=false);

};

#endif

OpenURL.cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//---------------------------------------------------------------------------

#pragma hdrstop

#include "OpenURL.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

//---------------------------------------------------------------------------
__fastcall TOpenURL::TOpenURL()
{
}
//---------------------------------------------------------------------------
__fastcall TOpenURL::~TOpenURL()
{
}
//---------------------------------------------------------------------------

bool TOpenURL::OpenURL(const String pURL, const bool pDisplayError)
{
#if defined(__ANDROID__)
 String xURL = (pURL.Pos("mailto:")==0) ? pURL : TIdURI::URLEncode(pURL);
 _di_JIntent xIntent = TJIntent::JavaClass->init(
             TJIntent::JavaClass->ACTION_VIEW,
             TJnet_Uri::JavaClass->parse(StringToJString(xURL)) );
 try {
  SharedActivity()->startActivity(xIntent);
  return true;
 } catch (const Exception &e) {
  if (pDisplayError) ShowMessage("Error: " + e.Message);
  return false;
 }
#elif defined(__APPLE__)
 NSUrl NSU = StrToNSUrl(TIdURI::URLEncode(pURL));
 if (SharedApplication()->canOpenURL(NSU)) {
  return SharedApplication()->openUrl(NSU);
 } else {
  if (pDisplayError) ShowMessage("Error, no se pudo abrir el URL:\n" + pURL);
  return false;
 }
#else
 throw(Exception("Platform not supported!"));
#endif
}

El ejemplo original la encontré en código Delphi XE5 aquí.


¿Cómo usarla? aquí unas pocas líneas de código:
1
2
 std::unique_ptr<TOpenURL> xOURL(new TOpenURL());
 xOURL->OpenURL("https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=ZZKU4L2CD2Y28");

Nota: No tuve opción de probarlo en iPhone.

No hay comentarios.:

Publicar un comentario

Por favor ser gentil.