iphone - 从未调用过 shouldStartLoadWithRequest 委托(delegate)方法

标签 iphone ios xcode uiwebview appdelegate

我正在创建一个具有 WebView 的应用程序。当用户单击 Web View 中的链接或按钮时,我希望能够获取新的 url 并编辑新的 url。

shouldStartLoadWithRequest 应该可以解决问题,但是当我单击链接时,永远不会调用该方法。我找不到为什么这不起作用。

我在某处读到我需要添加这一行:

webView.delegate = (id)self;

我尝试了一下,还是遇到同样的问题。请帮忙

AppDelegate.m

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *currentURL = [[request URL] absoluteString] ;

    NSLog(@"Url: %@", currentURL);
    return YES;
}

Controller .m

- (void)viewDidLoad
{
    webView.delegate = (id)self;
    NSString *tokenString = @"123";
    [super viewDidLoad];

    NSString *fullURL = [[NSString alloc] initWithFormat:@"http://192.168.1.69:8888/webapp/test.php?uid=%@", tokenString];
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];
}

最佳答案

如果您的 Controller 实例将自身添加为委托(delegate),那么您添加到 AppDelegate 实例的方法将永远不会被调用。

shouldStartLoadWithRequest 方法移至 Controller 类中。

关于iphone - 从未调用过 shouldStartLoadWithRequest 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17477063/

相关文章:

ios - 如何对齐 UIImage 下面的 UIButton 中的文本,因为它没有正确显示

ios - "Unable to create XXX-Info.plist"使用 Jenkins 构建

iphone - 播放声音,等待播放完成并继续 (iphone)

iphone - 获取国家域名代码

ios - 导出时的 AVVideoCompositionCoreAnimationTool 仅发送图像和音频,但不发送视频

ios - 获取嵌入到 CollectionView 中的 TableView 中的 IndexPath 项

ios - 离线下载国家 SKMaps map 包

objective-c - 没有在 Xcode 中嵌入一些 youtube 视频

ios - 从 Swift 指定 Objective-C 闭包变量名称

iphone - 如何做到 tabBar.title != navBar.title ?