iOS Objective-C webview链接按钮到网页

时间:2017-04-05 23:52:30

标签: ios objective-c iphone xcode uiwebview

所以我一直在寻找,似乎无法找到答案。在ViewController.m中的Objective-C应用程序中,我将web应用程序包装在这样的webview中:

 - (void)viewDidLoad {
    [super viewDidLoad];

 NSURL *url = [NSURL URLWithString:@"https://myapp.com"];
NSURLRequest *request = [NSURLRequest requestWithURL: url];
[_webView loadRequest:request];
}

我遵循了一个教程,允许我添加一个工具栏: enter image description here

继续前进很好,但那不是我想要的。我希望能够有“登录”这样的按钮,它会带我到https://myapp.com/signin等。但是我搜索了这么多,但似乎无法找到如何做到这一点。

我不太确定,但是我想在ViewController.m中测试一下我正在尝试:

- (IBAction)clicks:(id)sender {

NSURL *spotiURL = [NSURL URLWithString:@"http://myapp.com/clicks"];

[[UIApplication sharedApplication] openURL:spotiURL options:@{}  completionHandler:^(BOOL success) {
 if (success) {
    NSLog(@"Opened url");
}
}];

}

似乎没有做任何事情。请帮忙:(

3 个答案:

答案 0 :(得分:0)

您目前只需通过iOS处理URL的打开来打开应用程序外部的URL。要在您的网页浏览中打开网址,您必须将代码修改为以下内容:

- (IBAction)clicks:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://myapp.com/clicks"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];    
}

答案 1 :(得分:0)

enter image description here在Web视图中使用前向,后退和停止代码。

在ViewController.h中

@interface ViewController : UIViewController<UIWebViewDelegate>
@property(strong,nonatomic) IBOutlet UIWebView *webview;

- (IBAction)backButtonTapped:(id)sender;
- (IBAction)forwardButtonTapped:(id)sender;
- (IBAction)StopButtonClick:(id)sender;

在ViewController.m

- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"WebView : %@",_webview);
_webview.delegate = self;
[_webview setUserInteractionEnabled: YES];

NSURL *targetURL = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
if (!request) {
    UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"Network Error" message:@"Data not received due to network connection.Try again..." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alertview show];
}
else
{
    NSLog(@"Data loaded...");
}
[_webview loadRequest:request];
}

- (IBAction)backButtonTapped:(id)sender {
UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"Back button pressed." message:@"" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertview show];
[_webview stopLoading];
NSLog(@"back button pressed");
[_webview goBack];
}
- (IBAction)forwardButtonTapped:(id)sender {
UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"forward button pressed." message:@"" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertview show];
[_webview stopLoading];
NSLog(@"forward button pressed");
[_webview goForward];
}

- (IBAction)StopButtonClick:(id)sender {
UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"Stop button pressed." message:@"" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertview show];
[_webview stopLoading];
NSLog(@"Stop button pressed");
}

希望,这对某人有帮助。

答案 2 :(得分:0)

请尝试这个,应该工作

- (IBAction)clicks:(id)sender {
NSMutableURLRequest *requestObj = [[NSMutableURLRequest alloc]initwithURL:[NSURL URLWithString:@"http://myapp.com/clicks"]];
[_webView loadRequest:requestObj];
}