【问题标题】:iOS Objective-C webview link button to webpageiOS Objective-C webview链接按钮到网页
【发布时间】:2017-08-31 18:59:02
【问题描述】:

所以我一直在四处寻找,似乎无法找到答案。在 ViewController.m 内的 Objective-C 应用程序中,我将我的网络应用程序包装在这样的网络视图中:

 - (void)viewDidLoad {
    [super viewDidLoad];

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

并且我遵循了一个允许我添加工具栏的教程:

来回走没问题,但这不是我想要的。我希望能够拥有诸如“登录”之类的按钮,它将带我到https://myapp.com/signin 等。但我已经搜索了很多,但似乎无法找到如何做到这一点。

我不太确定,但要在 m 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");
}
}];

}

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

【问题讨论】:

  • 您现在拥有的代码将在您的应用程序之外打开一个浏览器窗口。
  • 那么我如何在手机中打开它而不是任何想法?
  • myapp.com/clicks准备一个NSURLRequest并用UIWebview加载请求

标签: ios objective-c iphone xcode uiwebview


【解决方案1】:

试试这个,应该可以的

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

【讨论】:

    【解决方案2】:

    在网页视图中使用前进、后退和停止代码。

    在 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");
    }
    

    希望,这对某人有帮助。

    【讨论】:

    • 谢谢加载,我想知道是否可以将相机功能添加到我的网络应用程序中。我有一条允许使用表单 (POST) 上传图片的路线,但我希望能够使用本机相机功能?
    【解决方案3】:

    您当前的代码只是通过让 iOS 处理 URL 的打开来在您的应用程序之外打开 URL。为了在您的 web 视图中打开 URL,您必须将代码修改为以下内容:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-03
      • 2013-10-07
      相关资源
      最近更新 更多