Random websites button

后端 未结 1 1547
鱼传尺愫
鱼传尺愫 2021-01-29 16:16

this one is pretty simple I guess but I just can\'t find the answer that would suit perfectly for my issue. I want to make a button that opens a random URL from a list I\'ll giv

相关标签:
1条回答
  • 2021-01-29 16:35

    Like Popeye said, you can store the URLs into a NSArray and pick one of them randomly:

    #include <stdlib.h>
    
    - (IBAction)site:(id)sender {
        NSArray *urls = @[
            [NSURL URLWithString:@"http://www.google.com"],
            [NSURL URLWithString:@"http://www.facebook.com"],
            [NSURL URLWithString:@"http://www.twitter.com"]
        ];
    
        int index = arc4random_uniform(urls.count);
        NSURL *randomURL = urls[index];
    
        if ([[UIApplication sharedApplication] canOpenURL:randomURL])
            [[UIApplication sharedApplication] openURL:randomURL];
    }
    
    0 讨论(0)
提交回复
热议问题