Note that there are some explanatory texts on larger screens.

plurals
  1. POUIWebView Delegate get MIME Type
    primarykey
    data
    text
    <p>The UIWebView does not automatically support processing of Passbook .pkpass files.</p> <p>In this <a href="http://developer.apple.com/library/ios/#technotes/tn2302/_index.html" rel="noreferrer">technical note</a>, Apple recommend implementing a check via the UIWebViewDelegate methods to sniff out the MIME type and process it accordingly.</p> <blockquote> <p><em>To add passes using a UIWebView, implement the appropriate UIWebViewDelegate methods to identify when the view loads data with a MIME type of application/vnd.apple.pkpass</em></p> </blockquote> <p>However, I cannot find anything within the <a href="http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIWebViewDelegate_Protocol/Reference/Reference.html" rel="noreferrer">UIWebView Delegate Protocol Reference</a> that is capable of providing the MIME type.</p> <p>I can successfully download and process files directly using an <code>NSURLConnection</code> delegate with no problem, but what I wish to achieve is for passes to be properly processed if a user clicks on an Add To Passbook button while browsing within a UIWebView. Since I do not know the link, and many providers do not suffix their links with a .pkpass extension, following Apple's advice of examining the MIME type seems the best way to go.</p> <p>I have tried adding the following </p> <pre><code>- (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; } </code></pre> <p>My <code>NSURLConnection</code> delegate:</p> <pre><code>- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSString *mime = [response MIMEType]; if ([mime isEqualToString:@"application/vnd.apple.pkpass"] &amp;&amp; ![_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]; } } </code></pre> <p>The <code>NSURLConnection</code> delegates work fine when a connection is invoked directly, without the <code>UIWebView</code>. However, when I try launching an <code>NSURLConnection</code> from the <code>UIWebView</code> delegate the pass download fails because the only 80% or so of the .pkpass is being downloaded (I get a random mismatch of bytes in the _data variable and the Content-Length header).</p> <p>So, my questions:</p> <ol> <li>Is there an easier way to get hold of a <code>MIME</code> type, directly from the <code>UIWebView</code> Delegate methods?</li> <li>If not, then am I going about this the right way with opening up a parallel NSURLConnection, or is there a better way?</li> <li>If an NSURLConnection is the way to go, then what could be causing it to stop short of downloading the full file? </li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload