2017-04-05 17 views
0

所以我一直在寻找,似乎无法找到答案。在里面ViewController.m我的Objective-C的应用程序,我在我的包裹的web应用程序在网页视图像这样的:iOS Objective-C的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"); 
} 
}]; 

} 

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

+0

你现在将打开一个浏览器窗口,您的应用程序之外的代码。 – firstinq

+0

那么我怎么会打开它在我的手机,而不是任何想法? – Mohammed

+0

用http://myapp.com/clicks准备NSURLRequest并使用UIWebview加载请求 – firstinq

回答

0

您目前的代码只是通过让iOS处理URL的打开来在应用程序之外打开URL。为了在您的webview中打开URL,您必须修改代码以这样的事情:

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

enter image description here使用的代码,前进,后退和停止网络视图内

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

希望,这有助于别人。

+0

感谢您加载,我想知道是否可以将相机功能添加到我的Web应用程序。我有一个允许使用表格(POST)上传图片的路线,但我希望能够使用本机相机功能? – Mohammed

+0

我会尽力,如果它帮助upvote –

0

请试试这个,应该工作

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