2009-09-11 4 views

답변

0
+0

안녕하세요 qik.com 또는 ustream.com처럼 iphone에서 서버로 콘텐츠를 업로드 할 때 데몬. 따라서 앱 종료시에도 작업이 백그라운드 데몬으로 계속 남아 있습니다. 동일한 방법으로 데몬 프로세스를 구현할 수있는 방법이 있습니까? 감사 !!! – dvch

+0

이것에 대해 확실합니까? – Daniel

0

에게 heres는 당신을위한 스레드를 시작 당신은 또한있는 NSURLConnection에서 asynchronousRequest을 사용할 수 있습니다 말했다 NSThread 클래스를 heres 링크 http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/Reference/Reference.html ... 조사, 파일 업로드를위한 새로운 스레드를 시작할 수 있습니다 이런 식으로 할 수 있습니다. (기본적으로 내 프로젝트 중 하나에서 붙여 넣기입니다.) & 붙여 넣기. 신용이 개발 포럼에 일부 게시물에 간다, 그러나 더 이상부터 내가 누구인지 모르는 : 당신은 할 수 없습니다, 응용 프로그램이 실행되지 않은 상태에서

- (IBAction)startUpload:(id)sender { 
    NSString *filename = [NSString stringWithFormat:@"iphone-%d.png", [NSDate timeIntervalSinceReferenceDate]]; 

    NSString *boundary = @"----BOUNDARY_IS_I"; 

    NSURL *url = [NSURL URLWithString:@"http://yourgreatwebsite.com/upload"]; 

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; 

    [req setHTTPMethod:@"POST"]; 

    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 

    [req setValue:contentType forHTTPHeaderField:@"Content-type"]; 

    NSData *imageData = UIImagePNGRepresentation([imageView image]); 

    // adding the body 
    NSMutableData *postBody = [NSMutableData data]; 


    // first parameter an image 
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filename\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:imageData]; 

    // second parameter information 
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    //[postBody appendData:[@"Content-Disposition: form-data; name=\"some_other_name\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    //[postBody appendData:[@"some_other_value" dataUsingEncoding:NSUTF8StringEncoding]]; 
    //[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r \n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    [req setHTTPBody:postBody]; 

    //start the spinner and deactivate the buttons... 
    [self setButtonsEnabled:NO]; 


    [[NSURLConnection alloc] initWithRequest:req delegate:self]; 
    } 

    #pragma mark urlconnection delegate methods 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    // this method is called when the server has determined that it 
    // has enough information to create the NSURLResponse 

    // it can be called multiple times, for example in the case of a 
    // redirect, so each time we reset the data. 
    // receivedData is declared as a method instance elsewhere 
    [receivedData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    // append the new data to the receivedData 
    // receivedData is declared as a method instance elsewhere 
    [receivedData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection 
    didFailWithError:(NSError *)error 
{ 
    // release the connection, and the data object 
    [connection release]; 

    // inform the user 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Upload Error" message:[NSString stringWithFormat:@"The upload failed with this error: %@", [error localizedDescription]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 

    NSLog(@"Connection failed! Error - %@ %@", 
      [error localizedDescription], 
      [[error userInfo] objectForKey:NSErrorFailingURLStringKey]); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    // do something with the data 
    // receivedData is declared as a method instance elsewhere 
#ifdef DEBUG 
    NSLog(@"upload succeeded!"); 
#endif 

    // release the connection, and the data object 
    [connection release]; 

    NSString *response = [NSString stringWithCString:[receivedData bytes] length:[receivedData length]];  


#ifdef DEBUG 
    NSLog(response); 
#endif 
} 
0

당신이 배경 업로드로 다스 려하는 경우 (OS 아무튼 그것을 허용하지 않음). 앱이 실행되는 동안 배경 화면이라면 여기에 게시 된 링크와 샘플 코드는 정상적으로 작동합니다.

2

"백그라운드에서"의미하는 것이 명확하지 않지만 비동기 적으로 업로드하려는 경우에는 NSURLConnection, NSURLRequest를 사용하거나 ASIHTTPRequest이라는 훌륭한 라이브러리를 사용할 수 있습니다. 그것은 잘 작동하고 다운로드 및 업로드 진행을 보여주는 간단한 방법을 제공합니다.

+0

+1 멋진 프레임 워크처럼 보입니다! –

0

this post (매우 잘 검토 된) 응답을 참조하십시오.

앱이 실행되고 있지 않을 때 백그라운드에서 파일을 업로드 할 수는 없지만 앱이 실행 중일 때는 완전히 수행 할 수 있습니다. 이런 식으로 전경 스레드에 영향을 미치지 않고 진행 상황 등을 표시 할 가능성을 높일 수 있습니다.

관련 문제