我目前正在将webApp打包为应用程序。在Web应用程序中,有一个按钮,当自动点击时会询问您是否要转到存储图像的相机,选择后将为您上传图像。在手机浏览器的网络应用程序中,这可以按预期工作。
现在我已将webApp包装在UIWebView中,此功能不再有效。实际上,当选择上述任一选项时,它似乎会关闭UIWebview。有谁知道通过UIWebView使这个工作的过程是什么。
我必须得到许可吗?或者还有更多内容,例如在单击按钮时检测HTML并从UIWebView处理它。
按钮上的HTML看起来像这样。
<input type="file" required id="file" name="file" accept="image/*" multiple>
添加代码
WebViewController.h
#import <UIKit/UIKit.h>
@interface WebViewController : UIViewController <UIWebViewDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
UIViewController *viewController;
}
........
WebViewController.m
#import "WebViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "AppDelegate.h"
@end
@implementation WebViewController
- (void)viewDidLoad
{
[super viewDidLoad];
viewController = [[UIViewController alloc] init];
NSString *fullURL = self.mainURL; // get this from defualts - have removed code for this
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
[lib enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
NSLog(@"%i",[group numberOfAssets]);
} failureBlock:^(NSError *error) {
if (error.code == ALAssetsLibraryAccessUserDeniedError) {
NSLog(@"user denied access, code: %i",error.code);
}else{
NSLog(@"Other error code: %i",error.code);
}
}];
}
-(void)webViewDidStartLoad:(UIWebView *)viewWeb
{
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
- (void)webViewDidFinishLoad:(UIWebView *)viewWeb
{
}
//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 presentViewController:imagePicker animated:YES completion:nil];
}
//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:@"myApp"])
{
if ([request.URL.host isEqualToString:@"myFunction"])
{
[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 dismissViewControllerAnimated:YES completion:nil];
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imageData = UIImageJPEGRepresentation (image, 0.5);
NSURL *tmp = [NSURL URLWithString:@"ignore"];
[_viewWeb loadData:imageData MIMEType:@"image/jpeg" textEncodingName:@"UTF-8" baseURL:tmp];
}
@end
我删除了一些无关的代码
答案 0 :(得分:6)
我已经成功解决了这个问题,它又回到了iOS 8中发现的仍然没有修复的错误。它与UIWebView的呈现方式有关。
添加此代码修复了它。
-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
if ( self.presentedViewController)
{
[super dismissViewControllerAnimated:flag completion:completion];
}
}
对于任何其他困惑或挣扎于此的注意事项,您不需要开始使用UIImagePicker或UIWebView委托。您的Web视图应该打开相机库或相机。这是一个问题的真正痛苦所以希望这可以帮助我的位置上的其他人。你也可以看到THIS问题,因为这是我得到答案的地方。谢谢安德烈的帮助
答案 1 :(得分:2)
作为替代方案,您可以尝试使用UIWebViewDelegate的方法webView:shouldStartLoadWithRequest:navigationType:
来捕获自定义事件:
在您的HTML中:
<a href="myApp://myFunction">Do something</a>
<button onclick="window.location.href='myApp://myFunction'">Do some action</button>
然后:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([request.URL.scheme isEqualToString:@"myApp"]) {
if ([request.URL.host isEqualToString:@"myFunction"]) {
// do something
}
return NO;
}
return YES;
}