UIWebView delegate gets MIME type

UIWebView does not support automatic processing of Passbook.pkpass files.

In this technical note, Apple recommends introducing validation through the UIWebViewDelegate methods in order to sniff out the MIME type and process it accordingly.

To add passes using UIWebView, use the appropriate UIWebViewDelegate Methods to determine when a view loads data using the MIME application type /vnd.apple.pkpass

However, I cannot find anything in the link to the UIWebView delegate protocol , which is able to provide a MIME type.

I can successfully upload and process files directly using the delegate NSURLConnectionwithout any problems, but what I want to achieve is the correct processing of passes if the user clicks the Add to Account button while viewing in the UIWebView interface. Since I don’t know the link, and many providers do not suffix their links with the .pkpass extension, following Apple’s recommendations for learning the MIME type, it’s best to go.

I tried to add the following

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)newRequest 
                                                 navigationType:(UIWebViewNavigationType)navigationType 
{

   NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[newRequest URL]];

   // Spoof iOS Safari headers for sites that sniff the User Agent
   [req addValue:@"Mozilla/5.0 (iPhone; CPU iPhone OS 6_1 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25" forHTTPHeaderField:@"User-Agent"];

   NSURLConnection *conn = [NSURLConnection connectionWithRequest:newRequest delegate:self];

   return YES;
} 

My NSURLConnectiondelegate:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSString *mime = [response MIMEType];

    if ([mime isEqualToString:@"application/vnd.apple.pkpass"] && ![_data length]) {

        _data = nil; // clear any old data
        _data = [[NSMutableData alloc] init];

        [_webPanel stopLoading];
    }
}

-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    [_data appendData:data];
    NSLog(@"Size: %d", [_data length]);
}

-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{

    if ([_data length]) {

        PKAddPassesViewController  *pkvc = [PassKitAPI  presentPKPassFileFromData:_data];
        pkvc.delegate = self;
        [self presentViewController:pkvc
                           animated:YES
                         completion:nil];
    }
}

NSURLConnection , , UIWebView. , NSURLConnection UIWebView, , 80% .pkpass( _data Content- ).

, :

  • MIME, UIWebView Delegate?
  • , , NSURLConnection, ?
  • NSURLConnection - , ?
+5
3
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {

    NSURL *url = request.URL;
    NSURLRequest *req  = [NSURLRequest requestWithURL:url];
    NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];
    [conn start];
    return YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    NSString *mime = [response MIMEType];
    NSLog(@"%@",mime);

}
0

NSURLProtocol .

- (void)URLProtocol:(NSURLProtocol *)protocol didReceiveResponse:(NSURLResponse *)response cacheStoragePolicy:(NSURLCacheStoragePolicy)policy

, .

0

js

let contentType = webView.stringByEvaluatingJavaScript(from: "document.contentType;")
0
source

All Articles