Objective-C:在UIWebView外部打开链接

时间:2016-05-16 07:26:15

标签: ios objective-c uiwebview

我正在尝试从UIWebView打开链接并将其打开到另一个应用程序。 我首先在社交媒体上测试它,Facebook和Twitter运行良好,但Instagram和Youtube不是。请参阅下面的代码。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeLinkClicked ) {
        if ([request.URL.scheme isEqualToString:@"https"]) {
            if ([request.URL.host isEqualToString:@"www.facebook.com"]) {
                NSLog(@"FB FROM WEBVIEW BUTTON");
                if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb://"]]) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"fb://profile/FlawlessFaceandBody"]];
                }
                else {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.facebook.com/FlawlessFaceandBody/"]];
                    NSLog(@"No FB APP");
                }
                [self homepage];
            }
            return YES;
        }
        else if ([request.URL.scheme isEqualToString:@"https"]) {
            if ([request.URL.host isEqualToString:@"twitter.com/myflawless"]) {
                NSLog(@"TW FROM WEBVIEW BUTTON");
                if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"twitter://"]]) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"twitter://profile/myflawless"]];
                }
                else {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://twitter.com/myflawless?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor"]];
                    NSLog(@"No TW APP");
                }
                [self homepage];
        }
            return YES;
        }
        else if ([request.URL.scheme isEqualToString:@"https"]) {
            if ([request.URL.host isEqualToString:@"instagram.com/myflawless"]) {
                NSLog(@"IG FROM WEBVIEW BUTTON");
                if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"instagram:/user/"]]) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"instagram://profile/myflawless"]];
                }
                else {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.instagram.com/myflawless/"]];
                    NSLog(@"No IG APP");
                }
                [self homepage];
            }
            return YES;
        }
    }
    else if ([request.URL.scheme isEqualToString:@"https"]) {
        if ([request.URL.host isEqualToString:@"youtube.com/FlawlessFaceandBody"]) {
            NSLog(@"YT FROM WEBVIEW BUTTON");
            if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"youtube://"]]) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"youtube://profile/FlawlessFaceandBody"]];
            }
            else {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.youtube.com/user/FlawlessFaceandBody"]];
                NSLog(@"No YT APP");
            }
            [self homepage];
        }
        return YES;
    }

    return YES;
}

此外,在此之后,我将尝试从UIWebView打开不同的选定链接并将其传递给Safari,我希望您也可以为此提供答案。

谢谢!

2 个答案:

答案 0 :(得分:2)

您是否有过在系统日志中看到此类错误“此应用程序不允许查询方案xxx”的经历?我猜Youtube和Instagram不在你的白名单中。您可以通过观察在不在白名单中的网址上调用“canOpenURL”方法进行检查,它会返回“NO”,即使如您所说,安装了Instagram和Youtube您的手机。我希望这会对你有所帮助。

答案 1 :(得分:1)

您确定YouTube应用有" youtube://"网址类型,或者如果有,也许YouTube应用程序有另一种解析方式" youtube:// profile / ****" ?

抱歉我的英文=)

<强>更新

终于找到了解决方案:

的YouTube:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"youtube://www.youtube.com/user/FlawlessFaceandBody"]];

Instagram的:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"instagram://user?username=myflawless"]];

另外,对于Instagram,您可以在以下链接中找到其他URL方案:

https://www.instagram.com/developer/mobile-sharing/iphone-hooks/