2017-03-09 1 views
1

私は、アプリケーションのロード時に私のウェブサイトを開くObjective-cアプリケーションを持っています。URLがObjecitve-Cの私のウェブサイトと異なるかどうかを検出します

私のウェブサイトのリンクのほとんどは、異なるウェブサイト/ URLを指しています。

私の目的-Cコードを更新しようとしています。どこのURLが私のウェブサイトと異なる場合、私のAPPで開くのではなく、SafariブラウザでURLを開きます。

これも可能ですか?ここで

が私のコードである

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 

@property (weak, nonatomic) IBOutlet UIWebView *webView; 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Load the url into the webview 
    NSURL *url = [NSURL URLWithString:@"http://mywebsite.com/"]; 
    [self.webView loadRequest:[NSURLRequest requestWithURL:url]]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 
+2

をあなたは 'UIWebViewDelegate'の方法で見たことがありますか? – rmaddy

+0

http://stackoverflow.com/questions/28040261/how-to-load-only-html-page-in-webview-and-all-www-page-load-in-safari-in-ios – rmaddy

答えて

1

あなたはUIWebViewDelegate、具体的に実装する:

- (void)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)type { 
    if (![request.url.host isEqualToString:@"mywebsite.com"]) { 
     [UIApplication.sharedApplication openURL:request.absoluteURL]; 
     return NO; 
    } 
    return YES; 
} 

かつ迅速

func webView(webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType navType: UIWebViewNavigationType) -> Bool { 
    if request.url?.host != "mywebsite.com" { 
     UIApplication.shared.openURL(request.absoluteURL) 
     return false 
    } 
    return true 
} 
+0

質問SwiftではなくObjective-Cというタグが付けられています。 – rmaddy

+0

だから、すぐに慣れて、obj-cも追加します。 –

+0

1. 'URLRequest'は' host'プロパティを持っていません。 2.まず、ナビゲーションタイプがリンクをクリックしていることを確認する必要があります。 – rmaddy

1

にあなたはのUIWebViewと使用のデリゲートを実装が必要です。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { 

    if (navigationType == UIWebViewNavigationTypeLinkClicked) { 
     NSString *url = request.URL.host; 

     if([url rangeOfString:@"http://mywebsite.com"].location == NSNotFound) { 
      [[UIApplication sharedApplication] openURL:request.URL]; 
      return NO; 
     } 
     return YES; 
     } 

     return YES; 

} 
+1

完全なURLで部分文字列を探すのではなく、URLの 'host'部分だけを見ていませんか? – rmaddy

+1

そして 'openURL'を呼び出した後に' NO'を返す必要があります。 'true'を' YES'(これはObjective-Cです)に置き換えます。 – rmaddy

関連する問題