【问题标题】:Trying to open a UIWebView link with Safari尝试使用 Safari 打开 UIWebView 链接
【发布时间】:2014-03-16 02:51:49
【问题描述】:

您好,我有一个小问题。我已经尝试了我能找到的所有类型的解决方案,减去过时的代码,关于获取UIWebView 链接以弹出打开 Safari 并将其加载到那里的主题。

到目前为止,我可以在模拟器中加载特定的大小,但是每次单击它时,它都会加载到那里。我必须错过重要的一步,否则AppDelegate .h .mViewController .h .m 完全搞砸了。

我热衷于为第三代 iPod/iPhone 等设备编写代码。我知道 Xcode 喜欢更新很多,我有 5.0.2 版本。我基本上又是No0b了,因为我已经退出游戏一段时间了。

如果您有任何提示,请告诉我。除了放弃它。哈哈。我知道这是可以做到的。这是我所拥有的......

#import "WIAppDelegate.h"

@implementation WIAppDelegate

- (BOOL)webview:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
   navigationType:(UIWebViewNavigationType)navigationType {
    // This practically disables web navigation from the webView.
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        [[UIApplication sharedApplication] openURL:[request URL]];
        return FALSE;
    }
    return TRUE;
}


#import <UIKit/UIKit.h>

@interface WIViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIWebView *webview;

@end

#import "WIViewController.h"

@interface WIViewController ()

@end

@implementation WIViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *fullURL = @"http://THESITE.com";
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [_webview loadRequest:requestObj];
}

@end

【问题讨论】:

    标签: ios iphone objective-c uiwebview uiwebviewdelegate


    【解决方案1】:

    您需要在充当UIWebViewDelegate 的类上实现webview:shouldStartLoadWithRequest:navigationType: 方法

    这很可能存在于您的 WIViewController 类中

    @implementation WIViewController
    
    - (void)viewDidLoad
    {
      [super viewDidLoad];
    
      NSString *fullURL = @"http://THESITE.com";
      NSURL *url = [NSURL URLWithString:fullURL];
      NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
      [_webview loadRequest:requestObj];
      _webview.delegate = self;
    }
    
    - (BOOL)webview:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
    {
      if (UIWebViewNavigationTypeLinkClicked == navigationType) {
        [[UIApplication sharedApplication] openURL:[request URL]];
        return NO;
      }
      return YES;
    }
    
    @end
    

    您还需要确保将此类实际设置为 UIWebViewDelegate 我已将此作为 viewDidLoad 的最后一行,但如果您愿意,可以将其连接到 xib 中

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2014-03-11
      • 2011-12-01
      • 1970-01-01
      相关资源
      最近更新 更多