Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在safari中打开UIWebView链接?UIWebView代理放置_Iphone_Xcode_Delegates_Uiwebview_Webview - Fatal编程技术网

Iphone 在safari中打开UIWebView链接?UIWebView代理放置

Iphone 在safari中打开UIWebView链接?UIWebView代理放置,iphone,xcode,delegates,uiwebview,webview,Iphone,Xcode,Delegates,Uiwebview,Webview,我试图在safari中打开UIWebView的链接,但到目前为止没有成功。我相当肯定我对代表们做了错事。你们能看一下吗 这是我的viewcontroller.m中的内容 (BOOL)webView:(UIWebView*)webView应加载请求: (NSURLRequest*)请求导航类型: (UIWebViewNavigationType)navigationType;{ NSURL *requestURL =[[request URL]retain]; if(([[requestURL s

我试图在safari中打开UIWebView的链接,但到目前为止没有成功。我相当肯定我对代表们做了错事。你们能看一下吗

这是我的viewcontroller.m中的内容

(BOOL)webView:(UIWebView*)webView应加载请求: (NSURLRequest*)请求导航类型: (UIWebViewNavigationType)navigationType;{

NSURL *requestURL =[[request URL]retain];
if(([[requestURL scheme]isEqualToString:@"http"])&&(navigationType == 
UIWebViewNavigationTypeLinkClicked)){
return ![[UIApplication sharedApplication]openURL:[requestURL
自动释放]]; } [请求URL发布]; 返回YES;}

很抱歉格式化。无论如何,我的第一个问题是,上面的webView是否应该与我在.h文件中声明的webView相同

我的下一个问题是关于授权webview。这是我的viewcontroller.h

(我知道它不是javascript,但它在这里看起来比在blockquote中更好)

下面是我在.m viewdidload函数中输入的内容(这是一个猜测,我不知道放在哪里,甚至不知道它是否应该是self)

[webView setDelegate:self]


运行此项目时,代码可能根本不存在,链接在应用程序中仍然打开,而不是在safari中。你们能帮我解决我做错了什么,或者给我一些关于如何设置NSLog或其他什么的建议,让我看看哪里出了问题吗?感谢您的帮助

请参阅下面的代码:此代码是Apple示例代码的一部分

#导入
@界面WebViewController:UIViewController
{
UIWebView*myWebView;
}
@属性(非原子,保留)UIWebView*myWebView;
@结束

#导入“WebViewController.h”
#导入“Constants.h”
@WebViewController的实现
@综合myWebView;
-(无效)解除锁定
{
myWebView.delegate=nil;
[myWebView发布];
[super dealoc];
}
-(无效)viewDidLoad
{
[超级视图下载];
self.title=NSLocalizedString(@“WebTitle”,@“”);
CGRect webFrame=[[UIScreen mainScreen]applicationFrame];
webFrame.origin.y+=kTopMargin+5.0;//离开URL输入字段及其标签
webFrame.size.height-=40.0;
self.myWebView=[[UIWebView alloc]initWithFrame:webFrame]autorelease];
self.myWebView.backgroundColor=[UIColor whiteColor];
self.myWebView.scalesPageToFit=是;
self.myWebView.autoresizingMask=(uiviewsautoresizingflexiblewidth | uiviewsautoresizingflexiblewhight);
self.myWebView.delegate=self;
[self.view addSubview:self.myWebView];
CGRect textFieldFrame=CGRectMake(kLeftMargin,kTweenMargin,
self.view.bounds.size.width-(kLeftMargin*2.0),kTextFieldHeight);
UITextField*urlField=[[UITextField alloc]initWithFrame:textFieldFrame];
urlField.borderStyle=UITextBorderStyleBezel;
urlField.textColor=[UIColor blackColor];
urlField.delegate=self;
urlField.placeholder=@”;
urlField.text=@”http://www.apple.com";
urlField.backgroundColor=[UIColor-whiteColor];
urlField.autoresizingMask=UIViewAutoresizingFlexibleWidth;
urlField.returnKeyType=UIReturnKeyGo;
urlField.keyboardType=UIKeyboardTypeURL;//这使键盘对键入URL更加友好
urlField.autocapitalizationType=UITextAutocapitalizationTypeNone;//不大写
urlField.autocorrectionType=UITextAutocorrectionTypeNo;//我们不喜欢键入时自动完成
urlField.clearButtonMode=UITextFieldViewModeAlways;
[urlField setAccessibilityLabel:NSLocalizedString(@“URLTextField”,@“)];
[self.view addSubview:urlField];
[urlField发布];
[self.myWebView加载请求:[NSURLRequest requestWithURL:[NSURL URLWithString:@]http://www.apple.com/"]]];
}
//在视图控制器的视图被释放并设置为nil后调用。
//例如,导致视图被清除的内存警告。由于-dealloc而未调用。
//因此,释放在viewDidLoad中加载或可以延迟重新创建的所有属性。
//
-(无效)视图卸载
{
[超级视频下载];
//释放并设置为零
self.myWebView=nil;
}
#布拉格标记-
#pragma标记UIViewController委托方法
-(无效)视图将显示:(BOOL)动画
{
self.myWebView.delegate=self;//在显示web视图时设置委托
}
-(无效)视图将消失:(BOOL)已设置动画
{
[self.myWebView停止加载];//如果web视图仍在加载其内容
self.myWebView.delegate=nil;//由于webview处于隐藏状态,请断开委托
[UIApplication sharedApplication].networkActivityIndicatorVisible=否;
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
//我们在此视图控制器中支持旋转
返回YES;
}
//这有助于在单击“完成”按钮时关闭键盘
-(BOOL)textField应返回:(UITextField*)textField
{
[textField resignFirstResponder];
[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[textField text]]];
返回YES;
}
#布拉格标记-
#pragma标记UIWebViewDelegate
-(void)webView加载:(UIWebView*)webView
{
//启动加载时,在状态栏中显示活动指示器
[UIApplication sharedApplication].networkActivityIndicatorVisible=是;
}
-(无效)webViewDidFinishLoad:(UIWebView*)webView
{
//加载完成后,在状态栏中隐藏活动指示器
[UIApplication sharedApplication].networkActivityIndicatorVisible=否;
}
-(void)webView:(UIWebView*)webView失败加载错误:(NSError*)错误
{
//加载错误,在状态栏中隐藏活动指示器
[UIApplication sharedApplication].networkActivityIndicatorVisible=否;
//在webview中报告错误
NSString*errorString=[NSString stringWithFormat:
@“出现错误:
%@”, 错误。本地化描述]; [self.myWebView loadHTMLString:errorString baseURL:nil]; } @结束
参考下面的代码:此代码是Apple示例代码的一部分
#import <UIKit/UIKit.h>

@interface WebViewController : UIViewController <UITextFieldDelegate, UIWebViewDelegate>
{
    UIWebView *myWebView;
}

@property (nonatomic, retain) UIWebView *myWebView;

@end
#import "WebViewController.h"
#import "Constants.h"

@implementation WebViewController

@synthesize myWebView;

- (void)dealloc
{
    myWebView.delegate = nil;
    [myWebView release];

    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = NSLocalizedString(@"WebTitle", @"");

    CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
    webFrame.origin.y += kTopMargin + 5.0;  // leave from the URL input field and its label
    webFrame.size.height -= 40.0;
    self.myWebView = [[[UIWebView alloc] initWithFrame:webFrame] autorelease];
    self.myWebView.backgroundColor = [UIColor whiteColor];
    self.myWebView.scalesPageToFit = YES;
    self.myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    self.myWebView.delegate = self;
    [self.view addSubview:self.myWebView];

    CGRect textFieldFrame = CGRectMake(kLeftMargin, kTweenMargin,
                                       self.view.bounds.size.width - (kLeftMargin * 2.0), kTextFieldHeight);
    UITextField *urlField = [[UITextField alloc] initWithFrame:textFieldFrame];
    urlField.borderStyle = UITextBorderStyleBezel;
    urlField.textColor = [UIColor blackColor];
    urlField.delegate = self;
    urlField.placeholder = @"<enter a URL>";
    urlField.text = @"http://www.apple.com";
    urlField.backgroundColor = [UIColor whiteColor];
    urlField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    urlField.returnKeyType = UIReturnKeyGo;
    urlField.keyboardType = UIKeyboardTypeURL;  // this makes the keyboard more friendly for typing URLs
    urlField.autocapitalizationType = UITextAutocapitalizationTypeNone; // don't capitalize
    urlField.autocorrectionType = UITextAutocorrectionTypeNo;   // we don't like autocompletion while typing
    urlField.clearButtonMode = UITextFieldViewModeAlways;
    [urlField setAccessibilityLabel:NSLocalizedString(@"URLTextField", @"")];
    [self.view addSubview:urlField];
    [urlField release];

    [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]]];
}

// called after the view controller's view is released and set to nil.
// For example, a memory warning which causes the view to be purged. Not invoked as a result of -dealloc.
// So release any properties that are loaded in viewDidLoad or can be recreated lazily.
//
- (void)viewDidUnload
{
    [super viewDidUnload];

    // release and set to nil
    self.myWebView = nil;
}


#pragma mark -
#pragma mark UIViewController delegate methods

- (void)viewWillAppear:(BOOL)animated
{
    self.myWebView.delegate = self; // setup the delegate as the web view is shown
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.myWebView stopLoading];   // in case the web view is still loading its content
    self.myWebView.delegate = nil;  // disconnect the delegate as the webview is hidden
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // we support rotation in this view controller
    return YES;
}

// this helps dismiss the keyboard when the "Done" button is clicked
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[textField text]]]];

    return YES;
}


#pragma mark -
#pragma mark UIWebViewDelegate

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    // starting the load, show the activity indicator in the status bar
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    // finished loading, hide the activity indicator in the status bar
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    // load error, hide the activity indicator in the status bar
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    // report the error inside the webview
    NSString* errorString = [NSString stringWithFormat:
                             @"<html><center><font size=+5 color='red'>An error occurred:<br>%@</font></center></html>",
                             error.localizedDescription];
    [self.myWebView loadHTMLString:errorString baseURL:nil];
}

@end