In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces what the iOS SDK integration method has related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have something to gain after reading this iOS SDK integration method, let's take a look at it.
# CocoaPods Integration
1. Install CocoaPods
The installation method is simple. Ruby is included in Mac. You can download and install it by using the gem command of ruby:
$sudo gem install cocoapods
$pod setup
two。 Prepare the Podfile file
Under our project directory, create a new file named Podfile in the following format, and list the names of dependent libraries in the file in turn:
The author uses the standard version here:
Target 'GeTuipush' do
Platform: ios
Pod 'GTSDK'
End
Target 'NotificationService' do
Platform: ios
Pod 'GTExtensionSDK'
End
3. Complete GTSDK import
Execute the following command in the project root directory:
$pod install
After the execution is complete, the project directory structure is shown in the following figure:
Note: before pod install, your project must be created, and if there is a target:NotificationService in the Podfile file, you need to create a notification extension Target before pod install.
4. Enable the push function: since it is a push, of course it is necessary to turn on the push function! :
5. Backend operation permission setting: it is said on the official website of a push that it is to better support message push, provide more push styles, and improve message arrival rate. Since this is said, it doesn't matter whether it is enabled first, as shown in the following figure:
6.XCode10 recommends enabling WiFi information authorization: above Xcode 10.x, find Capabilities-> Access WiFi Information in the application Target settings, and confirm that the switch has been set to ON. As shown in the following figure:
Note: both the main Target and the Target of the notification extension need to be opened
7. The code part, here is our programmer's favorite part, paste and copy. Since this is the first time to integrate a push SDK code, I still studied it carefully.
# initialize SDK to register APNs and get CID
1. Add callback API class to AppDelegate:
# import
# import
/ / UserNotifications.framework is required for iOS10 and above
# if _ _ IPHONE_OS_VERSION_MAX_ALLOWED > = _ _ IPHONE_10_0
# import
# endif
@ interface AppDelegate: UIResponder
@ property (strong, nonatomic) UIWindow * window
@ end
two。 Initialize SDK and register APNs:
# import "AppDelegate.h"
/ / when you apply for App on the sender's website, the registered AppId, AppKey, AppSecret
# define kGtAppId @ "GVZZTqh7lu6S4VLMacneZ7"
# define kGtAppKey @ "RRYDFjGzO17TJXZfGeTuq3"
# define kGtAppSecret @ "7BXDJ0IgWF6a8M0xCgo4G"
@ interface AppDelegate ()
@ end
@ implementation AppDelegate
-(BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *) launchOptions {
/ / Override point for customization after application launch.
[GeTuiSdk startSdkWithAppId:kGtAppId appKey:kGtAppKey appSecret:kGtAppSecret delegate:self]
/ / sign up for APNs
[self registerRemoteNotification]
Return YES
}
Register APNs to get DeviceToken:
/ * * Register APNs * /
-(void) registerRemoteNotification {
UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter]
Center.delegate = self
[center requestAuthorizationWithOptions: (UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionCarPlay) completionHandler: ^ (BOOL granted, NSError * _ Nullable error) {
If (! error) {
NSLog (@ "request authorization succeeded!")
}
}]
[[UIApplication sharedApplication] registerForRemoteNotifications]
}
The demo code is provided to developers in the demo, which can be modified according to the different iOS systems supported by APP. Our project supports iOS10 at a minimum.
Get CID information:
/ * * SDK starts successfully and returns cid * /
-(void) GeTuiSdkDidRegisterClient: (NSString *) clientId {
NSLog (@ "clientId:%@", clientId)
}
What are these three parameters kGtAppId, kGtAppKey and kGtAppSecret for, and how to obtain them? I looked back at the official website of the next post to figure out exactly how the three parameters of the applicant were bound to the bundleID I applied.
# how to get kGtAppId, kGtAppKey and kGtAppSecret
1. Create a push developer account
Visit a push developer center and apply for a push account
two。 Register new applications
Note: registering a new application is on the application management page rather than the message push page.
Enter the application name and application representation in the registration application interface, select a push product, check iOS, and enter the package name and bundleID, as shown in the following figure:
I'm a little confused here. When you create an application, you want to check iOS, but when you see that the Android platform is selected by default, and you need to enter the Android signature, what can I do if the signature is what? See in front of a hint on how to get, click, found that there is a SHA256 signature, holding a try attitude, directly copy over, Yo oh, can use oh, feel happy.
After the submission is successful, you can get kGtAppId, kGtAppKey and kGtAppSecret, enter the three parameters into our project, and then run the project to get the cid in the callback method of GeTuiSdkDidRegisterClient. Well, it seems that we have succeeded one by one, and we will continue to refuel until we are successful.
# register DeviceToken and count the number of clicks on APNs notifications
1. Register DeviceToken with a push server:
/ * remote Notification Registration successfully delegated * /
-(void) application: (UIApplication *) application didRegisterForRemoteNotificationsWithDeviceToken: (NSData *) deviceToken {
/ / register deviceToken with a push server for the convenience of developers, it is recommended to use a new method
NSLog (@ "deviceToken:%@", deviceToken)
[GeTuiSdk registerDeviceTokenData:deviceToken]
}
two。 Handle APNs notification click events:
Because our project is minimum adapted to iOS10, here I only add notification click events for iOS10 and later versions. If you want to be compatible with below iOS10, you can find it in a push demo.
IOS 10 and later to handle APNs notification click events
/ / iOS 10: triggered when you click a notification to enter App, and count the number of valid user clicks in this method
-(void) userNotificationCenter: (UNUserNotificationCenter *) center didReceiveNotificationResponse: (UNNotificationResponse *) response withCompletionHandler: (void (^) ()) completionHandler {
NSLog (@ "didReceiveNotification:%@", response.notification.request.content.userInfo)
/ / [GTSdk]: send the received APNs information to tweet statistics
[GeTuiSdk handleRemoteNotification:response.notification.request.content.userInfo]
CompletionHandler ()
}
3. Accept a transparent message sent through a push channel:
/ * * SDK receives callback of transparent message * /
-(void) GeTuiSdkDidReceivePayloadData: (NSData *) payloadData andTaskId: (NSString *) taskId andMsgId: (NSString *) msgId andOffLine: (BOOL) offLine fromGtAppId: (NSString *) appId {
/ / received a tweet message
NSString * payloadMsg = nil
If (payloadData) {
PayloadMsg = [[NSString alloc] initWithBytes:payloadData.bytes length:payloadData.length encoding:NSUTF8StringEncoding]
}
NSString * msg = [NSString stringWithFormat:@ "taskId=%@,messageId:%@,payloadMsg:%@%@", taskId,msgId, payloadMsg,offLine? @ ": @"]
NSLog (@ "\ n > [GexinSdk ReceivePayload]:% @\ n\ n", msg)
}
Get the transparent message, but when the application is in the background or the application kills, how do we get the APNs message? here we need to use the push certificate on a push platform. How do we get the push certificate? Because I am also engaged in pushing this book for the first time, and have stepped on a lot of holes, in order not to step on the same hole next time, so here is a regulation on how to make a push certificate.
# how to create a push certificate?
1. Enter the Apple developer Center and select the certificate option, as shown in the following figure:
two。 You must create an APPID before creating a push certificate, because the push certificate is bound to the APPID, as shown in the following figure:
Select allow push (Push Notifications) in the following App Services, as shown in the following figure:
After the 3.APPID is created, you need to create a push certificate at this time, and select the corresponding push certificate according to the desired environment, including the development environment push certificate and the production environment push certificate, and then associate it with the APPID you just created, as shown below:
At this point, we need to upload the CSR file. We go back to the desktop, open the keychain, apply for a certificate from the authority and save it to the local disk, as shown below:
In this way, the CSR file is created. We go back to the Apple developer Center, continue to create our push certificate, and choose to save the local CSR file, as shown below:
In this way, our push certificate is created. Find the downloaded push certificate in the local download and double-click to add it to the keychain. Then open the keychain to find the created push certificate, right-click to export the P12 certificate, and enter the certificate password, as shown below:
4. Open our push developer Center, and upload the correct APNs certificate in "push message push-Application list-Application configuration", as shown below:
The next most important moment comes, and that is the test to see if our push can succeed.
# push test
I tested the push on a push platform. Click the create push button on the previously created application in the application list, as shown below:
After entering, I was a little confused, because I didn't know the logic of a push SDK before. after asking about a push technical support, the technical support told me how to push on a push platform. I was too careless. People wrote very clearly in the first sentence. Push notification is currently only supported for Android users. IOS, please use transparent message. awkward! Then try tweeting through the message page. As shown in the following figure:
Transparent message test:
APNs message test
NICE ah, this application in the foreground, the application in the background and the application is killed in the case can receive push messages, awesome ah! It seems that we have successfully watched 90%!
# Notification Service Extension
When I was smug, I suddenly found that there was a multimedia push on the official website of a tweet. Shit, and this kind of operation, under the trend of curiosity, let me re-examine how to do multimedia push. Because we have previously created the target for the notification extension, we go straight to the code.
After 1.Notification Service Extension is added, two classes, NotificationService.h and NotificationService.m, are automatically generated in the project, including the following two methods:
-(void) didReceiveNotificationRequest: (UNNotificationRequest *) request withContentHandler: (void (^) (UNNotificationContent * _ Nonnull)) contentHandler {
Self.contentHandler = contentHandler
Self.bestAttemptContent = [request.content mutableCopy]
/ / [Test Code] TODO: you can change the notification style here. Eg: modify the title. The development phase can be used to determine whether to run the notification extension.
/ / self.bestAttemptContent.title = [NSString stringWithFormat:@ "% @ [WillIn]", self.bestAttemptContent.title]
/ / [GTSDK] Statistics APNs arrival and multimedia push support API. It is recommended to use this API.
[GeTuiExtSdk handelNotificationServiceRequest:request withAttachmentsComplete: ^ (NSArray * attachments, NSArray * errors) {
/ / self.bestAttemptContent.title = [NSString stringWithFormat:@ "% @ [Success]", self.bestAttemptContent.title]
Self.bestAttemptContent.attachments = attachments; / / set multimedia attachments in notification
Self.contentHandler (self.bestAttemptContent)
}]
}
In this method, we can process our APNs notifications and personalize them to the user. This method is called when the message pushed by APNs is delivered, and you can process the pushed content, and then use the contentHandler method to end the processing. However, if the processing time is too long, it will enter the serviceExtensionTimeWillExpire method for the final emergency processing.
-(void) serviceExtensionTimeWillExpire {
/ / [GTSDK] destroy SDK and release resources
[GeTuiExtSdk destory]
/ / self.bestAttemptContent.title = [NSString stringWithFormat:@ "% @ [Timeout]", self.bestAttemptContent.title]
Self.contentHandler (self.bestAttemptContent)
}
If the didReceiveNotificationRequest method does not call the contentHandler method to end processing within a limited time, the method will be called back before it expires. At this point, you can display your APNs message after emergency processing, and if it is not processed, the original APNs push is displayed.
The next step is to test whether our multimedia push is successful. I found a picture of a puppy on the Internet and pushed it directly on a push platform.
Multimedia testing
6, finally got it! It's very good.
However, before an old project said to integrate push, I know that the old project to use XCode integration, which for lazy people like me, it is a kind of torture, ah, torture is torture, should be done. But I've done it with CocoaPods before, and this time it's not easy to integrate with XCode. But in order to prevent mistakes, I still made a demo first, so that if I integrate on my own project, I will have more control and less holes to step on.
# XCode Integration
1. Import a push SDK:
two。 Library reference check:
3. Add system dependency libraries:
Libc++.tbd
Libz.tbd
Libsqlite3.tbd
Libresolv.tbd
Security.framework
MobileCoreServices.framework
SystemConfiguration.framework
CoreTelephony.framework
AVFoundation.framework
CoreLocation.framework
UserNotifications.framework (iOS 10 and above need to be added. Access via Optional)
AdSupport.framework (if you are using an IDFA-free version of SDK, you need to delete the AdSupport library)
Fortunately, the following steps are basically the same, the only relief!
4. Enable push function, background operation permission setting, and enable WiFi information authorization
Here is the same as the above steps, it will not be verbose.
5.copy code, this is our programmer's favorite, ha, done before, here is not cumbersome.
6. Add Notification Service Extension
(1)。 Open XCode and select File- > New- > Target- > Notification Service Extension from the menu. As shown in the following figure:
Note: the Bundle Identifier of 1.Extension cannot be the same as the Bundle Identifier of Main Target (that is, your own App Target), otherwise a duplicate error of BundleID will be reported. The Bundle Identifier of 2.Extension needs to be under the namespace of Main Target, for example, the BundleID of Main Target is ent.getui.xxx, then the BundleID of Extension should be similar to ent.getui.xxx.yyy. Failure to do so will cause naming errors.
I saw this on an official website. I also stepped on this hole before, so I recorded it here.
After adding Notification Service Extension, the corresponding Target is generated. Click the Finish button will pop up whether to activate the Target corresponding to the scheme option box, select Activate, if the option box does not pop up, you need to add the corresponding scheme. As shown in the following figure:
(2) after NotificationService Extension is added, two classes NotificationService.h and NotificationService.m will be automatically generated in the project
It's the same as above, so it's not cumbersome.
(3)。 Add GtExtensionSdk dependent libraries
Select the Target corresponding to Notification Service Extension, and add the following dependent libraries:
Libz.tbd
Libsqlite3.tbd
GTExtensionSDK.framework
UserNotifications.framework
(4) .XCode 10 recommends enabling WiFi information authorization: above Xcode 10.x, find Capabilities-> Access WiFi Information in the application Target settings, and confirm that the switch has been set to ON. As shown in the following figure:
(5)。 Enable Http access support for multimedia addresses:
# # problems encountered during Integration
Invalid deviceToken
What impressed me most was the invalid deviceToken. When I was testing the APNS push, I asked the technical support over there. They said that I could test it in the application configuration first, and then I took my deviceToken to test it. The result indicated that I was invalid deviceToken, I was dizzy, and then I continued to consult the technical support of a push. They were quite patient and told me that this reason may be a problem with my certificate environment. After some careful inspection, I found that I uploaded a general certificate on a push platform, and then the authorization certificate on my XCode was in the development environment, so I got the deviceToken in the development environment. If I test it, of course there will be errors. There are two solutions: first, upload the push certificate under the development environment on a push development platform. Second: replace your authorization certificate with a production environment.
Notify that the code that modifies the title in the extension does not take effect
Self.bestAttemptContent.title = [NSString stringWithFormat:@ "% @ [Success]", self.bestAttemptContent.title]
I found that there is such a line of code in demo. After I opened this line of code, I pushed a message and found that the title had not changed. I was shocked! Ask a tweet technical support, a tweet technical support said, let me run the main target, and then run the notification extension, when running the notification extension will let us find the master targetAPP, select the master target, and then push will have, um, think about it, this should be the bug of XCode.
This is the end of the article on "what are the methods of iOS SDK integration?" Thank you for reading! I believe you all have a certain understanding of the knowledge of "what are the methods of iOS SDK integration". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.