简体   繁体   中英

Open URL with UIButton

I hope You are well, I've been working a few weeks on iphone and at the moment I have a problem try to open a safari with a url, I have a json file that come a url which is dinamic.

here I leave the code.

- (void) loadFiles {

NSArray *_json = [[[self getDataFromJson] objectAtIndex:0] retain];
if ([_json count] > 0)
{
 for (int i = 0; i < [_json count]; i++)
 {
  NSDictionary *file = [_json objectAtIndex:i];

  UIImage *buttonImage = [UIImage imageNamed:@"btn.png"];
  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  [button setImage:buttonImage forState:UIControlStateNormal];

  [button addTarget:self action:@selector(openBrowser:) forControlEvents:UIControlEventTouchUpInside];

  //works but i have warnings
  button.tag = [file objectForKey:@"linkURL"] ;
  CGRect frame = CGRectZero;
  frame.size = buttonImage.size;
  button.frame = frame;

  NSString *name = [file objectForKey:@"name"];
  NSString *description = [file objectForKey:@"description"];


            //Create Box 

 }
}
}

- (void) openBrowser:(id)sender
{ 
 NSString *url = ((UIControl *) sender).tag;
 [[UIApplication sharedApplication]  openURL:[NSURL URLWithString:url] ];
}

I need to open from the UIButtom an URL without complaint . Any suggestion or help thank you. Cheers

Subclass UIButton and add a property to store the URL.

// MyButton.h
@interface MyButton : UIButton {
    NSString *urlString_;
}

@property (nonatomic, retain) NSString *urlString;

@end



// MyButton.m
#import "MyButton.h"

@implementation MyButton

@synthesize urlString = urlString_;

- (void)dealloc {
    [self setUrlString:nil];
    [super dealloc];
}

@end

and then you continue with your code:

#import "MyButton.h"
.
.
.
- (void) loadFiles {

    NSArray *_json = [[[self getDataFromJson] objectAtIndex:0] retain];
    if ([_json count] > 0)
    {
        for (int i = 0; i < [_json count]; i++)
        {
            NSDictionary *file = [_json objectAtIndex:i];

            UIImage *buttonImage = [UIImage imageNamed:@"btn.png"];
            MyButton *button = [MyButton buttonWithType:UIButtonTypeCustom];
            [button setImage:buttonImage forState:UIControlStateNormal];

            [button addTarget:self action:@selector(openBrowser:) forControlEvents:UIControlEventTouchUpInside];

            //works but i have warnings
            button.urlString = [file objectForKey:@"linkURL"] ;
            CGRect frame = CGRectZero;
            frame.size = buttonImage.size;
            button.frame = frame;

            NSString *name = [file objectForKey:@"name"];
            NSString *description = [file objectForKey:@"description"];


            //Create Box 

        }
    }
}

- (void) openBrowser:(id)sender {
    if ([sender isKindOfClass:[MyButton class]]) {
        [UIApplication sharedApplication] openURL:[NSURL URLWithString:[[(MyButton *)sender urlString]]];
    }
}

tag is an int type. So you cannot store NSString URL here. You need to store URLs in some other place, may be in a NSMutableArray. Store index i in button's tag and store URL in a NSMutableArray in position i. Then in button handler use the tag as the index to get URL from that NSMutableArray.

And not related with the question, you are not releasing _json NSArray. So you are leaking memory. And also you don't need to test if ([_json count] > 0) . Your for loop is already handling that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM