Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 28th, 2012  |  syntax: None  |  size: 5.32 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. i want to save my pdf into my iphone, pdfs url are with me through parsing
  2. @implementation SecondViewController
  3.  
  4. @synthesize scrollView,receivedData;
  5.  
  6. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  7.  
  8. {  
  9.     [receivedData appendData:data];
  10. }
  11.  
  12. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  13. - (void)viewDidLoad {
  14.     [super viewDidLoad];
  15.  
  16.     [myIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
  17.     myIndicator.hidesWhenStopped = YES;
  18.     [myIndicator startAnimating];
  19.  
  20.     UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"iphone_landscape.png"]];
  21.     self.view.backgroundColor = background;
  22.     [background release];  
  23.  
  24.     NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://litofinter.es.milfoil.arvixe.com/displayxml1.aspx"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:150.0];
  25.  
  26.     NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  27.  
  28.     if (theConnection) {
  29.         receivedData = [[NSMutableData data] retain];
  30.     }
  31.  
  32. }
  33.  
  34. - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  35. {
  36.     int x=20,y=50;
  37.  
  38.     appDelegate = (AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate];
  39.  
  40.     scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 45,320, 480)];  
  41.     scrollView.contentSize = CGSizeMake(320,5000);
  42.     scrollView.showsVerticalScrollIndicator = YES;
  43.  
  44.     for (Litofinter *lito in appDelegate.bookArray) {
  45.         if([appDelegate.currentButtonPressed isEqualToString:lito.cName])
  46.         {
  47.             NSLog(@"Count == %d ===",[lito.productsArray count]);
  48.             for (Products *prod in lito.productsArray) {
  49.  
  50.                 NSString * urlString = [prod.thumbnail stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
  51.                 NSURL * imageURL = [NSURL URLWithString:urlString];
  52.  
  53.                 NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
  54.                 UIImage * image = [UIImage imageWithData:imageData];
  55.  
  56.                 [myIndicator stopAnimating];
  57.                 [myIndicator removeFromSuperview];
  58.  
  59.                 UIButton *imageButton = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
  60.                 [imageButton setFrame:CGRectMake(x, y, 150, 200)];
  61.                 [imageButton setImage:image forState:UIControlStateNormal];
  62.                 [imageButton setTitle:prod.pdf forState:UIControlStateNormal];
  63.                 [imageButton addTarget:self action:@selector(onTapBook:) forControlEvents:UIControlEventTouchUpInside];
  64.  
  65.                 [scrollView addSubview:imageButton];
  66.  
  67.                 x = x + 150;
  68.  
  69.                 if(x >300)
  70.                 {
  71.                     y = y +250;
  72.                     x = 20;
  73.  
  74.                 }
  75.             }
  76.         }
  77.     }
  78.     [self.view addSubview:scrollView];
  79.  
  80.     [connection release];
  81.     [receivedData release];
  82.  
  83.  
  84. }
  85.  
  86. -(void)onTapBook:(id)sender{
  87.  
  88.     UIButton *button = (UIButton *) sender;
  89.     appDelegate.currentBookPressed = [button currentTitle];
  90.  
  91. //  viewController2 = [[PdfShowViewController alloc]initWithNibName:@"PdfShowViewController" bundle:nil];
  92. //  [self presentModalViewController:viewController2 animated:YES];
  93.  
  94.     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Ver Catalogo!" message:@"" delegate:self cancelButtonTitle:@"Cancelar" otherButtonTitles:@"Ver on-line",@"Descargar",nil];
  95.     [alert show];
  96.  
  97.      /*[NSString stringWithFormat:@"%@",appDelegate.currentBookPressed] */
  98. }
  99.  
  100. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  101. {  
  102.  
  103.     NSString *title = [alertView buttonTitleAtIndex:buttonIndex];  
  104.  
  105.     if([title isEqualToString:@"Ver on-line"])  
  106.     {
  107.         // i will show the pdf online here
  108.  
  109.     }  
  110.     else if([title isEqualToString:@"Descargar"])  
  111.     {          
  112.  
  113.         // what to write to download the pdf
  114.     }  
  115.  
  116. }
  117.  
  118. -(IBAction)onTapBack{
  119.     [self dismissModalViewControllerAnimated:YES];
  120. }
  121.  
  122. // Override to allow orientations other than the default portrait orientation.
  123. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  124.     return YES;
  125. }
  126.  
  127.  
  128. - (void)didReceiveMemoryWarning {
  129.     [super didReceiveMemoryWarning];
  130. }
  131.  
  132. - (void)viewDidUnload {
  133.     [super viewDidUnload];
  134. }
  135.  
  136.  
  137. - (void)dealloc {
  138.     [super dealloc];
  139.     [scrollView release];
  140. }
  141.  
  142.  
  143. @end
  144.        
  145. NSData *myFile = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"your_url"]]; [myFile writeToFile:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], @"yourfilename.pdf"] atomically:YES];
  146.        
  147. ASIHTTPRequest *myDownloadRequest = [ASIHTTPRequest requestWithURL:fileUrl];
  148. [request setDownloadDestinationPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], @"yourfilename.pdf"]];
  149.        
  150. [receivedData writeToFile:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], @"yourfilename.pdf"] atomically:YES];
  151.        
  152. NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://litofinter.es.milfoil.arvixe.com/displayxml1.aspx"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:150.0];
  153.  
  154.     NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  155.  
  156.     if (theConnection) {
  157.         receivedData = [[NSMutableData data] retain];
  158.     }