iOS - UIWebView - 需要提交表单才能在 Safari 中打开

发布于 2024-12-06 18:42:44 字数 1274 浏览 1 评论 0原文

我一直在为 iOS 平台开发 HTML5 / Javascript 网络应用程序。我对 Objective 和 XCode 比较陌生,但我了解该程序的方法。我试图让 UIWebView 打开一个表单,一旦在 Safari 中提交(提交按钮),而不是在当前的 UIWebView 中打开它。

建议使用以下代码在 Safari 中打开标准 标签:

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

return YES;

}

(原始帖子 此处

我尝试专门针对提交的表单调整此代码,如下所示:

- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)inType {

if ( inType == UIWebViewNavigationTypeFormSubmitted ) {
    [[UIApplication sharedApplication] openURL:[request URL]];
    return YES;
}
if ( inType == UIWebViewNavigationTypeFormResubmitted ) {
    [[UIApplication sharedApplication] openURL:[request URL]];
    return YES;
}

return YES;

}

仅供参考,我已将每个 IF 语句的返回值切换为 YES / NO,但这并不似乎是问题......有什么想法吗?代码?理论? 谢谢!

I have been developing an HTML5 / Javascript webapp for the iOS platform. I am relatively new to Objective and XCode, but I know my way around the program. I am attempting to have the UIWebView open a form, once submitted (submit button) in Safari as opposed to opening it in the current UIWebView.

The code below is suggested for opening standard <a> tags in Safari:

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

return YES;

}

( original post here )

I have tried adapting this code specifically for submitted forms like so:

- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)inType {

if ( inType == UIWebViewNavigationTypeFormSubmitted ) {
    [[UIApplication sharedApplication] openURL:[request URL]];
    return YES;
}
if ( inType == UIWebViewNavigationTypeFormResubmitted ) {
    [[UIApplication sharedApplication] openURL:[request URL]];
    return YES;
}

return YES;

}

And FYI I have switched the return values of each IF statement to YES / NO, however this doesn't seem to be the issue... Any thoughts? Code? Theory? Thank You!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空宴 2024-12-13 18:42:44

以下实现了我正在寻找的“在 Safari 中打开”结果。

首先,我通过将 webView.delegate = self; 添加到我的 viewDidLoad 声明中来实现 UIWebViewDelegate 协议:

appViewController.m

- (void)viewDidLoad 
{
    [super viewDidLoad];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];  
    NSData *htmlData = [NSData dataWithContentsOfFile:filePath];  
    if (htmlData) {  
        NSBundle *bundle = [NSBundle mainBundle]; 
        NSString *path = [bundle bundlePath];
        NSString *fullPath = [NSBundle pathForResource:@"index" ofType:@"html" inDirectory:path];
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fullPath]]];
        webView.delegate = self;
    }
}

其次,我添加了以下

-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request 
                                               navigationType:(UIWebViewNavigationType)navigationType 
{
    NSURL* url = [[request URL] retain];
    if (navigationType == UIWebViewNavigationTypeFormSubmitted || navigationType ==     UIWebViewNavigationTypeFormSubmitted)
    {
        return ![[UIApplication sharedApplication] openURL:url];
    }
    return YES;
}

内容:一点令人好奇的是 XCode 返回了以下警告标志:

警告:“appViewController”类未实现“UIWebViewDelegate”协议

不知道为什么......但是!该应用程序现在会在 Safari 中打开提交表单的结果。

The following achieved the "Open in Safari" result I was looking for.

First I implemented UIWebViewDelegate protocol by adding webView.delegate = self; to my viewDidLoad declaration:

appViewController.m

- (void)viewDidLoad 
{
    [super viewDidLoad];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];  
    NSData *htmlData = [NSData dataWithContentsOfFile:filePath];  
    if (htmlData) {  
        NSBundle *bundle = [NSBundle mainBundle]; 
        NSString *path = [bundle bundlePath];
        NSString *fullPath = [NSBundle pathForResource:@"index" ofType:@"html" inDirectory:path];
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fullPath]]];
        webView.delegate = self;
    }
}

Secondly I added the following:

-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request 
                                               navigationType:(UIWebViewNavigationType)navigationType 
{
    NSURL* url = [[request URL] retain];
    if (navigationType == UIWebViewNavigationTypeFormSubmitted || navigationType ==     UIWebViewNavigationTypeFormSubmitted)
    {
        return ![[UIApplication sharedApplication] openURL:url];
    }
    return YES;
}

One point of curiosity was that XCode returned the following warning flag:

warning: class 'appViewController' does not implement the 'UIWebViewDelegate' protocol

Not sure why... but! The app now opens the results of the submitted form in Safari.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文