2Pascal-新时代的Pascal

 找回密码
 立即注册
搜索
热搜: fastreport
查看: 1557|回复: 3
打印 上一主题 下一主题

app打开手机默认浏览器

[复制链接]

3

主题

10

帖子

36

积分

新手上路

Rank: 1

积分
36
跳转到指定楼层
楼主
发表于 2015-11-4 15:23:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
京东购书支持本站
请问一下win下面ShellExecute(0, nil, '[img]file:///C:\Users\ADMINI~1\AppData\Local\Temp\8LDO48C$8@[GWU0353$FOVS.png[/img]http://baidu.com', nil, nil, 1);这样可以打开默认浏览器,那么OS、安卓,IOS上怎么做呢。
之前有人给我说用SharedApplication.openURL(StrToNSUrl('http://baidu.com')); 可以任然不能用。

回复

使用道具 举报

3

主题

10

帖子

36

积分

新手上路

Rank: 1

积分
36
沙发
 楼主| 发表于 2015-11-4 15:46:08 | 只看该作者
京东数码购物支持本站
[mw_shl_code=delphi,true]unit OpenViewUrl;

interface

uses
{$IFDEF ANDROID}
  Androidapi.Helpers,
  FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.Net, Androidapi.JNI.JavaTypes;
{$ENDIF}
{$IFDEF IOS}
  Macapi.Helpers, iOSapi.Foundation, FMX.Helpers.iOS,
{$ENDIF}
  IdURI, SysUtils, Classes, FMX.Dialogs;
// URLEncode is performed on the URL
// so you need to format it   protocol://path
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;

implementation

function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
{$IFDEF ANDROID}
var
  Intent: JIntent;
begin
// There may be an issue with the geo: prefix and URLEncode.
// will need to research
  Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
    TJnet_Uri.JavaClass.parse(StringToJString(TIdURI.URLEncode(URL))));
  try
    SharedActivity.startActivity(Intent);
    exit(true);
  except
    on e: Exception do
    begin
      if DisplayError then ShowMessage('Error: ' + e.Message);
      exit(false);
    end;
  end;
end;
{$ELSE}
{$IFDEF IOS}
var
  NSU: NSUrl;
begin
  // iOS doesn't like spaces, so URL encode is important.
  NSU := StrToNSUrl(TIdURI.URLEncode(URL));
  if SharedApplication.canOpenURL(NSU) then
    exit(SharedApplication.openUrl(NSU))
  else
  begin
    if DisplayError then
      ShowMessage('Error: Opening "' + URL + '" not supported.');
    exit(false);
  end;
end;
{$ELSE}
begin
  raise Exception.Create('Not supported!');
end;
{$ENDIF IOS}
{$ENDIF ANDROID}

end.[/mw_shl_code]
找到办法了,这是openurl的公用函数,直接这么调用即可
[mw_shl_code=delphi,true]{$IFDEF IOS}
  OpenURL('http://baidu.com',true);
  {$ENDIF}[/mw_shl_code]


这个方法可以让IOS跳转,安卓上不识别IdURI,安卓我继续研究,研究OK,附上源码。
回复 支持 反对

使用道具 举报

3

主题

10

帖子

36

积分

新手上路

Rank: 1

积分
36
板凳
 楼主| 发表于 2015-11-4 17:01:45 | 只看该作者
京东购书支持本站
[mw_shl_code=delphi,true]unit OpenViewUrl;

interface

// URLEncode is performed on the URL
// so you need to format it   protocol://path
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;

implementation

uses
{$IFDEF ANDROID}
  IdURI, SysUtils, Classes, FMX.Dialogs,
  FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.Net,Androidapi.Helpers,Androidapi.JNI.JavaTypes;
{$ENDIF}
{$IFDEF IOS}
  IdURI, SysUtils, Classes, FMX.Dialogs,
  Macapi.Helpers, iOSapi.Foundation, FMX.Helpers.iOS;
{$ENDIF}
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
{$IFDEF ANDROID}
var
  Intent: JIntent;
begin
// There may be an issue with the geo: prefix and URLEncode.
// will need to research
  Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
    TJnet_Uri.JavaClass.parse(StringToJString(TIdURI.URLEncode(URL))));
  try
    SharedActivity.startActivity(Intent);
    exit(true);
  except
    on e: Exception do
    begin
      if DisplayError then ShowMessage('Error: ' + e.Message);
      exit(false);
    end;
  end;
end;
{$ENDIF}
{$IFDEF IOS}
var
  NSU: NSUrl;
begin
  // iOS doesn't like spaces, so URL encode is important.
  NSU := StrToNSUrl(TIdURI.URLEncode(URL));
  if SharedApplication.canOpenURL(NSU) then
    exit(SharedApplication.openUrl(NSU))
  else
  begin
    if DisplayError then
      ShowMessage('Error: Opening "' + URL + '" not supported.');
    exit(false);
  end;
end;
{$ENDIF}

end.[/mw_shl_code]

修修改改就可以用了。
回复 支持 反对

使用道具 举报

90

主题

293

帖子

8万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
81886
地板
发表于 2015-11-4 17:11:46 | 只看该作者
京东数码购物支持本站
https://sourceforge.net/projects/fmxflyfilesutils/
code\SourceFlyFilesUtils.pas


[mw_shl_code=delphi,true]{$IFDEF POSIX}
const
{$IFDEF UNDERSCOREIMPORTNAME}
  _PU = '_';
{$ELSE}
  _PU = '';
{$ENDIF}
const
  libc        = '/usr/lib/libc.dylib';
  libpthread  = '/usr/lib/libpthread.dylib';
  libiconv    = '/usr/lib/libiconv.dylib';
  libdl       = '/usr/lib/libdl.dylib';

{$IF not Declared(_PU)}
const
  // On Mac OSX, cdecl names have a preceeding underscore
  // if x86 native backend.
  {$IF Defined(UNDERSCOREIMPORTNAME)}
  _PU = '_';
  {$ELSE}
  _PU = '';
  {$ENDIF}
  {$EXTERNALSYM _PU}
{$ENDIF}

const
{$IFNDEF IOS}
  _INODE_SUFFIX = '$INODE64';
{$ELSE IOS}
  _INODE_SUFFIX = '';
{$ENDIF !IOS}
  {$EXTERNALSYM _INODE_SUFFIX}
function _system(Name: MarshaledAString): Integer; cdecl;
  external libc name _PU + 'system';
{$ENDIF}


function GetEncodeURL(const URL: string; Https:Boolean = True): string;
begin
  Result := URL;
  if FileExists(Result) then
  begin
{$IFDEF MSWINDOWS}
{$ELSE}
    if Result.Substring(1, 1) <> '/' then
    begin
      Result := '/' + Result;
    end;
    Result := 'file://' + Result;
{$ENDIF}
  end
  else
  begin
    if Https then
    begin
      Result := 'https://' + URL;
    end
    else
    begin
      Result := 'https://' + URL;
    end;
    Result := TIdURI.URLEncode(Result);
  end;
end;


function OpenFileOnExtApp(const FileName: string; Https:Boolean = True): Boolean;
{$IFDEF ANDROID}
var
  Intent: JIntent;
  FileExtension: string;
  mime: JMimeTypeMap;
  MIMEType: JString;
  FileToOpen: string;
begin
// There may be an issue with the geo: prefix and URLEncode.
// will need to research
  Result := False;
  FileExtension := AnsiLowerCase(ExtractFileExt(FileName));
  mime := TJMimeTypeMap.JavaClass.getSingleton();
  MIMEType := nil;
  if mime <> nil then
  begin
    MIMEType := mime.getMimeTypeFromExtension(StringToJString(FileExtension));
  end;
  if MIMEType <> nil then
  begin
    // 调用相应程序打开当前程序
    Intent := TJIntent.Create;
    Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
    FileToOpen := GetEncodeURL(FileName, Https);
    Intent.setDataAndType(StrToJURI(FileToOpen), MIMEType);
    try
      SharedActivity.startActivity(Intent);
      Result := True;
    except
    end;
  end;

end;
{$ELSE}
{$IFDEF IOS}
var
  NSU: NSUrl;
  AURL: string;
begin
  Result := False;
  AURL := GetEncodeURL(FileName, Https);
  // iOS doesn't like spaces, so URL encode is important.
  NSU := StrToNSUrl(AURL);
  if TOSVersion.Check(9) or SharedApplication.canOpenURL(NSU) then
  try
    Result := SharedApplication.openUrl(NSU);
  except
  end;
end;
{$ELSE}
{$IFDEF MSWINDOWS}
var
  AURL: string;
begin
  Result := False;
  AURL := GetEncodeURL(FileName, Https);
  try
    ShellExecute(GetActiveWindow, 'open', PChar(AURL), '', '', SW_MAXIMIZE);
    Result := True;
  except
  end;
end;
{$ELSE}
var
  M:TMarshaller;
  AURL: string;
begin
  Result := False;
  //AURL := 'open -a Safari ' +  GetEncodeURL(AURL);
  AURL := 'open ' +  GetEncodeURL(FileName, Https);
  try
    _system(M.AsAnsi(AURL, CP_UTF8).ToPointer);
    Result := True;
  except
  end;
//  raise Exception.Create('Not supported!');
end;
{$ENDIF MSWINDOWS}
{$ENDIF IOS}
{$ENDIF ANDROID}[/mw_shl_code]
(C)(P)Flying Wang
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|新时代Pascal论坛

GMT+8, 2024-4-29 21:08 , Processed in 0.061627 second(s), 23 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表