Objective C iOS 中从 URL 获取用户指定的信息
目前我正在使用 UIWebview,它显示用户提供的网页内容。在这个内容中会有其他网页内容的链接,我需要决定是在 safari 还是在 UIWebview 中打开链接。
我当前的解决方案是在 html 中的 URL 中添加一个 queryString,例如 Visit W3Schools
当用户按下链接时,UIViewController 中的以下方法决定如何打开链接。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [request URL];
NSLog(@"%@",url);
NSLog(@"%@",[url scheme]);
NSLog(@"%@",[url user]);
NSLog(@"%@",[url query]);
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
if ([[url query]isEqualToString:@"OutOfAppApp"]) {
NSLog(@"go out");
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else if([[url query]isEqualToString:@"StayInApp"])
{
return YES;
}
}
return YES;
}
我在 Web 开发方面没有太多经验,因此看不到使用这种方法的后果。这是一种有效的方法还是我应该使用其他方法?
问候
编辑: 我现在想出了另一种使用自定义 URL 方案的方法,但我仍然希望评论这是否是一个有效的解决方案:)
我已经在链接中添加了前缀,例如 访问 W3Schools
当用户按下链接时,应用程序会检查前缀并决定是在应用程序中还是在 safari 中启动 URL 的其余部分。这是通过以下方法完成的:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [request URL];
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
if ([[url scheme]isEqualToString:@"InAPP"]) {
NSLog(@"go in");
NSURLRequest *req=[NSURLRequest requestWithURL:[self removeExtensionFromURL:url]];
[[self browserView] loadRequest:req];
return NO;
}
else if([[url scheme]isEqualToString:@"OutAPP"])
{
NSLog(@"go out");
[[UIApplication sharedApplication] openURL:[self removeExtensionFromURL:url]];
return NO;
}
}
return YES;
}
-(NSURL*)removeExtensionFromURL:(NSURL*)url
{
NSMutableString *urlString=[NSString stringWithFormat:@"%@",url];
NSString *newURLString;
if ([[url scheme]isEqualToString:@"InAPP"]) {
newURLString = [urlString stringByReplacingOccurrencesOfString:@"InAPP:" withString:@""];
}
else if([[url scheme]isEqualToString:@"OutAPP"])
{
newURLString = [urlString stringByReplacingOccurrencesOfString:@"OutAPP:" withString:@""];
}
return [NSURL URLWithString:newURLString];
}
Currently I am working with a UIWebview which shows web content provided from a user. In this content there will be links to other web content and I need to decide whether to open links in safari or in the UIWebview.
My current solution is to add a queryString to the URL in the html e.g. <a href=http://www.w3schools.com?StayInApp target=_blank>Visit W3Schools</a>
When the user presses the link the following method in the UIViewController decides how to open the link.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [request URL];
NSLog(@"%@",url);
NSLog(@"%@",[url scheme]);
NSLog(@"%@",[url user]);
NSLog(@"%@",[url query]);
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
if ([[url query]isEqualToString:@"OutOfAppApp"]) {
NSLog(@"go out");
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else if([[url query]isEqualToString:@"StayInApp"])
{
return YES;
}
}
return YES;
}
I do not have very much experience within web development and can therefore not see the consequences of using this approach. Is this a valid approach or should I use another way?
Regards
EDIT:
I have now come up with another approach using custom URL schemes, but I would still like comments on whether this is a valid solution :)
I have added a prefix to the links such as <a href=InAPP:http://www.w3schools.com target=_blank>Visit W3Schools</a>
When a user presses a link the app examines the prefix and decides whether to launch the rest of the url in the app or in safari. This is done with the following methods:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [request URL];
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
if ([[url scheme]isEqualToString:@"InAPP"]) {
NSLog(@"go in");
NSURLRequest *req=[NSURLRequest requestWithURL:[self removeExtensionFromURL:url]];
[[self browserView] loadRequest:req];
return NO;
}
else if([[url scheme]isEqualToString:@"OutAPP"])
{
NSLog(@"go out");
[[UIApplication sharedApplication] openURL:[self removeExtensionFromURL:url]];
return NO;
}
}
return YES;
}
-(NSURL*)removeExtensionFromURL:(NSURL*)url
{
NSMutableString *urlString=[NSString stringWithFormat:@"%@",url];
NSString *newURLString;
if ([[url scheme]isEqualToString:@"InAPP"]) {
newURLString = [urlString stringByReplacingOccurrencesOfString:@"InAPP:" withString:@""];
}
else if([[url scheme]isEqualToString:@"OutAPP"])
{
newURLString = [urlString stringByReplacingOccurrencesOfString:@"OutAPP:" withString:@""];
}
return [NSURL URLWithString:newURLString];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论