2009-09-11 61 views

回答

0

你可以開始爲你的文件上傳一個新的線程,查找到NSThread類繼承人鏈接http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/Reference/Reference.html ......那說你也可以使用asynchronousRequest從NSURLConnection的該繼承人啓動一個線程爲大家做參考http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html

+0

嗨 像qik.com或ustream.com一樣,當他們從iPhone上傳內容到服務器時,它可以通過守護進程。因此,即使退出應用程序時,該任務仍在使用後臺守護進程。有什麼方法可以用同樣的方式實現守護進程?謝謝 !!! – dvch 2009-09-11 20:43:59

+0

你確定嗎? – Daniel 2009-09-12 04:51:37

0

你可以這樣做(這基本上是從我的一個項目中刪除&粘貼)。幸得在發展論壇的一些帖子,但我不知道這是誰的了:

- (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,看起來像一個真棒框架! – 2009-09-11 21:05:54

0

請參閱this post一個(非常深思熟慮的)迴應。

雖然無法在應用程序未運行時在後臺上傳文件,但在應用程序運行時完全可以這樣做。這樣你不會影響你的前景線程,再加上你可能會增加這個來顯示進度等。