[英]Trying to open a UIWebView link with Safari
嗨,我有一個小問題。 我已嘗試過使用此主題找到的所有類型的解決方案(減去過時的代碼),以獲取UIWebView
鏈接以彈出Safari並將其加載到那里。
到目前為止,我可以獲取要加載到模擬器中的特定大小,但是每次單擊它時,它就會立即加載到那里。 我必須錯過主要步驟,否則AppDelegate .h .m
和ViewController .h .m
完全混亂了。
我熱衷於為第三代iPod / iPhone設備編碼。 我知道Xcode喜歡更新很多,並且我有5.0.2版本。 從本質上來說,我已經退出游戲了一段時間了,所以我基本上還是No0b。
如果您有任何提示,請告訴我。 除了放棄。 大聲笑。 我知道可以做到的。 這就是我所擁有的...
#import "WIAppDelegate.h"
@implementation WIAppDelegate
- (BOOL)webview:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
// This practically disables web navigation from the webView.
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:[request URL]];
return FALSE;
}
return TRUE;
}
#import <UIKit/UIKit.h>
@interface WIViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIWebView *webview;
@end
#import "WIViewController.h"
@interface WIViewController ()
@end
@implementation WIViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *fullURL = @"http://THESITE.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webview loadRequest:requestObj];
}
@end
您需要在充當UIWebViewDelegate
的類上實現webview:shouldStartLoadWithRequest:navigationType:
方法
這很可能存在於WIViewController
類中
@implementation WIViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = @"http://THESITE.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webview loadRequest:requestObj];
_webview.delegate = self;
}
- (BOOL)webview:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (UIWebViewNavigationTypeLinkClicked == navigationType) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
@end
您還需要確保將此類實際設置為UIWebViewDelegate
我將其作為viewDidLoad
的最后一行,但如果願意,可以將其掛接到xib中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.