Reputation: 229
I am trying to open links from UIWebView and open it to the another app. I'm testing it first on social media, Facebook and Twitter are working well but Instagram and Youtube are not. See my code below.
- (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;
}
Also, after this I will try opening different chosen links from UIWebView and pass it to Safari, I hope you can give answers for this as well.
Thanks!
Upvotes: 0
Views: 1090
Reputation: 319
Are you sure that YouTube App has "youtube://" URL type, or if it has, maybe YouTube app has another parsing that "youtube://profile/****" ?
Sorry for my english =)
UPDATE:
Finally found a solution:
YouTube:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"youtube://www.youtube.com/user/FlawlessFaceandBody"]];
Instagram:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"instagram://user?username=myflawless"]];
Also for Instagram you can find additional URL schemes at the link below:
https://www.instagram.com/developer/mobile-sharing/iphone-hooks/
Upvotes: 1
Reputation: 1224
Did you experience to see this kind of error "This app is not allowed to query for scheme xxx" in your syslog? I guess that Youtube and Instagram are not in your whitelist. You can check this by observing that calling the "canOpenURL
" method on a URL that is not in your whitelist, it will return "NO
", even if as you said, both Instagram and Youtube installed on your phone. I hope this would help you.
Upvotes: 2