1

I am creating a webview that will contain different url's. So it will be random when you go to the webview. But i cant figure out how to do this. I looked at another question, and got it figured out kinda of. . This is the question i looked at, Random websites button I got it working in a webview, instead of safari. But i can't get the webview to work on another view controller now. It only works on the first view controller. I just need to get the webview to work on the other view cotroller. Not for sure if i need a global variable or what. Here is my code, so you can understand what i'm working with.

- (IBAction)site:(id)sender {
    NSArray *urls = @[
                      [NSURL URLWithString:@"http://www.youtube.com"],
                      [NSURL URLWithString:@"http://www.twitter.com"],
                      [NSURL URLWithString:@"http;//www.yahoo.com"]
                      ];

    int index = arc4random_uniform(urls.count);
    NSURL *randomURL = urls[index];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: randomURL];
    [self.webView loadRequest:request];

please help, thanks!

Community
  • 1
  • 1
asdf
  • 11
  • 2

1 Answers1

0
int *randomURL;
        NSString *generatedURL;
        NSArray *urls;
        urls =@[@"http://www.youtube.com",
                @"http://www.twitter.com",
                @"http://www.yahoo.com"];
        randomURL =arc4random()%[urls count];
        generatedURL = [urls objectAtIndex:randomURL];
        NSLog(@"Generated URL = %@", generatedURL);
        NSURL *url = [NSURL URLWithString:generatedURL];
        NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
        [webView loadRequest:requestURL];

You should place the code above in the viewDidLoad method

Dinesh Sekar
  • 91
  • 1
  • 1
  • 8