2017-04-05 44 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]; 
}