我正在尝试在后台运行 -getHTMLupdate 但它不起作用。
-viewDidLoad
- (void)viewDidLoad {
UIImage *navBarImage = [[UIImage imageNamed:@"menubar.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 15, 5, 15)];//Navbar
[[UINavigationBar appearance] setBackgroundImage:navBarImage forBarMetrics:UIBarMetricsDefault];
[self performSelectorInBackground:@selector(getHTMLupdate) withObject:nil];
}
这是-getHTMLupdate
-(void) getHTMLupdate {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
/* set headers, etc. on request if needed */
[request setURL:[NSURL URLWithString:@"http://appstarme.com/GD/GDHTMLPARSINGINFORMATION.php"]];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSScanner *scanner = [NSScanner scannerWithString:html];
NSString *token = nil;
[scanner scanUpToString:@"<h1>" intoString:NULL];
[scanner scanUpToString:@"</h1>" intoString:&token];
NSLog(@"DATA : %@", html);
_updateFromUser.text = html;
这是控制台输出
2013-07-02 20:26:25.775 Social App[8130:3707] DATA :
Please check out the new Skype contest at www.Skype.com
2013-07-02 20:26:25.776 Social App[8130:3707] bool _WebTryThreadLock(bool), 0x1e8ab970: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
1 0x3a021259 WebThreadLock
2 0x36083185 <redacted>
3 0xc75fd -[homeChannel getHTMLupdate]
4 0x349e7231 <redacted>
5 0x3c2170e1 <redacted>
6 0x3c216fa8 thread_start
NSLog 奇怪地返回正确的数据,但应用程序仍然崩溃
编辑
在代码末尾添加了这个
[_updateFromUser performSelectorOnMainThread: @selector(setText:)
withObject: html
waitUntilDone: FALSE];
完美地工作。