ios - UIWebview 禁用重定向到 AppStore

标签 ios iphone objective-c

我的应用程序中有 UIWebview,这就是我在应用程序中有时导航到 URL 的方式:

NSURL *url = [NSURL URLWithString:@"http://mp3skull.com/mp3/nirvana.html"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];

[webView loadRequest:request];

有时我注意到,如果我点击链接,AppStore 应用程序会打开一些应用程序。

是否可以禁用它?

最佳答案

在方法 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

你可以做

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *requestedURL = [[request URL] absoluteString];
    // URL for opening itunes according to Apple docs is something like this @"http://itunes.apple.com
    // But I do believe that it will be some sort of URL scheme that opens it specifically to the app on the app store.
    // So without an example this is the best I can provide.
    if([requestedURL isEqualToString:@"http://itunes.apple.com"]) {
    // or   if([requestedURL rangeOfString:@"itunes.apple.com"].location==0) {
        // What is happening here is that if the request url that is being request is 
        // "http://mp3skull.com/mp3/nirvana.html" then we don't want to continue with the request so stop.
        return NO;
    }
    // Otherwise for all other requests continue
    return YES;
}

请记住,您需要在 UIWebView 上设置委托(delegate),因为此方法是 UIWebViewDelegate 方法 - 参见 Apple Documentation on UIWebViewDelegate for more information

关于ios - UIWebview 禁用重定向到 AppStore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21909886/

相关文章:

ios - 以编程方式选择 Collection View 单元格无法执行单元格的某些 isSelected 例程

ios - 为什么 MagnificationGesture 在 SwiftUI 中只更新一次状态?

iphone - 如何检查互联网是否活跃并且工作正常?

html - 在 HTML 文件的内容中搜索特定单词

ios - iOS-脏内存持续增长

iphone - 使用 CALayers 的圆形 UIView - 只有一些角落 - 如何?

ios - 录制带有叠加文本和图像的视频

ios - 如何启用/禁用特定文本字段的 OneTimeCode 功能?

ios - 带有自定义单元格(带有 .xib)的 UITableView 崩溃

iphone - AFnetworking waitUntilFinish 不工作