Objective-C: 在 UIWebView 之外打开链接
Objective-C: Open Links Outside 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,希望您也能给出答案。
谢谢!
您确定 YouTube 应用有 "youtube://" URL 类型吗?如果有,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,您还可以在下面的 link 中找到其他 URL 方案:
https://www.instagram.com/developer/mobile-sharing/iphone-hooks/
您是否遇到过在您的系统日志中看到这种错误 "This app is not allowed to query for scheme xxx"?我猜 Youtube 和 Instagram 不在您的白名单中。您可以通过观察在不在白名单中的 URL 上调用“canOpenURL
”方法来检查这一点,它会 return“NO
”,即使您说,你的 phone 上安装了 Instagram 和 Youtube。希望对您有所帮助。
我正在尝试从 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,希望您也能给出答案。
谢谢!
您确定 YouTube 应用有 "youtube://" URL 类型吗?如果有,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,您还可以在下面的 link 中找到其他 URL 方案:
https://www.instagram.com/developer/mobile-sharing/iphone-hooks/
您是否遇到过在您的系统日志中看到这种错误 "This app is not allowed to query for scheme xxx"?我猜 Youtube 和 Instagram 不在您的白名单中。您可以通过观察在不在白名单中的 URL 上调用“canOpenURL
”方法来检查这一点,它会 return“NO
”,即使您说,你的 phone 上安装了 Instagram 和 Youtube。希望对您有所帮助。