简体   繁体   English

从打包为原生iOS应用的Web应用打开外部链接

[英]Open external link from web app packaged as native iOS app

I have a link on my mobile app (created with Sencha Touch 2) with a target="_blank" attribute. 我的移动应用程序(使用Sencha Touch 2创建)上有一个链接,其中包含target="_blank"属性。 The app is packaged as a native iOS app. 该应用程序打包为本机iOS应用程序。 The problem is, the link is not opening in Safari as expected, instead it opens inside the app. 问题是,链接没有按预期在Safari中打开,而是在应用程序内打开。 It is very important that the link opens in Safari in a new browser window. 在新的浏览器窗口中,在Safari中打开链接非常重要。 How can I achieve that? 我怎样才能做到这一点?

I should add that I am using the native packager of Sencha (sencha package). 我应该补充一点,我正在使用Sencha的本地打包器(sencha包)。 The default behaviour seems to open the new window in the same webview. 默认行为似乎在同一webview中打开新窗口。 But I need them to be opened in mobile Safari. 但我需要在移动Safari中打开它们。

In an Xcode project I could do the following: 在Xcode项目中,我可以执行以下操作:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        [[UIApplication sharedApplication] openURL:request.URL];
        return NO;
    }
    return YES;
}

How to do that with Sencha Touch native packaging? 如何使用Sencha Touch原生包装?

I use this to open links in either Mobile Safari, or a new window. 我使用它在Mobile Safari或新窗口中打开链接。 Works with Native iOS packaging and web apps: 适用于原生iOS包装和网络应用程序:

function externalLink(link) {
try{
    Ext.device.Device.openURL(link);
}catch(err) {
    window.open(link, '_blank');
}
}

I haven't discovered a way to do this with sencha native packaging. 我还没有发现使用sencha原生包装的方法。

But wrapping the application in a phonegap and compiling the application in Xcode, with required urls added to the ExternalHost array in phonegap.plist should work right? 但是将应用程序包装在一个phonegap中并在Xcode中编译应用程序,并将所需的url添加到phonegap.plist中的ExternalHost数组中应该可以正常工作吗?

Also, (still on the Phonegap solution) I assume hacking AppDelegate.m to open links in Safari would work just as fine. 此外,(仍然在Phonegap解决方案上)我假设黑客AppDelegate.m在Safari中打开链接也可以正常工作。

Programmatically (Javascript) opens a link in Mobile Safari. 以编程方式(Javascript)在Mobile Safari中打开一个链接。 Ideal if you cannot use a standard html link but you must use Javascript: 如果你不能使用标准的html链接,但你必须使用Javascript,这是理想的选择:

var a = document.createElement("a");
a.setAttribute('href', facebook);
a.setAttribute('target', '_blank');
a.click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM