Eigenen HTML Code in WebView ausführen (mit NSString möglich?)

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Eigenen HTML Code in WebView ausführen (mit NSString möglich?)

    Hallo liebe Community,
    ich habe eine kleine Frage:
    Mein Programm zieht aus einer Seite den Quellcode, welchen es benötigt und lädt diesen herunter, (selbstständig, da der nette Ersteller Kommentare hinterlassen hat ^^)
    schreibt ihn in einen aus einem NSArray in einen NSString und dann.. -
    Stehe ich vor einem Problem.
    Ich versuche seit geraumer Zeit den Quellcode, welchen ich mir mehr oder weniger geklaut habe in einer WebView anzeigen zu lassen.
    Ich habe aber keine Ahnung wie. Geht das überhaupt?
    Wenn ja, wie?
    Wäre super wenn ihr mir helfen könntet. Ich bin nur Hobbyprogrammierer und meine Fähigkeiten sind leider etwas beschränkt :/
    Wenn ihr irgendeinen Code braucht einfach fragen,
    ich wüsste jetzt nicht was ihr brauchen könntet...

    ( Ich habe schon eine funktionierende WebView, kann damit auch Internetseiten öffnen.)


    Sorry für meine dämliche Frage ^^
  • joluc schrieb:

    Hallo liebe Community,
    ich habe eine kleine Frage:
    Mein Programm zieht aus einer Seite den Quellcode, welchen es benötigt und lädt diesen herunter, (selbstständig, da der nette Ersteller Kommentare hinterlassen hat ^^)
    schreibt ihn in einen aus einem NSArray in einen NSString und dann.. -
    Stehe ich vor einem Problem.
    Ich versuche seit geraumer Zeit den Quellcode, welchen ich mir mehr oder weniger geklaut habe in einer WebView anzeigen zu lassen.
    Meine Glaskugel ist beschlagen. ;) Zeig den Code mal her.
  • Aber nicht lachen :(

    AppDelegate.m

    Quellcode

    1. #import "AppDelegate.h"
    2. @interface AppDelegate ()
    3. @property (weak) IBOutlet NSWindow *window;
    4. @end
    5. @implementation AppDelegate
    6. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    7. // Insert code here to initialize your application
    8. // NSString in eine NSURL umwandeln
    9. //TEST TEST TEST
    10. response.isVisible = NO;
    11. searchwindow.isVisible = YES;
    12. NSLog(@"Starting up.");
    13. }
    14. -(void)call1
    15. {
    16. //Los gehts :)
    17. //Hat ziemlich lang gedauert dass zu machen.
    18. //Großen Dank an den Betreiber!
    19. filter1 = (@"<!--<br /><strong>Aufstellung aller Formen:</strong><br /><br />-->");
    20. vorhanden1 = NO;
    21. vorhanden2 = NO;
    22. go1 = NO;
    23. NSLog(@"Let's go!");
    24. //BAUE URL
    25. NSString *urlbauer;
    26. NSLog(@"Building URL!");
    27. urlbauer = (@"http://www.frag-caesar.de/lateinwoerterbuch/");
    28. urlbauer = [urlbauer stringByAppendingString:eingabe1.stringValue];
    29. urlbauer = [urlbauer stringByAppendingString:(@"-uebersetzung-1.html")];
    30. NSURL *url21 = [NSURL URLWithString:urlbauer];
    31. NSLog(@"URL READY!");
    32. //FERTIG
    33. NSLog(@"Downloading...");
    34. // Die Daten aus der Text-Datei abrufen
    35. NSData *data21 = [NSData dataWithContentsOfURL:url21];
    36. NSLog(@"Connecting... (GET)");
    37. // Die Daten aus der Textdatei in einen NSString umwandeln
    38. NSString* erlaubnis = [[NSString alloc] initWithData:data21
    39. encoding:NSUTF8StringEncoding];
    40. NSArray *textLines21= [erlaubnis componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    41. NSLog(@"Download complete.");
    42. NSLog(@"Formatting...");
    43. NSString *myContent = [[NSString alloc] initWithData:data21 encoding:NSUTF8StringEncoding];
    44. // KEYWORDS
    45. // <!--<br /><strong>Aufstellung aller Formen:</strong><br /><br />-->
    46. // Aufstellung aller Formen
    47. // STARTE KEY SUCHE
    48. //SUCHE START KEY
    49. for(int i = 0; i < [textLines21 count]; i++)
    50. {
    51. loop1int = i;
    52. loop1string = (@"%@",[textLines21 objectAtIndex:i]);
    53. if ([loop1string containsString:(@" <div id=\"header-latin\">")])
    54. {
    55. line1 = i;
    56. vorhanden1 = YES;
    57. }
    58. }
    59. //\
    60. NSString *loop2string;
    61. //SUCHE ENDE
    62. for(int i = line1; i < [textLines21 count]; i++)
    63. {
    64. loop1int = i;
    65. loop2string = (@"%@",[textLines21 objectAtIndex:i]);
    66. if ([loop2string containsString:(@"<!--<br /><strong>Aufstellung aller Formen:</strong><br /><br />-->")])
    67. {
    68. line2 = i;
    69. vorhanden2 = YES;
    70. }
    71. }
    72. // ENDE DES SUCHENS VON KEYS
    73. vorhanden1 = YES;
    74. vorhanden2 = YES;
    75. // line1 = 85;
    76. // line2 = 979;
    77. //
    78. if (vorhanden1 == NO)
    79. {
    80. NSLog(@"Sorry, the Word you requested can not be filtered.");
    81. }
    82. if (vorhanden2 == NO)
    83. {
    84. NSLog(@"Sorry, the Word you requested can not be filtered.");
    85. }
    86. //\
    87. NSLog(@"Der benötigte Code befindet sich auf Linie %d bis zu Linie %d. Er wird nun gefiltert...",line1,line2);
    88. kompatibelkom = NO;
    89. int durchlauf;
    90. durchlauf = line1;
    91. int zutun;
    92. zutun = line2;
    93. int br1;
    94. //84 114
    95. //Formatierung des komplimentären Strings
    96. NSLog(@"Formatiere Komplimentären String um");
    97. // myContent = [myContent stringByReplacingOccurrencesOfString:(@"") withString:(@"`")];
    98. NSLog(@"Der komplimentäre String wurde umgeformt");
    99. NSArray *komarray1 = [myContent componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    100. htmlcode = (@"-");
    101. for(int i=line1; i<line2; i++)
    102. {
    103. durchlauf++;
    104. NSLog(@"Filtere Zeile %d von %d",i,line2-1);
    105. htmlcode = [htmlcode stringByAppendingString:(@"%@",[textLines21 objectAtIndex:durchlauf])];
    106. }
    107. //HTML CODE SPÄTER IN WEBVIEW AUSFÜHREN!
    108. ausgabe1.stringValue = htmlcode;
    109. eingabe1.stringValue = (@"");
    110. response.isVisible = YES;
    111. // wv = [[WebView alloc] init];
    112. //
    113. // [wv loadHTMLString:htmlcode baseURL:nil];
    114. if (kompatibelkom == YES)
    115. {
    116. NSLog(@"Great!");
    117. }
    118. }
    119. -(IBAction)sucher1:(id)sender1
    120. {
    121. NSLog(@"Starting searching routine!");
    122. [self call1];
    123. }
    124. - (void)applicationWillTerminate:(NSNotification *)aNotification {
    125. // Insert code here to tear down your application
    126. }
    127. @end
    Alles anzeigen
  • Sehr dämliche frage, aber wie mache ich das? Bei mir funktioniert das nicht wirklich...
    AppDelegate.h

    Quellcode

    1. //
    2. // AppDelegate.h
    3. // Transl
    4. //
    5. // Created by Jonathan Lucas Fritz on 06.05.15.
    6. // Copyright (c) 2015 Noscio. All rights reserved.
    7. //
    8. #import <Cocoa/Cocoa.h>
    9. #import <WebKit/WebKit.h>
    10. @interface AppDelegate : NSObject <NSApplicationDelegate>
    11. {
    12. BOOL *vorhanden1;
    13. BOOL *vorhanden2;
    14. BOOL *go1;
    15. BOOL *gefunden1;
    16. BOOL *kompatibelkom;
    17. IBOutlet WebView *wv;
    18. IBOutlet NSTextField *ausgabe1;
    19. IBOutlet NSTextField *eingabe1;
    20. IBOutlet NSButton *search;
    21. IBOutlet NSWindow *searchwindow;
    22. IBOutlet NSWindow *response;
    23. IBOutlet NSWindow *WebFenster1;
    24. int line1;
    25. int line2;
    26. int loop1int;
    27. NSString *loop1string;
    28. int *anzahlobjekte1;
    29. int *anzahlobjekte2;
    30. NSString *filter1;
    31. NSString *filter2;
    32. NSString *htmlcode;
    33. }
    34. @property (nonatomic, readwrite, strong) IBOutlet WebView *wv;
    35. -(IBAction)sucher1:(id)sender1;
    36. -(IBAction)sender1:(id)sender2;
    37. @end
    Alles anzeigen

    AppDelegate.m

    Quellcode

    1. //
    2. // AppDelegate.m
    3. // Transl
    4. //
    5. // Created by Jonathan Lucas Fritz on 06.05.15.
    6. // Copyright (c) 2015 Noscio. All rights reserved.
    7. //
    8. #import "AppDelegate.h"
    9. @interface AppDelegate ()
    10. @property (weak) IBOutlet NSWindow *window;
    11. @end
    12. @implementation AppDelegate
    13. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    14. // Insert code here to initialize your application
    15. // NSString in eine NSURL umwandeln
    16. //TEST TEST TEST
    17. response.isVisible = NO;
    18. searchwindow.isVisible = YES;
    19. NSLog(@"Starting up.");
    20. }
    21. -(void)call1
    22. {
    23. //Los gehts :)
    24. //Hat ziemlich lang gedauert dass zu machen.
    25. //Großen Dank an den Betreiber!
    26. filter1 = (@"<!--<br /><strong>Aufstellung aller Formen:</strong><br /><br />-->");
    27. vorhanden1 = NO;
    28. vorhanden2 = NO;
    29. go1 = NO;
    30. NSLog(@"Let's go!");
    31. //BAUE URL
    32. NSString *urlbauer;
    33. NSLog(@"Building URL!");
    34. urlbauer = (@"http://www.frag-caesar.de/lateinwoerterbuch/");
    35. urlbauer = [urlbauer stringByAppendingString:eingabe1.stringValue];
    36. urlbauer = [urlbauer stringByAppendingString:(@"-uebersetzung-1.html")];
    37. NSURL *url21 = [NSURL URLWithString:urlbauer];
    38. NSLog(@"URL READY!");
    39. //FERTIG
    40. NSLog(@"Downloading...");
    41. // Die Daten aus der Text-Datei abrufen
    42. NSData *data21 = [NSData dataWithContentsOfURL:url21];
    43. NSLog(@"Connecting... (GET)");
    44. // Die Daten aus der Textdatei in einen NSString umwandeln
    45. NSString* erlaubnis = [[NSString alloc] initWithData:data21
    46. encoding:NSUTF8StringEncoding];
    47. NSArray *textLines21= [erlaubnis componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    48. NSLog(@"Download complete.");
    49. NSLog(@"Formatting...");
    50. NSString *myContent = [[NSString alloc] initWithData:data21 encoding:NSUTF8StringEncoding];
    51. // KEYWORDS
    52. // <!--<br /><strong>Aufstellung aller Formen:</strong><br /><br />-->
    53. // Aufstellung aller Formen
    54. // STARTE KEY SUCHE
    55. //SUCHE START KEY
    56. for(int i = 0; i < [textLines21 count]; i++)
    57. {
    58. loop1int = i;
    59. loop1string = (@"%@",[textLines21 objectAtIndex:i]);
    60. if ([loop1string containsString:(@" <div id=\"header-latin\">")])
    61. {
    62. line1 = i;
    63. vorhanden1 = YES;
    64. }
    65. }
    66. //\
    67. NSString *loop2string;
    68. //SUCHE ENDE
    69. for(int i = line1; i < [textLines21 count]; i++)
    70. {
    71. loop1int = i;
    72. loop2string = (@"%@",[textLines21 objectAtIndex:i]);
    73. if ([loop2string containsString:(@"<!--<br /><strong>Aufstellung aller Formen:</strong><br /><br />-->")])
    74. {
    75. line2 = i;
    76. vorhanden2 = YES;
    77. }
    78. }
    79. // ENDE DES SUCHENS VON KEYS
    80. vorhanden1 = YES;
    81. vorhanden2 = YES;
    82. // line1 = 85;
    83. // line2 = 979;
    84. //
    85. if (vorhanden1 == NO)
    86. {
    87. NSLog(@"Sorry, the Word you requested can not be filtered.");
    88. }
    89. if (vorhanden2 == NO)
    90. {
    91. NSLog(@"Sorry, the Word you requested can not be filtered.");
    92. }
    93. //\
    94. NSLog(@"Der benötigte Code befindet sich auf Linie %d bis zu Linie %d. Er wird nun gefiltert...",line1,line2);
    95. kompatibelkom = NO;
    96. int durchlauf;
    97. durchlauf = line1;
    98. int zutun;
    99. zutun = line2;
    100. int br1;
    101. //84 114
    102. //Formatierung des komplimentären Strings
    103. NSLog(@"Formatiere Komplimentären String um");
    104. // myContent = [myContent stringByReplacingOccurrencesOfString:(@"") withString:(@"`")];
    105. NSLog(@"Der komplimentäre String wurde umgeformt");
    106. NSArray *komarray1 = [myContent componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    107. htmlcode = (@"-");
    108. for(int i=line1; i<line2; i++)
    109. {
    110. durchlauf++;
    111. NSLog(@"Filtere Zeile %d von %d",i,line2-1);
    112. htmlcode = [htmlcode stringByAppendingString:(@"%@",[textLines21 objectAtIndex:durchlauf])];
    113. }
    114. //HTML CODE SPÄTER IN WEBVIEW AUSFÜHREN!
    115. ausgabe1.stringValue = htmlcode;
    116. eingabe1.stringValue = (@"");
    117. response.isVisible = YES;
    118. if (kompatibelkom == YES)
    119. {
    120. NSLog(@"Great!");
    121. }
    122. WebFenster1.isVisible = YES;
    123. [self loadHTMLString:htmlcode baseURL:nil];
    124. }
    125. -(IBAction)sucher1:(id)sender1
    126. {
    127. NSLog(@"Starting searching routine!");
    128. [self call1];
    129. }
    130. - (void)loadHTMLString:(NSString *)string
    131. baseURL:(NSURL *)URL{
    132. }
    133. - (void)applicationWillTerminate:(NSNotification *)aNotification {
    134. // Insert code here to tear down your application
    135. }
    136. @end
    Alles anzeigen
    Ich stelle mich bestimmt extrem blöd an, bitte verzeiht mir das.
  • ich hab das jetzt nur einmal überflogen

    du machst

    Quellcode

    1. [self loadHTMLString:htmlcode baseURL:nil];

    und deine Methode sieht so aus

    Quellcode

    1. - (void)loadHTMLString:(NSString *)string
    2. baseURL:(NSURL *)URL{
    3. }
    das ist doch ne Methode vom WebView, die musst du doch nicht implementieren und schon gar nicht leer wenn du sie aufrufst

    das solltest du eher deinem WebView schicken

    Quellcode

    1. [self.wv loadHTMLString:htmlcode baseURL:nil];
    müsste es glaub ich sein, so wie ich das in deinem Code gesehen habe
    Ich weiß nicht immer wovon ich rede aber ich weiß das ich Recht habe. :saint:
  • ich entwickel nicht für den mac

    aber ein ganz kurzer Blick in die Doku und der Link von Gritsch zeigen das du wohl eher WebFrame und WebFrameView brauchst

    ungetestet und Doku nicht komplett gelesen, aber das kannst du ja bestimmt selber lesen
    Ich weiß nicht immer wovon ich rede aber ich weiß das ich Recht habe. :saint: