18 Replies
      Latest reply: Mar 23, 2017 4:20 AM by eskimo RSS
      M.Malamas Level 1 Level 1 (0 points)

        I want to have all the history (all visited website) shown in a table view or anything else. How can I do please ?

        • Re: WebView history
          eskimo Apple Staff Apple Staff (6,015 points)

          You’ll have to build this history yourself, using a delegate callback from the web view to learn about user actions.  If you’re using WKWebView (which you should be, it’s the future of web views on our platforms), you do this via the navigation delegate (WKNavigationDelegate).

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: WebView history
              M.Malamas Level 1 Level 1 (0 points)

              Thank you very much for your time and your answer . This is the first time I use web view , I don't know may things, can you quickly guide me through the steps please ? I'm using objective-c

                • Re: WebView history
                  NotMyName Level 4 Level 4 (550 points)

                  It's an introductory level programming assignment.

                   

                  Edit:  I posted a link to some old documentation.  I'm pretty sure the basic walk through of how to use the WK WebKit classes has been covered in recent WWDC video sessions.  That would be where to start if you need a walk through.

                    • Re: WebView history
                      M.Malamas Level 1 Level 1 (0 points)

                      Thank you for you response . I have a problem , I cannot perform a void for the WKWebview

                      
                      
                      @interface ViewControllerWeb () <WKNavigationDelegate>
                      @property(strong,nonatomic) WKWebView *webView;
                      ....
                      - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
                      
                      NSLog(@"success");
                      
                      }
                      
                      - (void)viewDidLoad {
                      
                          [super viewDidLoad];
                          _webView.navigationDelegate = self;
                      
                          _webView = [[WKWebView alloc] initWithFrame:self.view.frame];
                          _webView.frame = CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y +69, self.view.frame.size.width, self.view.frame.size.height -108);
                          [self.view addSubview:_webView];
                      }
                      
                      

                      : It doesn't work

                        • Re: WebView history
                          eskimo Apple Staff Apple Staff (6,015 points)

                          If you want a view controller to manage just a web view, it’s best to set it up in -loadView.  For example:

                          @import WebKit;
                          
                          @implementation ViewController
                          
                          - (void)loadView {
                              WKWebViewConfiguration *    config;
                              WKWebView *                webView;
                          
                              config = [[WKWebViewConfiguration alloc] init];
                              webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
                              webView.navigationDelegate = self;
                              self.view = webView;
                          }
                          
                          …
                          @end
                          

                          Share and Enjoy

                          Quinn “The Eskimo!”
                          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                          let myEmail = "eskimo" + "1" + "@apple.com"

                            • Re: WebView history
                              M.Malamas Level 1 Level 1 (0 points)

                              Thanks you very much!!

                               

                              But i have another problem with the cangoback ,cangoforward and WKBackforwardlist. I'm not able to enable the back button properly ,also the forward button. And I'm not able to set the WKBackforwardlist in a table view.

                              
                              - (void)viewDidLoad {
                                
                                  [super viewDidLoad];
                                  WKWebViewConfiguration *  config;
                                
                                  config = [[WKWebViewConfiguration alloc] init];
                                  WebView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
                                  WebView.navigationDelegate = self;
                                  WebView.UIDelegate = self;
                                  [self.view addSubview:WebView];
                                  WebView.frame = CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y +69, self.view.frame.size.width, self.view.frame.size.height -108);
                                
                                  textfield.autocapitalizationType = NO;
                                
                                  [Share setEnabled:NO];
                              
                              WKBackForwardList *list;
                              
                                  list = WebView.backForwardList;
                                  gobackbutton.enabled = WebView.canGoBack;
                                  goforwardbutton.enabled = WebView.canGoForward;
                                
                              }
                              
                                • Re: WebView history
                                  eskimo Apple Staff Apple Staff (6,015 points)

                                  I'm not able to enable the back button properly ,also the forward button.

                                  The web view’s canGoBack and canGoForward properties are observable via Key-Value Observing (KVO).  You can register an observer and have that enable and disable the relevant buttons.

                                  KVO is covered in the Key-Value Observing Programming Guide.

                                  And I'm not able to set the WKBackforwardlist in a table view.

                                  What part of that are you having problems with?

                                  Share and Enjoy

                                  Quinn “The Eskimo!”
                                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                                  let myEmail = "eskimo" + "1" + "@apple.com"

                                    • Re: WebView history
                                      M.Malamas Level 1 Level 1 (0 points)

                                      Thank you very much for you help..About the WKBackforwardlist in a table view, I have created another view controller for the bookmarks(history) and assign the bookmarks button of a toolbar to it. Should I create a subview instead of another view controller ? and I don't know how to link the tableview.dataSource with the WKBackforwardlist

                                       

                                      Thank you for your time

                                        • Re: WebView history
                                          eskimo Apple Staff Apple Staff (6,015 points)

                                          I have created another view controller for the bookmarks(history) and assign the bookmarks button of a toolbar to it. Should I create a subview instead of another view controller ?

                                          You probably want to use a view controller no matter what.  The key question is, how do you present that view controller?  And that depends on the UI you want.  For example, in Safari on iPhone the history is presented modally, whereas on iPad it’s contained with the main view (presumably via some sort of view controller containment).

                                          If you need help with these you should really ask over in the Cocoa Touch topic area; there’s nothing web view specific about them.

                                          I don't know how to link the tableview.dataSource with the WKBackforwardlist

                                          Conceptually a WKBackForwardList represents two arrays, the forward list (forwardList) and the back list (backList).  Each of these contains items of type WKBackForwardListItem that have information about that particular navigation (the title, the URL, and so on).  For history you only really care about the back list.  Presenting such an array of items in a table view is a pretty straightforward task.

                                          Share and Enjoy

                                          Quinn “The Eskimo!”
                                          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                                          let myEmail = "eskimo" + "1" + "@apple.com"

                                            • Re: WebView history
                                              M.Malamas Level 1 Level 1 (0 points)

                                              Thanks you very much for you answer !!

                                              I have one last question : I have two view controllers , one with the WKWebview and the second one with a table view . How can I call on the second view controller , the WebView.backforwardlist ?

                                                • Re: WebView history
                                                  eskimo Apple Staff Apple Staff (6,015 points)

                                                  I have two view controllers, one with the WKWebview and the second one with a table view. How can I call on the second view controller, the WebView.backforwardlist?

                                                  Normally you’d do this via dependency inject or delegation, but it’s hard to give concrete advice without knowing more about how those view controllers are related.  Are they peers?  Or is one subordinate to the other (for example, by being presented modally)?

                                                  Share and Enjoy

                                                  Quinn “The Eskimo!”
                                                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                                                  let myEmail = "eskimo" + "1" + "@apple.com"

                                                    • Re: WebView history
                                                      M.Malamas Level 1 Level 1 (0 points)

                                                      I use this to show the other view controller:

                                                      
                                                      - (IBAction)showbookmarks:(id)sender {
                                                          UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                                                          Bookmarks  *viewController = (Bookmarks *)[storyboard instantiateViewControllerWithIdentifier:@"3"];
                                                          [self presentViewController:viewController animated:YES completion:nil];
                                                      }
                                                      

                                                      and this to come back to the first :

                                                      
                                                      -(IBAction)backtoweb:(id)sender {
                                                          [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
                                                      }
                                                      
                                                        • Re: WebView history
                                                          eskimo Apple Staff Apple Staff (6,015 points)

                                                          OK, so the bookmarks view controller is presented on the web view controller.  In that case the web view controller can inject a reference to the WKBackForwardList object into the bookmarks view controller just before it presents it (betweens lines 3 and 4 in your code snippet).

                                                          Share and Enjoy

                                                          Quinn “The Eskimo!”
                                                          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                                                          let myEmail = "eskimo" + "1" + "@apple.com"

                                                            • Re: WebView history
                                                              M.Malamas Level 1 Level 1 (0 points)

                                                              Thanks you for your answer!

                                                              I do not know how to do that . I've this on my view controller web :

                                                              
                                                              @implementation ViewcontrollerWeb {
                                                                  NSArray *webhistory;
                                                              }
                                                              
                                                              - (IBAction)showbookmarks:(id)sender {
                                                                  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                                                                  Bookmarks  *viewController = (Bookmarks *)[storyboard instantiateViewControllerWithIdentifier:@"3"];
                                                                   //which code ?Please
                                                                  [self presentViewController:viewController animated:YES completion:nil];
                                                              }
                                                              
                                                              - (void)viewDidLoad {
                                                                  [super viewDidLoad];
                                                                   webhistory = [NSArray arrayWithObjects:webview.backforwardlist];
                                                              
                                                              
                                                              
                                                              
                                                              

                                                              And I want to use the array "webhistory" like this on the bookmarks view like this:

                                                              
                                                              - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
                                                              {
                                                                  return [webhistory count];
                                                              }
                                                              - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
                                                              {
                                                                  static NSString *simpleTableIdentifier = @"SimpleTableItem";
                                                              
                                                                  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
                                                              
                                                                  if (cell == nil) {
                                                                      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
                                                                  }
                                                              
                                                                  cell.textLabel.text = [webhistory objectAtIndex:indexPath.row];
                                                                  return cell;
                                                              }
                                                              
                                                              
                                                              
                                                                • Re: WebView history
                                                                  eskimo Apple Staff Apple Staff (6,015 points)

                                                                  I do not know how to do that.

                                                                  The basic idea is for the ‘parent’ view controller to give the ‘child’ view controller a reference to the model object that the child should display.  In this case you can have the child define a forwardBackList property of type WKBackForwardList, and then have the parent set that property to the web view’s forward/back list just before it presents the child.

                                                                  Share and Enjoy

                                                                  Quinn “The Eskimo!”
                                                                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                                                                  let myEmail = "eskimo" + "1" + "@apple.com"

                                                                    • Re: WebView history
                                                                      M.Malamas Level 1 Level 1 (0 points)

                                                                      Thank you for your answer! I've created a tableview on the view controller web just to see if It works.

                                                                      I use this to show the navigation history :

                                                                      @interface ViewControllerWeb () <UITableViewDelegate,UITableViewDataSource,WKNavigationDelegate,WKUIDelegate>
                                                                      @property(nonatomic,readonly,copy) NSArray<WKBackforwardlistItem*> *backlist;
                                                                      implementation Viewcontrollerweb {
                                                                           NSArray *webhistory;
                                                                      }
                                                                      -(void)viewDidload {
                                                                           [super viewDidLoad]
                                                                      webhistory = [NSArray arraywitharray:_backlist];
                                                                      
                                                                      

                                                                      and this :

                                                                      
                                                                      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
                                                                      {
                                                                          return [webhistory count];
                                                                      }
                                                                      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
                                                                      {
                                                                          static NSString *simpleTableIdentifier = @"SimpleTableItem";
                                                                        
                                                                          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
                                                                        
                                                                          if (cell == nil) {
                                                                              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
                                                                          }
                                                                        
                                                                          cell.textLabel.text = [webhistory objectAtIndex:indexPath.row];
                                                                          return cell;
                                                                      }
                                                                      
                                                                      

                                                                      But I don't see anything on the table view . Maybe my codes are wrong ?

                                                • Re: WebView history
                                                  antonifrommexico Level 1 Level 1 (0 points)

                                                  an 23, 2017 2:17 PM (in response to NotMyName) Thank you for you response . I have a problem , I cannot perform a void for the WKWebview @interface ViewControllerWeb ()   @property(strong,nonatomic) WKWebView *webView;  ....  - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {    NSLog(@"success");    }    - (void)viewDidLoad {        [super viewDidLoad];      _webView.navigationDelegate = self;        _webView = [[WKWebView alloc] initWithFrame:self.view.frame];      _webView.frame = CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y +69, self.view.frame.size.width, self.view.frame.size.height -108);      [self.view addSubview:_webView];  }  : It doesn't work

                                                    • Re: WebView history
                                                      eskimo Apple Staff Apple Staff (6,015 points)

                                                      You may have more luck attracting a response if you format your code as code.  You can do this using the <> icon in the editor.

                                                      Share and Enjoy

                                                      Quinn “The Eskimo!”
                                                      Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                                                      let myEmail = "eskimo" + "1" + "@apple.com"