Receiving Passbook's .pkpass from URL without Webview

痞子三分冷 提交于 2019-12-23 12:30:46

问题


Hi first of all i have to confess i really don't understand how the whole Passbook topic really works. So here's my situation: I have a backend system which creates .pkpass files stores them and creates an URL. When i open this URL in my browser it directly starts to download the pass file. How can i receive or open this file with my ios application?

Thanks in advance.


回答1:


You can use webservices to get pass data. Webservices can send your pass data in base64(NSString) format and you decode it to get NSData.

The use the data to initiate PKPass Object.

PKPass *pass = [[PKPass alloc] initWithData:passData error:&err];

Once you get PKPass you can use PKAddPassesViewController to show it inside the app. You can find detailed explanation here.

Note : you can directly download Pass data from the URL using NSUrlConnection and use the downloaded data to create PKPass.




回答2:


another way..simpler and can be used in mobile web apps as well. write a simple web service to return this pass download URL then parse the json/xml(depends on type of response) in your app(web or native ios) and then invoke the URL in safari.

from native app call [[UIApplication sharedApplication] openURL:passdownloadURL];

I have used it in my project and it works awesome!!




回答3:


If you host the link to the PKPass item on a website you can check that the page content is really a PKPass item by looking at the mimeType property on HTTPURLResponse, which should be "application/vnd.apple.pkpass". Once you confirm that, download the .pkpass data with an URLRequest and pass the data to the PKPass initializer like this:

var error:NSError?
let passBookData = PKPass( data: data, error: &error )

if error != nil
{
    print( "Error opening pkpass file" )
    return
}
let pkPassVC = PKAddPassesViewController( pass: passBookData )
currentVC.present( pkPassVC, animated: true, completion: completion )


来源:https://stackoverflow.com/questions/16817346/receiving-passbooks-pkpass-from-url-without-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!