2010-05-26 8 views

답변

12

UIWebView Delegate를 구현하고 시도한 페이지로드를 차단할 수 있습니다. 사용자 정의 URL을 설정하면 카메라를 시작할 수있는 Objective C에 메시지를 전달할 수 있습니다. 데이터를 웹에 다시 보내려면 내 예제에서와 같이 새로드를 초기화하거나 UIWebView에서 다른 메서드를 사용하여 일부 자바 스크립트를 전달할 수 있습니다. 여기

가 작동 예를 들어, 난 그냥 쓴 :

#import "WebViewCamAppDelegate.h" 
    #import <UIKit/UIKit.h> 
    @interface uiwebviewcameraAppDelegate : NSObject <UIApplicationDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIWebViewDelegate> { 
     UIWindow *window; 
    UIViewController *viewController; 
    UIWebView *webView; 
    } 

    @property (nonatomic, retain) IBOutlet UIWindow *window; 

    @end 


    @implementation uiwebviewcameraAppDelegate 

    @synthesize window; 

    //This is the HTML we initially show in the WebView. Note the url "showcamera:" is one I 
    //invented with the intent to intercept it to show the camera. 
    static NSString *htmlString = @"<br><A href=\"showcamera:\">Show Camera</a>"; 


    //Pretty Basic stuff. We set the UIWebView Delegate so we can intercept the call and set up  //a ViewController so we can animate the UIImagePicker 
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
    viewController = [[UIViewController alloc] init]; 

    webView = [[UIWebView alloc] initWithFrame:window.bounds]; 
    [webView loadHTMLString:htmlString baseURL:nil]; 
    webView.delegate = self; 

    [viewController.view addSubview:webView]; 
    [window addSubview:viewController.view]; 
     [window makeKeyAndVisible]; 
    return YES; 
    } 

    //Note: I check to make sure the camera is available. If it is not (iPod touch or Simulator) I show the photo library instead. 
    -(void) showCamera { 
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
     imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; 
    } 
    else { 
     imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    } 
    imagePicker.delegate = self; 
    [viewController presentModalViewController:imagePicker animated:YES]; 
    } 

    //Here we intercept any time the webview tries to load a document. When the user hits our "showcamera: url" we go to work. 
    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { 
    if ([[[request URL] scheme] isEqualToString:@"showcamera"]) { 
     [self showCamera]; 
     return NO; 
    } 
    return YES; 
    } 

    //After the imagepicker has done it's thing, we pass the data back to the webview to be displayed. 
    //If we wanted to be fancy here, we could have done this via Javascript so we could dynamically insert an image without reloading the page. 
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    [viewController dismissModalViewControllerAnimated:YES]; 
    UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    NSData *imageData = UIImageJPEGRepresentation (image, 0.5); 
    [webView loadData:imageData MIMEType:@"image/jpeg" textEncodingName:@"UTF-8" baseURL:nil]; 
    } 


    - (void)dealloc { 
    [viewController release]; 
    [webView release]; 
     [window release]; 
     [super dealloc]; 
    } 

    @end 
+0

안녕 브래드, 앱 위임에서이 작업을 수행하거나 않아도 어디서 당신은 당신의 웹보기를 설정 이제까지? – Rob85

관련 문제